Trying to Fill a text box with strings from other textboxes in C#

5.5k views Asked by At

I'm trying to create a simple AD Gui for our HR department to use but I'm hung up on something simple. Our email address format is [email protected]. I have a text box for first name and for last name and I want the text box for the email to autofill with [email protected]. I tried

string FirstName = txtBoxFirstName.text;
string LastName = txtBoxLastName.text;    
txtBoxEmail.Text = FirstName + "." + LastName + "@organization.org";

I also tried not setting them as strings, but that didn't work either. I even tried to have the Email text box fill after the "Last Name" text box loses focus by putting

private void txtBoxLastName_Leave(object sender, System.EventArgs e)
{
    txtBoxEmail.Text = txtBoxFirstName.text + "." + txtBoxLastName.text + "@organization.org";
}

I'm making this in a Windows Form application in Visual Studio Express 2013

Some more Info, I'm very VERY new to this. I've done a lot of searching on the web and nothing seems to be fully fitting my usage case to the point that it's fixed it. In response to a question, here's a more complete code snippet:

private void txtBoxLastName_Leave(object sender, System.EventArgs e)
    {
        string FirstName = txtBoxFirstName.Text;
        string LastName = txtBoxLastName.Text;
        txtBoxEmail.Text = FirstName + "." + LastName + "@lgfcu.org";
    }

This shows that they're in the txtBoxLastName_Leave.

4

There are 4 answers

4
MikeH On BEST ANSWER

I'm going to take a stab in the dark and assume that you didn't actually wire the event to the TextBox. Easiest way to create an event: Right click on your TextBox, click properties, select the "lightning symbol" toward the top, find the event you desire ("leave" in this case) and double click the column to the right of "Leave". This will wire the event (look in your designer.cs file for a +=) and create the method for you.

Alternatively, if you want to wire the event manually add this code (after InitializeComponent()):

txtBoxLastName.Leave += txtBoxLastName_Leave;
3
Jarek PiĆ³rkowski On

The textboxes' properties are title-cased, so you need to capitalize the first letters:

string FirstName = txtBoxFirstName.Text;
string LastName = txtBoxLastName.Text;

or

txtBoxEmail.Text = txtBoxFirstName.Text + "." + txtBoxLastName.Text + "@organization.org";
0
Llama11amaduck On

@MikeH got it; I had messed up and not been calling my function to populate the Email field. My code was fine,

    private void populateEmail(object sender, System.EventArgs e)
    {
        string FirstName = txtBoxFirstName.Text;
        string LastName = txtBoxLastName.Text;
        txtBoxEmail.Text = FirstName + "." + LastName + "@organization.org";
    }

but I had to tie it to an event. I set it such that when the text in FirstName or LastName are edited, it populates the email field which prevents the issue of it not generating correctly if someone goes back into a name field to modify it after clicking in the email box.

0
vipul kumar On

We have take string value and string value pass to TextBox

String str = "Welcome";
textBox.Text = str;