Clean and reset a form without leaving it with C# - c#

Plop,
I'm currently working on a Hangman game with C#. I use a main form to the menu, then I start another form within the first form in that way :
Form2 game = new Form2();
game.ShowDialog();
So when I finish a game, I don't want to close it form, but I want to reload the whole form. Here is the way I do :
private void Replay(object sender, EventArgs e)
{
this.Controls.Clear();
this.InitializeComponent();
Form2_Load(this, null);
}
This code removes all Controls then recreate them with InitializeComponent. I thought removing controls would clean the screen but it doesn't. Some labels and PictureBox that are removed still stay displayed.
Where am I doing this wrong ?

Related

Preloading a form in WPF c#

I am currently trying to make a Loading form which should stay on screen for 7 seconds before switching to the main form
I cant just simply load the main form directly after the loading bar is full because the Main form takes about 7 seconds to start
Is there i way to "pre load" the main form, but hiding it until the loading form is done?
This is the code for the loading form, simplyfied
private void FormLoading_Load(object sender, EventArgs e)
{
tick.Start();
}
private void tick_Tick(object sender, EventArgs e)
{
loadingbar.Increment(1);
if (loadingbar.Value == 700)
{
this.Close();
}
}
I would add a Splashscreen - this way you do not need to use a timer for waiting until the form is loaded. You simply display the splash screen at startup and close it inside the MainWindows Constructor.
You can add a splash screen by Visual Studio -> Right click your project -> Add new item -> Wpf -> Splashscreen.
If you really want that the form does display there for 7 seconds you can use Thread.Sleep before closing the Splashscreen.

Detect CloseEvent through different forms

PROBLEM SOLVED
SHORT STORY
I want to detect "FormClosing()" event through different forms, ie, when form1 is closed that is instantiated within form2, can form2 detect when user presses exit in form1?
LONG STORY
My team and I are working on a windows form application. Project has two forms: one is the main form page and the other is accessed via this main form. Main form looks like this:
And the second one looks like this:
If you press "Ekle/Sil" buttons within the main form, you are directed to form 2 where you can edit database entries. When you press "Sayfayı Yenile" button in the main form, the content of the text areas are refreshed by re-fetching entries from the database.
My problem is, I want to automatically refresh the main form when the user closes the second form. My research suggests I should use an "FormClosing()" event to detect a closing form. However, I want to detect this from the main form. Instantiating main form in second form's source code doesn't seem to be a reliable solution. Anyone can tell me how to do this?
EDIT
I solved the problem:
1) Created a public method within the main form that refreshes the page.
2) Send "this" property from the main form when creating the second form.
3) Added an "FormClosed()" handler within the second form that invokes this public method.
Still, I'm looking for a better solution.
EDIT 2
Better solution InBetween's answer
Simply use the Form.Closed event of the new child windows form. Everything is handled from the main form:
void EkleSil_Clicked(object sender, EventArgs e) //or whatever method is called when button is clicked
{
var newChildForm = new ChildForm();
newChildForm.Closed += childFormClosed;
newChildForm.Show();
}
void childFormClosed(object sender, EventArgs e)
{
((Form)sender).Closed -=childFormClosed;
updateShownData();
}
You can create a event in the second form and raise it when the form is closing .
Handle the event in the main form and refresh the main form when the event is raised
Another option would be to pass Form1 as an argument to Form2. Then use the Form.Closing event in Form2 and use the Form1 reference to trigger something.
Form1 form1Ref;
public Form2(Form1 mainform)
{
form1Ref = mainform;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.SomeMethod();
}

Call a new form from current form in c#

I want to create a program which have the same theme and looks like new forms are loading inside the current form from c#. I can do this by using panels but I do not wish to do so since if I had to do a modification it becomes a cumbersome.Instead of using panels I used forms. when i click a button in a form same style form with different values will appear on the current form. But it's looking so untidy and when i want to close the program I have to close every form. Since my final program will contain more than 15 guis it is not a practicle solution .so tried by using following code snippet.
private void ord_Click(object sender, EventArgs e)
{
this.Close();
order frm =new order();
frm.Show();
}
From this code I suppose to close the current form and display the next form. But this code didn't work .It closed both forms.
I can use this.hide() method instead of this.close .but its only hide the current form.When I want to close the Program I have to close each and every form.
I don't wish to use MDI forms. Because it is not suit for my theme. you can get idea about my theme by looking at following image.
GUI of the system
And I want to design a professional looking GUI. I also like to have comments about my GUI. Is it looking professional?
try this
private void ord_Click(object sender, EventArgs e)
{
order frm =new order();
frm.Show();
this.Hide();
}
if its still not working ?
then probably you are using forms in parent child fashion if you close the form which is running at the very start all the forms will be closed, Solution: you should hide child form using .Hide() and for the next form to open declare the parent form as mdi parent of child form hope you understand...

Different form will not load

I'm using c# to make a mobile 6 application. I created another windows form in the project. This is the form that I would like to load first. This is what have tried:
MainMenu gameMenu = new MainMenu();
private void MainForm_Load(object sender, EventArgs e)
{
this.Hide();
gameMenu.ShowDialog();
.....
}
When I run this the emulator comes up but it just stays as the default windows screen. And I don't get any of my forms.
GameMenu's parent is MainForm, which is now hidden, so the Dialog isn't going to be visible. You need to adjust your logic to do one of the following:
show the GameMenu first (i.e. Application.Run(new GameMenu))
Don't hide MainForm
Use gameMenu.Show() instead of ShowDialog()
You may need to get rid of this.Hide() or use gameMenu.Show() instead of gameMenu.ShowDialog() or you may need to do both.
If you have to use gameMenu.Show() instead of gameMenu.ShowDialog(), you may also want to do the following:
Subscribe to MainForm's GotFocus event and call gameMenu.Show() again whenever the other form gains focus unintentionally. Set MainForm's Enabled property to false while the gameMenu is shown if you want to prevent any accidental interaction with the MainForm while the gameMenu is supposed to be shown.

C# Form Problem: new form losing control and randomly hiding

I'm encountering strange behavior with forms on a c# 3.5 app. On a button click, my form1 hides itself, creates a new form2, and shows form2. Form1 also contains the event method triggered when form2 closes. Here's the code inside Form1:
Form2 form2;
void button1_Click(object sender, EventArgs e)
{
this.Hide();
form2 = new form2();
form2.Show();
form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
}
void form2_FormClosed(object sender, FormClosedEventArgs e)
{
form2.Dispose();
form2 = null;
this.Show();
}
Now, my problem is that sometimes when I open form2 (hiding form1), or when I close form2 (showing form1), the new form will come up on the screen for a blink and then hide itself. It's still open and I can click it from the taskbar to show it again, but the window itself is sent behind any other open windows. It looks like it opens up but minimizes instantly.
This behavior occurs randomly. Sometimes forms will open up and hide without a problem, but sometimes they'll lose focus over another window. I've tried using focus(), activate(), and topmost but all have failed to prevent the sudden hiding.
Does anyone know why is this happening and how to fix it?
Thanks.
You hide your form too soon. For a brief moment, your app has no window that can contain the focus. That forces Windows to go hunting for another window to give the focus to, it will pick one from another application. That window will now be the foreground window, your second form will not get the focus and appear lower in the Z-order. The fix is simple:
void button1_Click(object sender, EventArgs e)
{
form2 = new form2();
form2.Show();
form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
this.Hide(); // Moved
}

Categories

Resources