Set text box text based on character list
So, I have the following form:
What I am trying to achieve is the following:
After entering the name, I want the initials of the name to be generated by simply clicking the initials text box.
I found the following method to get the first characters of a string:
string EngineerName = tb_Name.Text.ToString();
EngineerName.Split(' ').ToList().ForEach(i => Console.Write(i[0] + " "));
My question is, how would I assign a list of characters to the text textbox
?
I tried:
private void Tb_Initials_Click(object sender, EventArgs e)
{
string EngineerName = tb_Name.Text.ToString();
EngineerName.Split(' ').ToList().ForEach(i => Console.Write(i[0] + " "));
tb_Initials.Text = EngineerName;
}
But this just populates the textbox with the same name ...
+3
Kahn Kah
source
to share
3 answers
You need to assign the split result somewhere. Consider this piece of code:
string EngineerName = tb_Name.Text.ToString();
string initials = "";
EngineerName.Split(' ').ToList().ForEach(i => initials += i[0] + " ");
tb_Initials.Text = initials;
+5
Michael
source
to share
You can do something like this:
private void Tb_Initials_Click(object sender, EventArgs e)
{
tb_Initials.Text = "";
string EngineerName = tb_Name.Text;
string[] splitted = EngineerName.Split(' ');
for(int i = 0; i<splitted.Length; i++)
tb_Initials.Text += splitted[i];
}
+2
João silva
source
to share
You need to assign it directly:
string EngineerName = tb_Name.Text.ToString();
var initials = "";
EngineerName.Split(' ').ToList().ForEach(i => initials += i[0] + " ");
//trim the extra space in the end.
initials = initials.TrimEnd();
tb_Initials.Text = initials;
0
RBT
source
to share