I started a game project in c# and i ran in to a problem .
In the game there is a map form that has buttons , when you click on a button a new form is opend- field.
My problem is that i dont know how to close the form(field) activity after i finish with it.
this.close() doesnt to the job and the form continue to work even if i cant see it.
private void Charmove_Load(object sender, EventArgs e)
{
.
.
Messagebox.show("you won the stage well done");
this.close();//problem
g.show;
}
Does your game use a Timer? If so, then stop it before closing the form.
private void Charmove_Load(object sender, EventArgs e)
{
...
timer1.Stop();
Messagebox.Show("You've won the stage. Well done!");
this.Close();
g.Show();
}
Related
So, I'm making a payroll management system as a hobby project to help my resume and general knowledge of c#. So, I'm making a UI and I can open a new window just fine with this code:
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
this.Hide();
}
however, I don't know the event to check when the little red "x" button is clicked, because when that button is clicked, I want to go back to the main screen because I hide the main screen when that button is clicked, and when i click the red "x" on the screen that just opened, it closes, but the application continues to run in the background.
If there is some better way to manage multiple menus, I'm open to suggestions, however, this is what I've found easiest.
Thanks in advance
I second Robert Harvey's suggestion; this gives the user the reassurance tha tht emain window is still open/ nothing got lost, but it's unreachably "behind" the CreateAdminAcct form while the CreateAdminAcct form is open
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.ShowDialog();
//do any code here that needs to access createAcct before it's lost
MessageBox.Show(createAcct.NewAdmin.Name);
}
If you really do want to hide your main form, pass the main form itself to createAcct, and make it createAcct's job to re-open the main form when it is closing
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct(this); //note passing this form to constructor
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
}
class CreateAcctForm : Form{
private Form _showWhenClosing;
CreateAcctForm(Form revertTo){
InitializeComponent();
_showWhenClosing = revertTo;
}
}
void Form_Closing(object sender, ...){ //event
_showWhenClosing.Show();
}
Side note: please rename your controls after you drop them ona form. code that's stuffed with label57, textbox25 is effectively obfuscated and really wearisome to follow
I have this syntax on my buton press event, but when I press it - the form does not close.
What is the proper way to close the form on the button press event?
private void btnClose_Click(object sender, EventArgs e)
{
IxalocToes nip = new IxalocToes();
nip.Close();
}
This method btnClose_Click runs inside your forms class
Forms have a method call Close, calling Close() or this.Close() inside the form will close it
private void btnClose_Click(object sender, EventArgs e)
{
IxalocToes nip = new IxalocToes();
nip.Close();
Close();
}
As suggested by many and it is right, calling
this.Close() or
Close()
will close the form. As you want to know why nip.Close() is not working, it's because the button is in a FORM but when you call nip.Close() instead of this.Close(), it will close the new object created, not the one on which the button resides.
The issue doing so is i have used tips and tricks here but the seems to run in a loop and wont give the results.
Basically i run a BackgroundWorker to hit a url get result and paste result to some Labels.
I have used Form_Activated but it just keeps on running in a loop and wont stop ever reached to the BackgroundWorker completed event .
MAIN CODE BLOCKS:
On Form_Load I Run the Function and get the results and show:
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Show();
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
else
{
MessageBox.Show("Thread already running....");
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
loadData(); // scrape a URL and paste info to Labels ....
}
This is it, now the user will minimize the application , now whenever he hits the the taskbar icon the form should rerun the same as in Form_Load. I hope that make sense , i have been able to do that using Form_Activate but it keeps going on .
Any suggestion how to get it done ?
I would store a boolean to remember if the form was minimized at the last FormResized event, and then if it was and if the form isn't currently minimized then call your method.
private bool minimized = false;
public void FormResized(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
minimized = true;
}
if (minimized && this.WindowState != FormWindowState.Minimized)
{
minimized = false;
MyMethod();
}
}
I've been stuck here for a few hours looking up code for hours and making batches of my own. One of the code ex I got from here gave me a instant svHost error.
Now my splash screen is my HWID / Serial check, if valid it opens the main form if not it closes the app completely.. But the issue is I've tried some way's with splash.show(); on the main form but it just freezes and goes all stupid for a few minutes, another thing most of the methods use timers I just need it so when HWID check is valid I can start the main form, I've tried application.Run(new mainForm()); if the HWID was correct then changed the program.cs file around but still no luck, I really need the help. It would be wonderful, thank's.
I've used splash screen animation while I'm connecting to socket. I've used backgroundworker for it.
Here's my code:
Main Form
LoadingScreen frmLoadingScreen = new LoadingScreen();
.....
bkwNetworkConnector.RunWorkerAsync();
frmLoadingScreen.ShowDialog();
/***********************************************************************************************************************/
private void bkwNetworkConnector_DoWork(object sender, DoWorkEventArgs e)
{
try
{
hostSocket = new TcpClient();
hostSocket.Connect(strIp, intPort);
}
catch (Exception exp)
{
}
}
private void bkwNetworkConnector_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
frmLoadingScreen.Close();
}
LoadingScreen Form:
with imagebox control. Load GIF image in it.
private void LoadingScreen_Load(object sender, EventArgs e)
{
pbAnimationBox.Image = Properties.Resources.LoadingAnimation; // win 8 animation
}
I'm using LoadingScreen Form as splash screen.
Hope this will hep you....
I have a strange bug, please, let me know if you have any clues about the reason.
I have a Timer (System.Windows.Forms.Timer) on my main form, which fires some updates, which also eventually update the main form UI. Then I have an editor, which is opened from the main form using the ShowDialog() method. On this editor I have a PropertyGrid (System.Windows.Forms.PropertyGrid).
I am unable to reproduce it everytime, but pretty often, when I use dropdowns on that property grid in editor it gets stuck, that is OK/Cancel buttons don't close the form, property grid becomes not usable, Close button in the form header doesn't work.
There are no exceptions in the background, and if I break the process I see that the app is doing some calculations related to the updates I mentioned in the beginning.
What can you recommend? Any ideas are welcome.
What's happening is that the thread timer's Tick method doesn't execute on a different thread, so it's locking everything else until it's done. I made a test winforms app that had a timer and 2 buttons on it whose events did this:
private void timer1_Tick(object sender, EventArgs e)
{
Thread.Sleep(6000);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
frmShow show = new frmShow();
show.ShowDialog(); // frmShow just has some controls on it to fiddle with
}
and indeed it blocked as you described. The following solved it:
private void timer1_Tick(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(DoStuff);
}
private void DoStuff(object something)
{
Thread.Sleep(6000);
}