Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to pass variables to a new form?

How to pass variables to a new form?

Scheduled Pinned Locked Moved C#
questionhelptutorial
16 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Emmet_Brown
    wrote on last edited by
    #1

    Hi everyone. I've got a listBox and a list of elements in it. I've got another windows form, and it will be created after an item in the listBox is double clicked. What I'm asking is; I want to "read" the name of the element from the list box, and then fill the new form according to this information. How can I pass the listBox1's selected Item's text to the new form? can anybody help?

    N M L 4 Replies Last reply
    0
    • E Emmet_Brown

      Hi everyone. I've got a listBox and a list of elements in it. I've got another windows form, and it will be created after an item in the listBox is double clicked. What I'm asking is; I want to "read" the name of the element from the list box, and then fill the new form according to this information. How can I pass the listBox1's selected Item's text to the new form? can anybody help?

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Create a constructor for the new form that takes the information necessary.


      I know the language. I've read a book. - _Madmatt

      E 1 Reply Last reply
      0
      • E Emmet_Brown

        Hi everyone. I've got a listBox and a list of elements in it. I've got another windows form, and it will be created after an item in the listBox is double clicked. What I'm asking is; I want to "read" the name of the element from the list box, and then fill the new form according to this information. How can I pass the listBox1's selected Item's text to the new form? can anybody help?

        M Offline
        M Offline
        Md Marufuzzaman
        wrote on last edited by
        #3

        You can just simply write a custom property method for that.

        Thanks Md. Marufuzzaman


        Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

        E 1 Reply Last reply
        0
        • N Not Active

          Create a constructor for the new form that takes the information necessary.


          I know the language. I've read a book. - _Madmatt

          E Offline
          E Offline
          Emmet_Brown
          wrote on last edited by
          #4

          Oh I've not created a constructor about 3 years, I don't remember how to but I'll try, I'll try the articles

          N 1 Reply Last reply
          0
          • M Md Marufuzzaman

            You can just simply write a custom property method for that.

            Thanks Md. Marufuzzaman


            Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

            E Offline
            E Offline
            Emmet_Brown
            wrote on last edited by
            #5

            you mean a public class that can be reached from the new form? is this it?

            M D 2 Replies Last reply
            0
            • E Emmet_Brown

              Oh I've not created a constructor about 3 years, I don't remember how to but I'll try, I'll try the articles

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              You need to research articles to create a class constructor?!? :omg: Go read some books, take a class, whatever it takes but stop trying to write applications until you do.


              I know the language. I've read a book. - _Madmatt

              E 1 Reply Last reply
              0
              • E Emmet_Brown

                you mean a public class that can be reached from the new form? is this it?

                M Offline
                M Offline
                Md Marufuzzaman
                wrote on last edited by
                #7

                Yes, but it depends on how you are going to use the class... you can also use a constructor.

                Thanks Md. Marufuzzaman


                Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

                1 Reply Last reply
                0
                • N Not Active

                  You need to research articles to create a class constructor?!? :omg: Go read some books, take a class, whatever it takes but stop trying to write applications until you do.


                  I know the language. I've read a book. - _Madmatt

                  E Offline
                  E Offline
                  Emmet_Brown
                  wrote on last edited by
                  #8

                  ahhaah just never needed to create one until now =) (just graduated) take it easy, thanks for help and advice :D

                  N L 2 Replies Last reply
                  0
                  • E Emmet_Brown

                    you mean a public class that can be reached from the new form? is this it?

                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #9

                    That would be awful IMHO if all you need is to send selected item. You should used a parameterized constructor. Or have a public event handler so that you can handle the double click event in the Form where you need to get the selected value. Constructor would be the simplest way though.

                    It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

                    1 Reply Last reply
                    0
                    • E Emmet_Brown

                      ahhaah just never needed to create one until now =) (just graduated) take it easy, thanks for help and advice :D

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #10

                      Just graduated? And this wasn't covered? Where did you graduate from?


                      I know the language. I've read a book. - _Madmatt

                      1 Reply Last reply
                      0
                      • E Emmet_Brown

                        ahhaah just never needed to create one until now =) (just graduated) take it easy, thanks for help and advice :D

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        tatarrullaz wrote:

                        just graduated

                        You should go right back to school. I'd written more constructors than I can count before I even started my first year at university. You have no excuse.

                        1 Reply Last reply
                        0
                        • E Emmet_Brown

                          Hi everyone. I've got a listBox and a list of elements in it. I've got another windows form, and it will be created after an item in the listBox is double clicked. What I'm asking is; I want to "read" the name of the element from the list box, and then fill the new form according to this information. How can I pass the listBox1's selected Item's text to the new form? can anybody help?

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          class Form2 {
                          private string m_Data;

                          internal Form2(string data) {
                              m\_Data = data;
                          }
                          
                          private void Form2\_Load(...) {
                              Use m\_Data here........
                          }
                          

                          }

                          class Form1 {
                          private void ListBox1_DoubleClick(...) {
                          Form2 f2 = new Form2(ListBox1.SelectedText);
                          f2.Show();
                          }
                          }

                          N 1 Reply Last reply
                          0
                          • L Lost User

                            class Form2 {
                            private string m_Data;

                            internal Form2(string data) {
                                m\_Data = data;
                            }
                            
                            private void Form2\_Load(...) {
                                Use m\_Data here........
                            }
                            

                            }

                            class Form1 {
                            private void ListBox1_DoubleClick(...) {
                            Form2 f2 = new Form2(ListBox1.SelectedText);
                            f2.Show();
                            }
                            }

                            N Offline
                            N Offline
                            Not Active
                            wrote on last edited by
                            #13

                            "Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime" Same goes for code.


                            I know the language. I've read a book. - _Madmatt

                            L 1 Reply Last reply
                            0
                            • E Emmet_Brown

                              Hi everyone. I've got a listBox and a list of elements in it. I've got another windows form, and it will be created after an item in the listBox is double clicked. What I'm asking is; I want to "read" the name of the element from the list box, and then fill the new form according to this information. How can I pass the listBox1's selected Item's text to the new form? can anybody help?

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              SelectedObject = GetSelectedObjectFromListBox(); MyPopUpForm form = new MyPopUpForm(); form.SelectedObject = SelectedObject; form.Show(); or SelectedObject = GetSelectedObjectFromListBox(); MyPopUpForm form = new MyPopUpForm(SelectedObject ); form.Show(); depending on your preference. In the 1st case you need a public property in your popup form, called SelectedObject. In the setter, you need to do what you need to do. In the 2nd case you need a constructor that takes a SelectedObject as a parameter. It is usually inadvisable to use this constructor to populate your form - store it then populate when the form is shown.

                              ___________________________________________ .\\axxx (That's an 'M')

                              1 Reply Last reply
                              0
                              • N Not Active

                                "Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime" Same goes for code.


                                I know the language. I've read a book. - _Madmatt

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                You are right. Until they know what a fish is, we have to give them fish :-)

                                N 1 Reply Last reply
                                0
                                • L Lost User

                                  You are right. Until they know what a fish is, we have to give them fish :-)

                                  N Offline
                                  N Offline
                                  Not Active
                                  wrote on last edited by
                                  #16

                                  No, you missed the point. We don't give them the code, we teach them to code.


                                  I know the language. I've read a book. - _Madmatt

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups