我正在尝试学习Csharp,并且我过去许多天都在许多不同的线程上进行了阅读,但是似乎没有一个适合我的问题和我的实际知识。
我有一个带有称为“信息栏”的文本框的Form1,用于显示可能来自Form2或其他类的方法的所有信息。
通过From2进行信息栏的更新可以很好地处理如下事件:
public partial class Form2 : Form
{
public delegate void dgNewTexEventHandler(object sender, string NewText);
public event dgNewTexEventHandler OnNewTextForInfobar_Form1;
public Form2()
{
InitializeComponent();
}
private void cmd_pass_text_to_infobar_Form1_Click(object sender, EventArgs e)
{
OnNewTextForInfobar_Form1?.Invoke(this, textBox_Form2.Text);
}
}
并在Form1中订阅该事件
secondForm.OnNewTextForInfobar_Form1 += UpdateInfobar;
现在,我需要使用一种完成数据处理的不同类(公共类DataProcessing)的方法来更新信息栏。 我似乎无法使它与同一事件一起使用,因为它属于Form2。 尝试执行第二个事件也没有成功,因为DataProcessing中的事件似乎没有被触发。
public partial class Form1 : Form
{
private void cmd_write_to_infobar_via_class_DataProcessing_with_event_Click(object sender, EventArgs e)
{
DataProcessing CallMethodInClassDataprocessing = new DataProcessing();
CallMethodInClassDataprocessing.write_string_to_label_Form1(textBox_Form1.Text);
CallMethodInClassDataprocessing.OnNewTextForInfobar_DataProcessing += UpdateInfobar;
}
private void UpdateInfobar (object sender, string UpdateText)
{
lbl_infobar.Text = UpdateText;
}
}
public class DataProcessing
{
public delegate void dgNewTexEventHandler(object sender, string NewText);
public event dgNewTexEventHandler OnNewTextForInfobar_DataProcessing;
public void write_string_to_label_Form1(string text)
{
// fire event and send text to infobar in Form1 : Does NOT work here! (??)
OnNewTextForInfobar_DataProcessing?.Invoke(this, text);
}
}
但是对我来说,使用多个事件从不同的窗体或类更新一个文本框似乎不是正确的方法。
也许您可以建议我一种更好的方法,该方法显然基于不太复杂的结构。
答案 0 :(得分:0)
一种非常简单的方法是为此类问题准备通用处理程序
例如,我们开始定义一个接口
// If some class wants to implement this interface it needs to
// have a method called WriteMessage that receives a string and returns nothing
public interface IReceiver
{
void WriteMessage(string message);
}
现在创建一个静态类,该类的工作是处理在发件人和接收并显示消息的对象之间传递的文本
public static class Messenger
{
private static IReceiver recv = null;
// Initialize the internal variable recv with the instance
// passed. We don't care what type is, we are only interested
// in a class that implements the IReceiver interface
public static void InitializeReceiver(IReceiver f)
{
recv = f;
}
// Call whatever class instance has been passed in the Initialize
// we know for sure that there is a WriteMessage method to call
public static void SendMessage(string message)
{
if(recv != null) recv.WriteMessage(message);
}
}
现在您的第一个表单应该实现IReceiver接口
public class Form1 : Form, IReceiver
{
Label l = new Label();
public Form1()
{
this.Controls.Add(l);
// We pass to the Messenger static class the instance of this form
// because we implement the IReceiver interface.
Messenger.InitializeReceiver(this);
}
// This is how we satisfy the Interface requirement.
// We add a method called WriteMessage that receives a string
// and doesn't return anything
public void WriteMessage(string message)
{
// Do whatever you want the message for this class
l.Text = message;
}
}
这时您可以在代码中的任何地方调用
Messenger.SendMessage("This is a test message");