在表单之间传递字符串

时间:2017-04-09 14:55:48

标签: c# winforms event-handling

我在C#中有以下Windows窗体程序:

表单1上有一个ListBox和Button。按下按钮时,它将显示Form2,其上有TextBox和Button。当按下表单2上的按钮时,它将把文本放入Form1上的ListBox。下面是每个表单的代码,然后是我正在使用的类。任何建议都会很棒。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();

        frm2.NewTextChanged += new EventHandler<CustomEvent>(form2_NewTextChanged);
        frm2.ShowDialog();            
        // Unsubscribe from event
        frm2.NewTextChanged -= form2_NewTextChanged;           
        frm2.Dispose();
        frm2 = null;
    }
    private void form2_NewTextChanged(object sender, CustomEvent e)
    {
        //Text = e.Text;
        lbItem.Items.Add(e.Text);
    }
}

public partial class Form2 : Form
{        
    public event EventHandler<CustomEvent> NewTextChanged;
    private string newText;
    public Form2()
    {
        InitializeComponent();
    }
    public string NewText
    {
        get { return newText; }
        set
        {
            if (newText != value)
            {
                newText = value;
                OnNewTextChanged(new CustomEvent(newText));
            }
        }
    }
    protected virtual void OnNewTextChanged(CustomEvent e)
    {            
        EventHandler<CustomEvent> eh = NewTextChanged;
        if (eh != null)
            eh(this, e);
    }
    private void btnSendToForm1_Click(object sender, EventArgs e)
    {
        newText = textBox1.Text;
    }
}

public class CustomEvent : EventArgs
{     
    private string text;
    public CustomEvent(string text)
    {
        this.text = text;
    }
    public string Text
    {
        get { return text; }
    }
}

我想使用自定义处理程序。有什么建议吗?

3 个答案:

答案 0 :(得分:1)

以下是Form2中自定义事件的示例:

public partial class Form2 : Form
{

    public delegate void NewText(string item);
    public event NewText NewTextChanged;

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (NewTextChanged != null)
        {
            NewTextChanged(textBox1.Text);
        }
    }

}

...以下是Form1订阅该事件的方式:

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.NewTextChanged += Frm2_NewTextChanged;
        frm2.Show();
    }

    private void Frm2_NewTextChanged(string item)
    {
        lbItem.Items.Add(item);
    }

答案 1 :(得分:0)

Form1代码,只是带有show和hide的普通表单

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.ShowDialog();
            listBox1.Items.Add(f.test);
        }

Form2代码 创建一个Form1可以读取的公共变量

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public string test;
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            test = textBox1.Text;
            this.Hide();
        }
    }
}

答案 2 :(得分:0)

使用事件在表单之间传输数据是一种很好的方式。

您的代码看起来不错。它有一个问题,但由于它会运行良好,但Form2的事件不会触发。

Form2按钮的clickevent中,您需要设置属性NewText的值而不是字段newText。因此,如果您将btnSendToForm1_Click更改为以下内容,那么一切都应该正常工作。

private void btnSendToForm1_Click(object sender, EventArgs e)
{
    this.NewText = textBox1.Text;
}