How to pass textbox data between two forms?

后端 未结 3 587
旧巷少年郎
旧巷少年郎 2021-01-28 14:14

How to send textbox value to textbox between two forms without Show()/ShowDialog() by button? I want to textBox will get value without open form.

相关标签:
3条回答
  • 2021-01-28 14:44

    To pass information from a parent from to a child form you should create a property on the child form for the data it needs to receive and then have the parent form set that property (for example, on button click).

    To have a child form send data to a parent form the child form should create a property (it only needs to be a getter) with the data it wants to send to the parent form. It should then create an event (or use an existing Form event) which the parent can subscribe to.

    An example:

    namespace PassingDataExample
    {
        public partial class ParentForm : Form
        {
            public ParentForm()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                ChildForm child = new ChildForm();
                child.DataFromParent = "hello world";
    
                child.FormSubmitted += (sender2, arg) =>
                {
                    child.Close();
    
                    string dataFromChild = child.DataFromChild;
                };
    
                child.Show();
            }
        }
    }
    
    namespace PassingDataExample
    {
        public partial class ChildForm : Form
        {
            public ChildForm()
            {
                InitializeComponent();
            }
    
            public string DataFromParent { get; set; }
    
            public string DataFromChild { get; private set; }
    
            public event EventHandler FormSubmitted;
    
            private void button1_Click(object sender, EventArgs e)
            {
                DataFromChild = "Hi there!";
    
                if (FormSubmitted != null)
                    FormSubmitted(this, null);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-28 14:48

    I don't know what exactly you mean by saying "without Show()/ShowDialog()", but that is not relevant anyway or I'll just assume here further that you have both windows open (doesn't matter how you accomplished that).

    You would like to avoid much coupling between two forms, especially not implementation details like a textbox etc. You could work with delegates and events to trigger the "sending" of data between your two forms. You can then easily pass event data and your subscribed other form (or any other object as a matter of fact) doesn't know the exact implementation details of your form, it only knows the data it will receive through the delegate (event). I am not going to post all code here, because it is already nicely explained at following URL: http://www.codeproject.com/Articles/17371/Passing-Data-between-Windows-Forms .

    0 讨论(0)
  • 2021-01-28 14:52

    To access the textbox data you need to use: textBox1.Text

    a form is an object so you can define a method that updates the text box value (you can expose the textbox itself with a public accessor)

    0 讨论(0)
提交回复
热议问题