简体   繁体   中英

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

  • 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?

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. This is a very useful way to locate other interaction with this control.

  • Fomatting is making the text box appear empty

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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