开发者

C# storing a string value from textbox and calling that variable

I am trying to write a C# based winform to do a several automated text entry processes as part of my work. I wish to have certain things entered into textboxes, saving me from doing it manually.

I am able to store the inputted data from a textbox as a string variable but I wish to know how I could call this upon clicking on a seperate textbox. Thus "copying" the data inside one box and placing it elsewhere, on several occasions.

I am not 100% sure this is how the string should be开发者_运维技巧 stored but this is my attempt:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string chattextbox;
    }

I want the stored string variable to be entered below

private void button1_Click(object sender, EventArgs e)
{
    Application.OpenForms["Loginwindow"].BringToFront();
}

After bringing the window to the front I wish to call the data from the string variable and have it KeyPress "Enter"

Hope I have made it clear as to what I am trying to do.

Thanks all


In your TextChanged event handler, you have a string variable declared, but you're not assigning anything to it at this point. To save it off, you need to do this:

string chattextbox; // member variable or property in your class

private void textBox1_TextChanged(object sender, EventArgs e)
{
    chattextbox = textBox1.Text;
}

Note that chattextbox must be a member variable in your Form class, otherwise, if you have the variable declared in the scope of the method (like in your code), it will be gone when the method returns.

You actually don't need to save it off in this way though, it will be accessible in the textBox.Text value at pretty much any time in the form. A lot of Winforms controls have a Text property which holds the string value shown in the control, whether it's a button, textbox, label, etc. You can most likely remove this TextChanged handler completely, and just implement your button1_Click (see below).

In your button1_Click handler, you can get the value like this. To pass the value to your other window, you'll need a property on the other window you can assign, or you'll need to pass it in a constructor before showing it.

private void button1_Click(object sender, EventArgs e)
{
    Loginwindow.SomeProperty = textBox1.Text; // Set the string value on your other form somehow ???
    Application.OpenForms["Loginwindow"].BringToFront();
}


Here, you are not assigning the value of the textbox to the string, just initializing a string variable.

Private Void textBox1_TextChanged(object sender, EventArgs e)
{
    string chattextbox;
    chattextbox=textBox1.Text;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