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. Get value from other form variable

Get value from other form variable

Scheduled Pinned Locked Moved C#
helptutoriallearning
8 Posts 4 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
    Edwin Syarief
    wrote on last edited by
    #1

    Hi, I'm a beginner, so please help me. I want to know how to get value from other form variable or other form control, from our form. Thanx Regard, Edwin46

    I R T 3 Replies Last reply
    0
    • E Edwin Syarief

      Hi, I'm a beginner, so please help me. I want to know how to get value from other form variable or other form control, from our form. Thanx Regard, Edwin46

      I Offline
      I Offline
      I explore code
      wrote on last edited by
      #2

      hi, if u have one form as form1 and other as form2 and u want to show data from form1 to form2 then u can pass the values from form1 through the overloaded contructor of the form2.In form1 u could public properties to hold values for passing and then in the constructor pass these prop vals.I hope its clear.

      E 1 Reply Last reply
      0
      • E Edwin Syarief

        Hi, I'm a beginner, so please help me. I want to know how to get value from other form variable or other form control, from our form. Thanx Regard, Edwin46

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        To get the value of a variable "V" in form "X" from form "Y", expose V as a public property in X, store a reference to an instance of X in Y, and access it. For example:

        public class X : Form
        {
        // The public property
        public int V {
        get { return v; }
        }

        // Private member variable
        int v = 0;
        ...
        }

        public class Y : Form
        {
        // Reference to instance of form X
        X x;

        void someMethod()
        {
        // x must have been properly initialized!
        int valueOfVinFormX = x.V;
        }
        }

        While this is probably the easiest way, it's not the recommended method of accessing data in other forms. But since you're a beginner, I decided to post a simple and direct answer. /ravi

        This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

        E 1 Reply Last reply
        0
        • I I explore code

          hi, if u have one form as form1 and other as form2 and u want to show data from form1 to form2 then u can pass the values from form1 through the overloaded contructor of the form2.In form1 u could public properties to hold values for passing and then in the constructor pass these prop vals.I hope its clear.

          E Offline
          E Offline
          Edwin Syarief
          wrote on last edited by
          #4

          Thanx a lot regard Edwin46

          1 Reply Last reply
          0
          • R Ravi Bhavnani

            To get the value of a variable "V" in form "X" from form "Y", expose V as a public property in X, store a reference to an instance of X in Y, and access it. For example:

            public class X : Form
            {
            // The public property
            public int V {
            get { return v; }
            }

            // Private member variable
            int v = 0;
            ...
            }

            public class Y : Form
            {
            // Reference to instance of form X
            X x;

            void someMethod()
            {
            // x must have been properly initialized!
            int valueOfVinFormX = x.V;
            }
            }

            While this is probably the easiest way, it's not the recommended method of accessing data in other forms. But since you're a beginner, I decided to post a simple and direct answer. /ravi

            This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

            E Offline
            E Offline
            Edwin Syarief
            wrote on last edited by
            #5

            Thanx this is very helpfull regard Edwin46

            1 Reply Last reply
            0
            • E Edwin Syarief

              Hi, I'm a beginner, so please help me. I want to know how to get value from other form variable or other form control, from our form. Thanx Regard, Edwin46

              T Offline
              T Offline
              T EDY
              wrote on last edited by
              #6

              you should try make a public class that returned a control u want to be call in another form:)

              E 1 Reply Last reply
              0
              • T T EDY

                you should try make a public class that returned a control u want to be call in another form:)

                E Offline
                E Offline
                Edwin Syarief
                wrote on last edited by
                #7

                Can U give me an example, Please. Thanx a lot regard Edwin46

                T 1 Reply Last reply
                0
                • E Edwin Syarief

                  Can U give me an example, Please. Thanx a lot regard Edwin46

                  T Offline
                  T Offline
                  T EDY
                  wrote on last edited by
                  #8

                  for example in form 1: public DataGridView DataGridView1 { get { return MyDataGridiew; } } and in form 2 you can call MyDataGridview Hope it solve your problems:) Regard, TCim

                  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