I have created a string in (e.g. Button1_click)

Call that string "textboxtext"
now for some basic text to assign to the string I did the following

public void button1_Click(object sender, EventArgs e)
        {
            string textboxtext;
            textboxtext = textBox1.Text;
            textboxtext += textBox2.Text;
            textboxtext += textBox3.Text;
        //Some other code later on

        }

Now later on in my code I have lets say Button2_click SO :

public void button2_Click(object sender, EventArgs e)
        {
//I want to call the string textboxtext here so I can edit it but it doesnt let me....
        }

I tried changing this part of the code:

public void button1_Click(object sender, EventArgs e)

into:

public string button1_Click(object sender, EventArgs e)

but because I have other code underneathe the current code in button1_click I get the error :

not all code paths return a value

So how can I "call" the string textboxtext from button1_click into button2_click ?

Recommended Answers

All 3 Replies

Doesnt matter, I did it the noob way - at the end I just coded it so that "textbox173.text = textboxtext;" and I made textbox173.visible = false; ! then on button2_click i got the string from textbox173 !

You can't.
Create a private field or a property in your form that contains the two buttons, and you can go ahead.
What you did was defining a variable local to the click method, once this method has finished, your local variable is vanished!

Member Avatar for saravind84

Declare the string variable textboxtext out side the function (class level variable)
Then you can use this varibale in all the functions inside that class ie (button1_Click and button2_Click)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.