Problems with refreshing a form - c#

I have a form with labels A, B and C. In static void Main(), I say:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form1 = new Form1();
Application.Run(form1);
Form1 constructor calls
InitializeComponent();
updateForm();
where updateForm reads a log ((FtpWebRequest)WebRequest) from a remote machine and updates the labels appropriately.
I would like to constantly update the form, because the remote log file is changing every few minutes.
I tried calling form1.updateLog() in while(true) loop in the main method, but the form doesn't refresh. Also, form1.Refresh() doesn't seem to work.
Any hints?
Thanks.

When you're doing that in a loop then most likely on the UI thread. This means that you constantly tell the Form to repaint yet don't give it time or an opportunity to do so (remember: you're blocking the thread on which it would do that).
Instead use a timer and refresh from there, e.g. every 100 ms or so.

Related

Form Navigation in Win CE 5 Application

I'm Trying to develop an app for an old handheld device running .Net 3.5 Win CE 5 however I cannot seem to get over a basic hurdle switching forms!!
I started using
Application.Run(new frm_class());
but this started crashing after opening any more then 2 forms.
I then tried.
this.hide();
frm_class frm = new frm_class();
frm.show();
then just ended up being a constant loop of loading the form
then I seen someone wrote a very simple class to handle this.
public static void switchForm(Form newForm, Form oldform)
{
newForm.Show();
oldform.Hide();
oldform.Dispose();
}
call via
frm_class frm = new frm_class();
switchform(frm,this);
which shuts the app down when loading the second form.
This seems so stupid but I cannot find out a way of just navigating through simple forms ie close this form then open this one or vice versa!!
can anyone help?
Application.Run(new frm_class()); will start a message loop and makes the specified form visible and waits untill it is closed. Once the specified form closes, the message loop terminates.
oldform.Dispose(); will dispose (and close) the form, not hide it.
Your second solution seems like the one you should take, which shouldn't loop. Make sure the code isn't in the constructor or form_load event if you're creating the same form again. Can you post the code surrounding this?
You could also remove the new frm_class() argument from Application.Run(). Which will create a constant running message loop. Just don't forget to call Application.Exit() at some point.
You should make sure the form is made visible before the parameterless Application.Run() because the execution inside this codeblock wont continue untill Application.Exit() is called from inside your form.
frm_class frm = new frm_class();
frm.Show();
Application.Run();
// Some button or event inside the form should be executing the exit call:
Application.Exit();

C# unload and load form / hide and close

I am coding a simple sidescroller in C# using Windows Form Applications. I want it so when the player touches an exit point, this immediately loads a new level.
To implement this, I use the following code:
if (player.Bounds.IntersectsWith(exit.Bounds))
{
Form2 myNewForm = new Form2();
myNewForm.Visible = true;
this.Hide();
}
This works. However, it loads numerous instances of form2 - I only want it to load once. I don't know how to write this (sorry, I'm a newbie - it took me a while just to write this code!).
Also, loading a level via a new form is inefficient. Is there a way to unload the open form to load the next one in the same window/instance, rather than creating another separate window?
Sorry if this is unclear. I've done my best research + I'm new. Please don't mention XNA! Thanks.
You need a small modification to your project's Program.cs file to change the way your app decides to terminate. You simple exit when there no more windows left. Make it look like this:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var start = new Form1();
start.FormClosed += WindowClosed;
start.Show();
Application.Run();
}
static void WindowClosed(object sender, FormClosedEventArgs e) {
if (Application.OpenForms.Count == 0) Application.Exit();
else Application.OpenForms[0].FormClosed += WindowClosed;
}
}
Now it is simple:
if (player.Bounds.IntersectsWith(exit.Bounds))
{
new Form2().Show();
this.Close();
}
You can use Application.OpenForms[] collection to retrieve the Opened form instance and then Show it.
Try This:
Form2 frmMyForm = (Form2)Application.OpenForms["formName"];
frmMyForm.Show();
The actual problem is not in your form being loaded multiple times, but in your game logic not being suspended when the end of the level is reached. It means that your game keeps playing an old level when a new level is already loaded.
If Form1 is the main form then your whole application will shutdown. You need bootstraper which will be the entry point of your application, not the Form1. If you do that your Form1 will be a child in the same sense as will be Form2. You can open and close them without shutting down the application. If you don't know how to do that, just create another empty form, let's call it Main and make it the starting form of your application. Then hide it and open Form1 from Main form as Modal. Then when you complete level in Form1, close Form1, the code flow will return to Main form and you'll spawn Form2 from Main form as Modal. You'll have fully predictable logic, where all forms are opened from a single controlled place.

