windows forms + popup +variable value

Looking for some help, if you have the time (anyone) it'd be great.

Here's the deal. I've got a windows form...dropdown list of states. If Florida is selected, I want a pop-up window to ask for a $$ amount. Then that value in the pop-up has to be stored in a variable so I can do some math on it before displaying it in a summary box.

Any idea how to do a windows pop-up, grab a value entered, and then display in a text box with something like

SummaryBox.Items.Add ("Amount entered in Florida Pop-up window: " + florida_value_entered_from_popup + ".");
//where florida_value_entered_from_popup is just a decimal variable

Thank you all again for taking the time to help out a new guy.

-Corby-

PS any good starter books out there that aren't geared towards 7th graders or MIT grads



Answer this question

windows forms + popup +variable value

  • Kandikan

    I'm pulling my hair out over this.  I know it's unbelievably simple but I don't have a C# book yet and the details out on the web don't seem to be helping...

    I can't get form2 to show so I can input text and then pass it back to form1.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    Setup is Form1 has a dropdown box, if Florida is selected, I need form2 to show.  In form2, one text box for input.  Do input, click OK, and that value needs to go back to form1 as a variable so I can do some math on it.

     

    Any help for a very new guy greatly appreciated in advance!

     
    -Corby-


  • iHEARTmicrosoft_BUT

    Sorry for not being more clear. I have included some code below that may help you. Mind, I haven't compiled this code so I don't know if the syntax is 100% correct (out of time), but it should help you get on your way. Pay attention to the comments as they explain just about everything I'm trying to say. I also don't want to take the fun away from you - discovering an answer on your own will help you to better understand what's going on. Keep up the good work.

    class Form1 : System.Windows.Forms.Form

    {

      // make sure this variable is public so we can access it from Form2

      public string putTheVariableHere = "";

      // this is just a button click handler; this could be any handler of your choice

      void Button_Click (object sender, EventArgs e)

      {

        // NOTE: look at the constructor for Form2

        Form2 frm2 = new Form2(this);

        frm2.Show();

      }

    }

    class Form2 : System.Windows.Forms.Form

    {

      // use this to store a reference to the Form1 object

      private Form m_refToForm1 = null;

      // UPDATED CONSTRUCTOR: I'm passing a Form object (Form1) to this constructor so I can store a reference to it

      public Form2 (Form form1)

      {

        // store the reference to the form1 object so we can access it from this form

        m_refToForm1 = form1;

      }

      // handler for the "save" button or whatever handler needs to update form1

      void Button_Click (object sender, EventArgs e)

      {

        // reference the "putTheVariableHere" string in form1 and store the text from my textbox on form2

        ((Form1)m_refToForm1).putTheVariableHere = txtThisIsMyTextBox.Text;

        // close this form

        this.Close();

      }

    }


  • Phil Seastrand

    Alright, I sort of have something like this in Form1.

    Here is the code I'm using in Form1 to call open Form2.  Keep in mind that I only want this pop-up to occur when the statecombo box (without having to click any buttons).  This works for the pop-up.

    private void statecombo_SelectedIndexChanged(object sender, System.EventArgs e)
      {
        if (Convert.ToString(statecombo.Text)=="Florida")
         {
           Form2 newform =
    new Form2();
           newform.Show();
         }
       }

    So when Florida is selected, Form2 pops up.  I need the value from textBox1 on Form2 to be sent to a variable in Form1 called "floridavariable".  This is the part that I'm having a problem with.

    I'm wondering if the first part of code you mentioned in the above post, isn't meshing with what I'm using...for example, you use the (this) in line 1 of your suggeston and if I put (this) in my above code, I get errors. 

    And when I try to modify your code for Form2 to work with my form names and variable names, it's tossing an error on "refToForm1"...so I'm mentally missing something there.  Can you perhaps clarify what I might be able to do with the "refToForm1" piece

    Thanks again for your help.

    -Corby-



  • Scott Berry

    Maybe use some form of global variable   The app is small, will only have 1 user, and even though I know global variables are not good practice, it might be an easy fix to what has turned out to be a full day of web searching, trial and error, and failure after failure on my end...  Any thoughts   Thank you for your time.
    -Corby-

  • dydoria

    Beautiful!

    It took me a while to figure out what goes where within my all-over-the-place code (and long weekend) but after messing with it for a while, I appear to have it working. I also think I'm starting to understand passing objects with this example.

    Huge help, I thank you deeply.  Much appreciated.

    -Corby-

  • Visio User

    I don't believe there is a .NET Framework class that will let you do this natively. So I think the only choice you have is to create a Windows Form that emulates the pop-up functionality you're looking for, but includes a text box and returns the entered value.

    Hope that helps.

  • kowalsky

    K, got the second form to load when Florida is selected from the dropdown list.

    My question now is geared towards getting that value from textbox1 in form2 back to form1 when OK is clicked on form2.

    Tnx again all.  This is a very helpful community.

  • Ensynch

    In your constructor for Form2 pass a reference to Form1 and store it as a local variable, something like this:

    Form2 frm2 = new Form2(this);
    frm2.ShowDialog();

    Then you can do something like this in the button event handler on Form2:

    ((Form1)refToForm1).VariableInForm1 = myTextBox.Text;
    this.Close();

  • windows forms + popup +variable value