1

I am currently making a windows form application on Visual studio in C#. I have a couple of text boxes where I want the user to input some stuff and then this information is checked whether it exists, if not, an error is thrown and a text box saying "Invalid File" is meant to appear, in red. However, currently, when I enable it's visibility, it simply shows up as a blank box, with no colour and no formatting. Here is the code I was using:

catch
{
  textBox9.Visible = true;
  System.Threading.Thread.Sleep(3000);
  textBox9.Visible = false;
}

This is what I want it to look like
This is what it does look like

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • `textBox9.BackColor = System.Drawing.Color.Red;` – Reza Aghaei Jan 14 '19 at 16:53
  • 1) It's not a good idea to use Exceptions to drive logic and 2) You probably don't want to lock up your UI thread for 3 seconds, so you probably don't want to sleep either. – Broots Waymb Jan 14 '19 at 16:54
  • The text box is already set to red to begin with, I added what you said with the visibility code to no avail. – Dom Beasley Jan 14 '19 at 16:55
  • I mean, thanks for the tips but it doesn't really help me with the problem – Dom Beasley Jan 14 '19 at 16:58
  • If you hide anything it will disappear. That is the goal of 'visible = false'. – Black Frog Jan 14 '19 at 16:58
  • where do you set the red color and the text? – Mario Mitterbauer Jan 14 '19 at 17:00
  • Just in the windows form at the start, the text box is just red to begin with – Dom Beasley Jan 14 '19 at 17:05
  • @DomBeasley when you debug your code and set the breakpoint at both texbox9.Visible, where does the formatting change? before the `Sleep()`method or afterwards? – Mario Mitterbauer Jan 14 '19 at 17:23
  • 1
    If you are trying to update the control on the UI thread - then you are blocking that thread with the Sleep. The control will only be updated after you exit this event - by then you have hidden the control again. It is a similar problem to updating a progressbar on the UI thread - information here: https://stackoverflow.com/questions/30905220/how-to-update-progress-bar-while-working-in-the-ui-thread There is an extension method to refresh a control here : https://social.msdn.microsoft.com/Forums/vstudio/en-US/878ea631-c76e-49e8-9e25-81e76a3c4fe3/refresh-the-gui-in-a-wpf-application?forum=wpf – PaulF Jan 14 '19 at 17:35

3 Answers3

0

The only thing it happens is the txtbox be visible, so the only code executed is the code inside catch...

Try to set all the properties in the catch, something like that:

For sure all gonna be executed now.

catch
{
  textBox9.Text = "Invalid File";
  textBox9.BackColor = Color.Red;
  textBox9.Visible = true;
  Thread.Sleep(3000);
  textBox9.Visible = false;
}

Edit:

I saw the comment, and that's right Thread will block all the code for 3 seconds. So I've other option, something like that:

catch
{
  textBox9.Text = "Invalid File";
  textBox9.BackColor = Color.Red;
  textBox9.Visible = true;
  int seconds = 3;
        if (seconds < 1) return;
        DateTime _desired = DateTime.Now.AddSeconds(seconds);
        while (DateTime.Now < _desired)
        {
             System.Windows.Forms.Application.DoEvents();
        }
  textBox9.Visible = false;
}
Mikev
  • 2,012
  • 1
  • 15
  • 27
  • Your Sleep call (like the original question) is running on the GUI thread, so it will block everything for 3 seconds. – LarsTech Jan 14 '19 at 17:45
-1

If i understand correctly you are trying to make the textbox work over 3 seconds and then go away, if that is so the code you need would look like this

Task.Run(async () => 
  this.Invoke(new Action(delegate (){
    textBox9.Visible = true;
    await Task.Delay(3000)
    textBox9.Visible = false;
}));

EDIT: This code is needed because you dont wanna hang the whole thread just wait 3 seconds and then make it go away, the way you are doing it, you are freezing the whole application if you are not using threads

EDIT2: It isnt showing anything because you are freezing the thread before it draws on your screen and then you are setting the textbox hidden. So nothing will show

nalnpir
  • 1,167
  • 6
  • 14
  • This is great code, but you didn't answer is question of why the formatting disappear when visibility is set to FALSE. – Black Frog Jan 14 '19 at 17:01
  • @BlackFrog i assumed due to the pictures that the problem was nothing showing at all. And that is because he is freezing the interface by using Thread.sleep. And then it ends and its disabled. I will add that its because of the visibility of the post anyway. Thanks – nalnpir Jan 14 '19 at 17:03
  • The 'await' operator can only be used within an async anonymous method. Consider marking this anonymous method with the 'async' modifier. – Dom Beasley Jan 14 '19 at 17:07
  • check now please, if it doesnt work i ll need more time to revise the code – nalnpir Jan 14 '19 at 17:21
  • Messed around a bit with it, I'll post the working one – Dom Beasley Jan 14 '19 at 17:22
  • Task.Run will run the code on a different thread. You mustn't access GUI controls from other threads than the form was created on. – NineBerry Jan 14 '19 at 17:26
  • @NineBerry Thats the reason why the code is using the invoke – nalnpir Jan 14 '19 at 17:28
-1
        private void DisplayError()
    {
        Task.Run(async () => (
                 this.Invoke(new Action(async delegate () {
                     textBox9.Visible = true;
                     await Task.Delay(3000);
                     textBox9.Visible = false;
                 }))));
    }

Thanks to nalnpir for the basis of this. This works for me.

  • Provided what you are showing I have 2 observations for it. First, the async of the Task.Run is not needed. Second you should edit the answer in your question for easier future references and delete this one. im glad you were able to come by the answer – nalnpir Jan 14 '19 at 17:27
  • @nalnpir If this is an answer, why should he delete it? – LarsTech Jan 14 '19 at 17:28
  • Isnt it common when the OP solves the question to edit the post and put it on the original question? – nalnpir Jan 14 '19 at 17:30
  • @nalnpir, No, not on StackOverflow. Questions should remain questions, answers should be answers. Keep them separate. – LarsTech Jan 14 '19 at 17:31
  • @nalnpir I don't have much idea what I'm doing for this, it works so I'm fine with it. I'm grateful for your help – Dom Beasley Jan 14 '19 at 20:46