简体   繁体   English

C#文本框保持为空

[英]C# Text box remaining empty

I'm calling a public method from another class. 我正在从另一个类调用公共方法。 It takes in a List as a parameter, and goes through the list printing out each item into a text field. 它接受一个List作为参数,然后遍历该列表,将每个项目打印到一个文本字段中。 The problem is the text field is remaining empty!. 问题是文本字段保持为空! I've checked that the list is populated by outputing the item to the console before I put it into the text box, and the text is coming up fine there. 在将其放入文本框之前,我已经通过将项目输出到控制台来检查是否已填充列表,并且在那里的文本很好。

The list contains strings, and should output each string to the textfield followed by a semi colon. 该列表包含字符串,并且应将每个字符串输出到文本字段,后跟一个半冒号。

This is the method which is being called: 这是被称为的方法:

public void fillAttachment(List<string> attachList)
{
  for (int i = 0; i < attachList.Count; i++)
  {
    Console.WriteLine("List: " + attachList[i]);
    txtAttach.Text += attachList[i] + ";";
  }
}

I would solve it in this way: 我会这样解决:

foreach(var attach in attachList)
{
  Console.WriteLine(attach);
  txtAttach.AppendText(string.Format("{0};", attach));
}

Setting the text property on a text box and it not displaying could be one of the following: 在文本框上设置text属性,使其不显示可能是以下之一:

  • You are not looking at the same control as you are setting the text in 您没有在设置文本时使用相同的控件

Could you have instantiated a second copy of the form object and it is this form that you are setting the txtAttach text property in? 您是否可以实例化表单对象的第二个副本,并且要在其中设置txtAttach文本属性的是此表单?

Could the control that you are expecting to be populated be a different one? 您希望填充的控件可以是其他控件吗? Right click the text box that you want the text to appear in click properties and check the name. 右键单击要使文本显示在单击属性中的文本框,然后检查名称。

  • Something else is clearing the textbox after you set it 设置文本框后,其他东西正在清除

Right click the txtAttach.Text and click Find All References, this will show you all the places that the Text property is referenced - written and read - in your project. 右键单击txtAttach.Text,然后单击“查找所有引用”,这将向您显示在项目中引用Text属性的所有位置(写入和读取)。 This is a very useful way to locate other interaction with this control. 这是查找与此控件进行其他交互的非常有用的方法。

  • Fomatting is making the text box appear empty Fomatting使文本框显示为空

Is the Font too small, or in the same colour as the background. 字体是否太小或与背景颜色相同。 Can you select the text in the text box? 您可以在文本框中选择文本吗?

The easiest way to test all of the above is to create a new text control on your form with a different name, change your code to populate it, check that it is indeed populated, then replace the old one. 测试以上所有内容的最简单方法是在表单上使用不同的名称创建一个新的文本控件,更改代码以填充它,检查它是否确实填充,然后替换旧的。

As an aside, you could also reduce the code with a single line as follows: 顺便说一句,您也可以用以下一行代码减少代码:

public void fillAttachment(List<string> attachList)
{
    txtAttach.Text = String.Join(";", attachList.ToArray());
}

Although this obviously skips out the console write line function. 尽管这显然会跳过控制台写入行功能。

Not sure why yours doesn't work but I would have done it like this... 不知道为什么你的不起作用,但是我会这样做的...

public void fillAttachment(List<string> attachList)
{
    string result = "";
    //OR (if you want to append to existing textbox data)
    //string result = txtAttach.Text;

    for (int i = 0; i < attachList.Count; i++)
    {
        Console.WriteLine("List: " + attachList[i]);
        result += attachList[i] + ";";
    }

    txtAttach.Text = result;
}

Does that work for you? 那对你有用吗? If not then there is something else very wrong that is not obvious from your code 如果不是这样,那么您的代码中就不会出现明显的其他错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM