Question How to refresh Form1 from inside a class?

te777

New member
Joined
May 8, 2015
Messages
2
Programming Experience
5-10
I have a class that uses a dialog for user input. After the input, and before the following procedure to use the input, Form1 should refresh, but it doesn't. I have a garbled display for Form1. How can I refresh Form1 just after closing the dialog object?

 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,136
Location
Sydney, Australia
Programming Experience
10+
There is no inherent function of a form that refreshes it, unless you're talking about redrawing it on-screen, which you're not. If you want the form to get new data and display it then you have to write code in the form to get the data and display it. There's nothing magic about it. However you displayed the data in the first place is the very same way you you display new data a second time.

If you want to initiate that in the form then you do it exactly the same way you always initiate something in an object, i.e. you handle an event of another object or another object calls a method. Does your other object have a reference to this form? If so then you can use the second option, otherwise you would use the first option.

If you're still confused, maybe you should show us some relevant code, mainly how each of these inter-related objects is created and uses the others.
 

te777

New member
Joined
May 8, 2015
Messages
2
Programming Experience
5-10
I fixed it. I passed the form object to the class in the constructor, assigned a class Form variable to that passed parameter in the constructor body, and then did a classFormVariable.Refresh() in the class where I needed to. Thanks for the reply.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,136
Location
Sydney, Australia
Programming Experience
10+
Oh, it seems that you actually did mean redrawing the form on-screen. I'm so used to people using the term "refresh" to mean reload data or the like that I assumed that you did too.

I would tend to look at why this is happening in the first place and try to avoid that rather than fixing it after the fact. It may be that calling Refresh on the form is the best option but that's usually not the case. My guess is that you're doing something on the UI thread that you shouldn't be, so I'd look at that first. Prevention is almost always better than cure.
 
Top Bottom