Jump to content

How to get the value of a label in one form to another form in C#?

so i have a value in a label of one form and i want to get this value to another form and assign it to a variable for doing some calculation.is there a way to get this done?

Link to comment
Share on other sites

Link to post
Share on other sites

Is this a fixed (non editable) WinForms label? If so, why not just declare it as a variable inside the other form?

Failed to load signature. Error 404 - Witty comment not found

Link to comment
Share on other sites

Link to post
Share on other sites

--- Wrong thread, sorry! ---

Failed to load signature. Error 404 - Witty comment not found

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, M.Yurizaki said:

I forget the property, but I'm sure it's label.text. Grab that, then pass it into whatever else you need.

 

However, I would argue using GUI elements to store your data is a very bad idea.

Agreed store the value in a global variable then load that into the label on form load or whenever you need it

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, vorticalbox said:

Agreed store the value in a global variable then load that into the label on form load or whenever you need it

i have currently done it that way but i have faced a problem. the value of the global variable will be updated in another method then i want this updated value to be sent to another form.i have used get and set methods to do this but the value that these methods pass is the value that was there in the variable before it is updated.is there anyway to pass the updated value?

Link to comment
Share on other sites

Link to post
Share on other sites

You would declare another variable in your second form, and when you are creating it, change the value.

An example would be

            SettingsF settingsForm = new SettingsF(); //Creating a new form
            settingsForm.String1 = Textbox1.Text; //Setting String1 from settingsform to the text from textbox1 in the original form

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×