Multithreading multiple forms

I have one main form that starts on program in program.cs as Application.Run(new Form1());. It opens object of 2 different forms as:
Form2 obj1 = new Form2();
obj2.Show();
Form3 obj2 = new Form3();
obj3.Show();
Each form has Thread.Sleep(10000) code but the problem is that on one's Thread.Sleep execution, whole programs sleeps. I thought if one form's Thread.Sleep runs, it will not stop other form and will not affect those.
All GUI components run on a single GUI thread therefore if you sleep that thread everything running on that thread sleeps, which in your case means all of your forms.
If you need a particular form to wait for something to happen then there are various ways of doing that, however, you need to be a bit more clear in your question as to what exactly it is you are waiting for.

Refreshing the form window

I'm making a small simple windows application. This is my main function:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// This will be my Form object
Form1 robotPath = new Form1();
Application.Run(robotPath);
// at this point I'll try to make changes to my object
// for instance I'll try to change a background image
robotPath.changeImage();
}
However after changing my object, the changes are not reflected in the output window (the background is not changed). I've tried robotPath.refresh() and robotPath.invalidate() but still the background is not changing. However, when I call the changeImage function using a button click event it works. But I want it to be changed without using a button/mouse event.(Background changes as the Form1 object is changed)
Any suggestion?
Application.Run()
does not return until the main form is closed. All code that runs after Application.Run() does not run until the program is shutting down. That's clearly not what you want.
You can solve the problem easily enough by reordering your main:
Form1 robotPath = new Form1();
robotPath.changeImage();
Application.Run(robotPath);
An alternative would be to move the call to changeImage into the constructor of Form1, or some event that fires early in the form's life, e.g. Load. This option better encapsulates the behaviour of the form.

how to open new window on form close?

how to open form 1 on close button click X in form 2
i tried this but it is not working:
private void supplierShow_FormClosing(object sender, EventArgs e)
{
new suppliersList().Show();
}
Thank you.
Not fully sure without knowing more your code but to me it looks like you are declaring a local variable (new suppliersList of base class Form probably) and showing the form from the Closing event of another form.
Your Form2 object is probably going to be deleted/disposed/garbage collected soon and at that point I am not completely sure that the form declared within that scope would still have a nice life.
If Form2 is the application form, the application will actually terminate when you close it.
in general I think that this kind of form switching is best done and controlled from the main method, the same place where you probably have Application.Run(new Form2()); because in there you have full control of the application flow and MessageLoops...
I have to solutions:
First:
You may have a unique reference to your suppliersList in Program class:
public static suppliersList SuppList = new suppliersList();
you can now hide it if you want:
Program.SuppList.Hide();
you can show it anytime, too:
Program.SuppList.Show();
Be carefull! Don't Dispose SuppList. If you did, assign to it a new object new suppliersList();. Please note that this solution would success only if form1 is not the main form (i.e. Application.Run(new form1()); )
Second:
You can start form2 as a child of window of form1:
new suppliersList().Show(this);
because, as in the other answer, the application loops are on form1. If form1 is closed, then entire application will be closed too. However, starting a child window of the main window will prevent application close.
If the answer is useful for you, please mark it as your best answer.

Categories