Building LoginID from multiple textBox Strings

Posted on 2005-05-04
Last Modified: 2011-09-20

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
Question by:darkstagg
    LVL 3

    Assisted Solution

    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.

    Author Comment

    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#.

    LVL 8

    Assisted Solution

    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)

    LVL 2

    Accepted Solution

    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;


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now