How can I pass values from one form to another?

前端 未结 10 2577
野的像风
野的像风 2020-12-02 00:43

Consider the situation where I have two windows forms, say F1 and F2. After using F1, I have now called F2.ShowDialog().

相关标签:
10条回答
  • 2020-12-02 01:28

    May be I am late. but all those who may want.

    In destination form have a constructor defined like this

    public partial class Destination: Form
    {
        string valueAccepted;
        public Destination(string _valuePassed)
        {
            InitializeComponent();
            this.valueAccepted= _valuePassed;
        }
    }
    

    and in Source Form call the form like this

            Source sourceForm= new Source ("value Passed");
            sourceForm.ShowDialog();
    

    this way "value Passed" is passed from Form Source to Form Destination

    0 讨论(0)
  • 2020-12-02 01:29

    let me reframe the question i ve 2 form f1, f2... i cal Form f2 = new Form(); f2.ShowDialog();

    // now i need to pass the parameter from f1 window to f2 (which is modal dialog box) also return value from f2 form to f1

    //now 'm using varibles that are in common namespace (for both f1 , f2)

    0 讨论(0)
  • 2020-12-02 01:30

    Add this code in the relevant place in your from f1.

    Form f2 = new Form();
    f2.ShowDialog();
    

    HTH

    0 讨论(0)
  • 2020-12-02 01:33

    Has anyone considered simply passing the value to the form in the tag attribute.

    Form newForm = new form();
    newForm.Tag = passValue;
    newform.showmodal();
    

    when newform is shown the load (or any other) routine can use the data in tag

    public void load()
    {
      if (this.Tag.length > 0)
      {
         // do something with the data
      }
    }
    
    0 讨论(0)
提交回复
热议问题