How to close a form in UserControl - c#

I created a UserControl with the buttons Save, Close and Cancel. I want to close the form without saving on the Cancel button, prompt a message to save on the Close button and Save without closing on the Save button. Normally, I would have used this.Close() on the Cancel button, but the UserControl doesn't have such an option. So I guess I have to set a property for that.
Scrolling down the "Questions that may already have your answer" section, I came across this question: How to close a ChildWindow from an UserControl button loaded inside it? I used the following C# code:
private void btnCancel_Click(object sender, EventArgs e)
{
ProjectInfo infoScreen = (ProjectInfo)this.Parent;
infoScreen.Close();
}
This does the job for one screen, but I wonder if I have to apply this code for all the screen I have? I think there should be a more efficient way. So my question is: Do I need to apply this code for every form I have, or is there another (more efficient) way?

you can use
((Form)this.TopLevelControl).Close();

you can use the FindForm method available for any control:
private void btnCancel_Click(object sender, EventArgs e)
{
Form tmp = this.FindForm();
tmp.Close();
tmp.Dispose();
}
Do not forget to Dispose the form to release resources.
Hope this helps.

You also can close one form in any part of the code using a remote thread:
MyNamespace.MyForm FormThread = (MyNamespace.MyForm)Application.OpenForms["MyForm"];
FormThread.Close();

I found the simple answer :) I all ready thought of something like that.
To close a WinForm in a ButtonClicked Event inside a UserControl use the following code:
private void btnCancel_Click(object sender, EventArgs e)
{
Form someForm = (Form)this.Parent;
someForm.Close();
}

Related

How to set the focus on a form

I want to set the focus on a popup form I have created when the mouse hover it,
I checked MSDN and they said to use the SetFocuse method but it does not work.
I tried this:
private void POPUPmainmanue_MouseHover(object sender, EventArgs e)
{
POPUPmainmanue.SetFocus();
}
It's strange but I could find no SetFocus() method for a Form on MSDN.
However, using the Activate() methods works well enough.
private void POPUPmainmanue_MouseHover(object sender, EventArgs e)
{
POPUPmainmanue.Activate();
}
Also, always make sure you do not forget to set the EventHandler:
POPUPmainmanue.MouseHover += POPUPmainmanue_MouseHover;
You also did not mention whether you are using WinForms or WPF, although I think it's the former as you say 'form'. Again, what exactly is POPUPmainmanue? A form? I don't think so. A form does not have a SetFocus() method.
I don't think it's easy to implement, cause it's not very clear the question:
It's possible to set focus on textbox when the mouse hovers the form, and another more clear possiblity to set focus on a form when a form opens.
this.BringToFront();
this.Activate();

C# Form Background Parameter

I have created a simple C# application to automatically login into a hotspot. I have a notify icon with a contextmenustrip with some functions like Connect, Disconnect, etc. I want to be able to run the form in the background showing only the notify icon to automatically login into the hotspot if the form is hidden.
I followed the instructions from VBNight in this post:
Hide form at launch
The application is running in the background, the notify icon showing but the Form_Load function is not working until I press on the notify icon.
I guess that's because Form_Load Occurs before a form is DISPLAYED for the first time.
Try moving your code from Form_Load to the constructor after InitializeComponent(); or so.
EDIT:
To answer your question, I suggest you extract code #1 from...
private void YOUR_BUTTON_Click(object sender, EventArgs e) {
// move this code #1 to...
}
and move the code to a brandnew method.
private void NewButtonClicked() {
// move code #1 here (in case)
}
Then, go back and call the method you just created.
private void YOUR_BUTTON_Click(object sender, EventArgs e) {
// You can leave code #1 but to remove duplicate,
NewButtonClicked();
}
Finally, replace YOUR_BUTTON.PerformClick(); with NewButtonClicked(); wherever you need. I assume you don't need any interaction with form Controls since the form is hidden.
I fixed it changing the form opacity to 0 and the ShowInTaskbar property to false. The form is hidden and the code is working.

How to programmatically click on cross button X c#

i searched google lot to know how can i programmatically click on cross button X using c#. i got the below code which is used for different purpose.
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if (string.Equals((sender as Button).Name, #"CloseButton"))
// Do something proper to CloseButton.
else
// Then assume that X has been clicked and act accordingly.
}
so anyone can tell me How to programmatically click on cross button for closing form. thanks
To check the close reason you can use the CloseReason enum
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.ApplicationExitCall)
// Do something...
}
If you want to programattically close a form you can use
this.Close();
This will launch the FormClosing event that is displayed above.
I hope this helps.
If you want to emulate close click investigate SendMessage to send WM_CLOSE to you window.

Using Exit button to close a winform program

I have an exit button on a winform that I want to use to close the program. I have added the button name to the FormClosed property found in the events section of the winforms properties. I thought that's all I had to do but when I click the button it does not close. I looked at the code and while a handler is created, there is no code inside of it. I don't know if that is correct or not. Here is the code that was created in the Form.cs file:
private void btnExitProgram_Click(object sender, EventArgs e)
{
}
What else do I have to do?
this.Close();
Closes the form programmatically.
Remove the method, I suspect you might also need to remove it from your Form.Designer.
Otherwise: Application.Exit();
Should work.
That's why the designer is bad for you. :)
The FormClosed Event is an Event that fires when the form closes. It is not used to actually close the form. You'll need to remove anything you've added there.
All you should have to do is add the following line to your button's event handler:
this.Close();
We can close every window using Application.Exit();
Using this method we can close hidden windows also.
private void btnExitProgram_Click(object sender, EventArgs e)
{
Application.Exit();
}
Put this little code in the event of the button:
this.Close();
Try this:
private void btnExitProgram_Click(object sender, EventArgs e) {
this.Close();
}
Used Following Code
System.Windows.Forms.Application.Exit( )
In Visual Studio 2015, added this to a menu for File -> Exit and in that handler put:
this.Close();
but the IDE said 'this' was not necessary. Used the IDE suggestion with just Close(); and it worked.
If you only want to Close the form than you can use this.Close();
else if you want the whole application to be closed use Application.Exit();
You can also do like this:
private void button2_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.ExitThread();
}

How do you PerformClick(); for a button on a different tab?

I'm using Visual c# express 2010, I have 3 tabs and on the first tab there is a button that exits the program. I'm trying to call that button click on the 2nd and 3rd tab with
btnExit.PerformClick();
but since it isn't visible nothing happens. How would I call the invisible button click?
any help would be appreciated
EDIT:
Thanks for the replies, the two answers work great but I found a way that I think is easier and better.
instead of systematically changing tabs or calling a whole different method, I did this
btnExit_Click(sender, e);
I can put that in any other button click and it works great, very simple to.
I think it's better to create a method that actually has the code to exit the program, and call that method from btnExit click event and also other buttons click event, than PerformClick of the exit button.
void ExitApplication()
{
// code to exit the application
}
protected void btnExit_Click(object sender, EventArgs e)
{
ExitApplication();
}
protected void ButtonInOtherTab_Click(object sender, EventArgs e)
{
ExitApplication();
}
This way it's easier to read and understand.
myTabs.SelectedTab = specificTab;
btnExit.PerformClick();

Categories

Resources