Building LoginID from multiple textBox Strings


I have a C# windows form built.  I want to get input from the user and build a loginID from that input.  So for example.  I have 3 textboxes.  the first is for input of the first name.  The second is for input of the last name.  The third textbox should display the last three chars of the lastname combined with the first 2 chars of the first name.

I want this to happen dynamically as the user types in the name.  I understand how to break the string apart into chars but I am unclear where to put this code so it updates the third textbox as the user types the data in.

Thanks for your help
Who is Participating?
I am calling the first text box txtFirst and the 2nd txt box txtLast and the third textbox txtUID.

Set the TextChanged event for txtFirst AND txtLast to this method:
            private void txtbox_TextChanged(object sender, System.EventArgs e)
                  string firstpre = "";
                  string lastpost = "";
                  string firsttxt = txtFirst.Text.Trim();
                  if(firsttxt.Length < 3){firstpre = firsttxt;}
                  else{firstpre = firsttxt.Substring(0,2);}
                  string lasttxt = txtLast.Text.Trim();
                  if(lasttxt.Length < 4){lastpost = lasttxt;}
                  else{lastpost = lasttxt.Substring(lasttxt.Length - 4);}
                  txtUID.Text = firstpre + lastpost;

You will need to add a TextChanged listener for the first two textboxes (i.e. first name and last name). Have both call the same method GenID (for example). Put the code in GenID to set the text of the third textbox to the logic you have defined.
darkstaggAuthor Commented:
This is where I am having the problem.  I am unclear on the TextChanged listener.  Is it a seperate method and how do I activate it.  Is there an example somewhere. Im sorry to be so confused.  I am a little new at C#.

this.textbox1.TextChanged += new System.EventHandler(this.TextBox1or2_TextChanged);
this.textbox2.TextChanged += new System.EventHandler(this.TextBox1or2_TextChanged);

private void TextBox1or2_TextChanged(object sender, System.EventArgs e)
//Here comes your code to update textbox3      something like below
//textbox3.Text = textbox1.Text.Substring(0,3) + textbox2.Text.Substring(0,5)

darkstaggAuthor Commented:
Very cool thanks.  Thats where I was going wrong on the textbox part.  Now I do have it accurately reacting on change.  It looks like I havent quite figured out the character input part yet. But I will give it a try before asking for more from you.

Sincere thanks I really appreciate it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.