0

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 ?

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by saravind84
0

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 !

0

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!

0

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)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.