1

My Program: I have a textBox and a pictureBox (which contains an error picture, placed exactly next to textBox) in userControl in my program.

My Goal: I want to HIDE the picture in the pictureBox only if the user types text in the textBox. If the textBox is left blank, the image in the pictureBox should be shown.

enter image description here

I tried using errorProvider but I was totally lost because I am a newbie in C# Programming. There are many errorProvider examples online but all examples are using Form and I am trying to do it in UserControl. So, I thought I should try this method. Please can you help me with the code? Thanks for your help in advance.

ANSWER:

Sealz answer works! My program will be working offline. So, this one also works:

if (String.IsNullOrEmpty(textBox1.Text))
        {
            //Show Picture
            pictureBox2.Visible = true;
        }
        else
        {
            //Hide Picture
            pictureBox2.Visible = false;
        }

Thanks everybody for looking at my question! You all are awesome. =)

4

2 回答 2

0

您可以使用IsNullOrEmpty

if (String.IsNullOrEmpty(textBox1.Text))
{
    //Show Picture
    pictureBox1.ImageLocation = "locationofimg";
}
else
{
    //Hide Picture
    pictureBox1.ImageLocation = "";
}

喜欢它。

在 form_Load() 上将图片框设置为空

 private void Form1_Load(object sender, EventArgs e) {
    pictureBox1.ImageLocation = "";
    }

然后在文本框更改方法中

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(textBox1.Text))
    {
         pictureBox1.ImageLocation = "";
    }
    else
    {
        pictureBox1.ImageLocation = "Image\Location.com.etc";
    }
}

这将使该框为空,开始时没有图像,并且在您键入时它会弹出。如果框文本被完全删除,图像将消失。

于 2013-08-26T19:21:56.213 回答
0

只需测试文本框是否有任何文本,并相应地设置属性。

pictureBox1.ImageLocation = (textBox1.Text.Length > 0) ?
    "imagefile" : String.Empty;

如果这需要动态更新,只需在文本框的TextChanged事件中执行此操作。

于 2013-08-26T19:30:20.897 回答