[英]Trying to Fill a text box with strings from other textboxes in C#
I'm trying to create a simple AD Gui for our HR department to use but I'm hung up on something simple. 我正在尝试为我们的人事部门创建一个简单的AD Gui,但我一直挂在简单的东西上。 Our email address format is firstname.lastname@organization.org. 我们的电子邮件地址格式为firstname.lastname@organization.org。 I have a text box for first name and for last name and I want the text box for the email to autofill with firstname.lastname@organization.org. 我有一个用于名字和姓氏的文本框,并且我希望电子邮件的文本框自动填充firstname.lastname@organization.org。 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 我在Visual Studio Express 2013的Windows窗体应用程序中进行此操作
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. 这表明它们位于txtBoxLastName_Leave中。
I'm going to take a stab in the dark and assume that you didn't actually wire the event to the TextBox. 我将在黑暗中刺痛一下,并假设您实际上并未将事件连接到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. 这将关联事件(在designer.cs文件中查找+=
)并为您创建方法。
Alternatively, if you want to wire the event manually add this code (after InitializeComponent()
): 另外,如果您想手动连接事件,请添加此代码(在InitializeComponent()
):
txtBoxLastName.Leave += txtBoxLastName_Leave;
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";
@MikeH got it; @MikeH明白了; 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. 我将其设置为在编辑FirstName或LastName中的文本时填充了电子邮件字段,这可以防止如果有人在单击电子邮件框后返回到名称字段进行修改时无法正确生成该问题。
We have take string value and string value pass to TextBox 我们将字符串值和字符串值传递给TextBox
String str = "Welcome";
textBox.Text = str;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.