Reading values from another form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Reading values from another form

    When you pull up a second form in your program,
    how do you get the values from the form?

    Form2 dataForm = new Form2();

    dataForm.ShowDi alog();
    if (dataForm.Dialo gResult == DialogResult.OK )
    {
    // I want to get the textBox1 data from the form
    }


    Thanks.


  • Marc Gravell

    #2
    Re: Reading values from another form

    In this case the best approach would be to expose what you want as public
    properties on Form2 - i.e.

    public string CustomerName {
    get {return textBox1.Text;}
    }

    then you can access dataForm.Custom erName

    For info, ShowDialog() [unlike Show()] doesn't Dispose() the form, so it
    would be best to be "using" this - i.e.
    using(Form2 dataForm = new Form2()) {
    // do everything you want
    }
    // it is now disposed
    Marc


    Comment

    • Cartoper

      #3
      Re: Reading values from another form

      Marc's solution most definitly works, I like to be a bit more complex
      and make a private variable to hold the value and then a public
      property to read the private variable. This way, when the user click
      on OK on the second form, the second form can validate and make sure
      everything is correct before loading the value into the variable and
      exiting. If the user clicks on cancel, the value is never populated
      and the first form, even if it tries to get the value, cannot.

      Comment

      • Steve

        #4
        Re: Reading values from another form

        Thanks Marc.

        "Marc Gravell" <marc.gravell@g mail.comwrote in message
        news:%23w0l7VPi IHA.4396@TK2MSF TNGP04.phx.gbl. ..
        In this case the best approach would be to expose what you want as public
        properties on Form2 - i.e.

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Reading values from another form

          Steve,

          I use in those cases internal properties.

          Cor

          "Steve" <stevensm@ston. nischreef in bericht
          news:em8rg$OiIH A.4320@TK2MSFTN GP06.phx.gbl...
          When you pull up a second form in your program,
          how do you get the values from the form?
          >
          Form2 dataForm = new Form2();
          >
          dataForm.ShowDi alog();
          if (dataForm.Dialo gResult == DialogResult.OK )
          {
          // I want to get the textBox1 data from the form
          }
          >
          >
          Thanks.
          >

          Comment

          Working...