Close about box - c#

I'm trying do make a windows software. But when I added the About inside the Help button which is inside the menu stripe.
I created a new winform from the VS 2013 templates called "AboutBox", and it comes with a button OK. I want this button when clicked to close only this "About Box". I pasted down here only the button method, I didn't change anything at all from the code
private void okButton_Click(object sender, EventArgs e)
{
//close aboutBox;
}

Something like this:
this.DialogResult = DialogResult.Ok;
this.Close();
Or, as someone pointed out, just edit the resource in the IDE for the button and set the DialogResult property.:

Related

Open and close forms through a menu c#

I've had a look around but none of the answers make any sense to me. I have a menu form which has buttons on; when users come to use the menu form, you can open other forms from the menu. Currently, I can get the form to open, but the menu form stays open too.
private void BtnAddNewCar_Click(object sender, EventArgs e)
{
AddCompanyCar carForm = new AddCompanyCar();
carForm.ShowDialog();
}
The code above opens the form AddCompanyCar from the menu. How do I add to this code so that the form 'Menu' closes when AddCompanyCar opens?
Are you sure want to do this as it impacts usability. If you're using WinForms, then just create a container window, and replace the panels instead. Might be easy and best way
If not and you wanna go-ahead, can take a look on this example
Why not just hide it, then show it again when ShowDialog() returns?
private void BtnAddNewCar_Click(object sender, EventArgs e)
{
this.Visible = false;
AddCompanyCar carForm = new AddCompanyCar();
carForm.ShowDialog(); // execution stops here until "carForm" is dismissed
this.Visible = true;
}
by closing the main window, you destroy the context in which you were previously working. As others suggest, simply hide the main window so you can return to it.

Close event not working

I want if user click the close button to close a WinForm, it will perform some work such as showing MessageBox message:
void frmMemberSearch_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("close message");
}
the about code does not work. Does anyone know what may be the problem?
just double click form closing event on Form Properties Tab.

Disable click button event c#

I program in Visual Studio 2013 C#.
I want to have a functionality to disable for short time a button.
I tried button.Enabled=false, but I see that when I click it during it is disabled then the click action starts right after I get that button enabled in other place of the program.
How to clear that event or block them when button is disabled?
Best regards,
Krzysztof
for disable button click.
button1.Click -= button1_Click; // here button1_Click is your event name
for enable button click.
button1.Click += button1_Click; // here button1_Click is your event name
This is the extremely simple way that I found on the internet and it works.
Disable the button(s)
Run whatever is needed to be performed on the procedure
Run this command Application.DoEvent();. This command enqueues all the click events (as many as the user clicked) and ignore/dispose them
Enable the button(s) at the end
Good Luck
Can't you just check for button's state before executing commands in the method?
For example:
void Click(object sender, EventArgs e)
{
if(!(sender as Button).Enabled)
return;
//other statements
}
Try
YourButton.Attributes.Add("onClick", "");
To remove its onClick altogether.
and then
YourButton.Attributes.Add("onClick", "YourButton_Click");
To add it again.
But your program shouldn't execute the Click when you say it does. It's likely there's something wrong in your code's logic.
Look at this code below, after click button2, button 1 is disabled. while button 1 is disabled, click button 1. After button 1 enable automatically after 5 seconds textBox1 update to "Update".
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Update";
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Thread.Sleep(5000);
button1.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
btn.Visibility = Visibility.Collapsed;
You can try this.
ALL WRONG!
"button1_Click" is not the standard handler behind a button, so it cannot be removed. That is your own function.
YourButton.Attributes does not exist at all.
Nobody even mentioned "Web Forms".
Your description does not match the Windows Forms Button behavior.
The solution for the question you really asked is:
Use a usual Windows Forms Button or override the click event to check again if the button is enabled.

MaskedTextBox.SelectAll on GotFocus doesn't work with mouse

I want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.
I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).
I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?
How can I get around this?
Steps to reproduce
Create a new Windows Form Application in .NET 4.0 in Visual
Studio 2010.
Add a TextBox, MaskedTextBox, and Button to the default form
Change the Mask property on the MaskedTextBox to "_____".
Add some event handlers:
private void maskedTextBox1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("GotFocus");
maskedTextBox1.SelectAll();
}
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Click");
maskedTextBox1.SelectAll();
}
Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.
Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
Try clicking on button in form. Text doesn't get selected.
Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project
Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB
If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.
Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}
Executing Focus before Select All worked for me:
private void Masked_Enter(object sender, EventArgs e) {
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).SelectAll();
}

How to close C# process?

I wrote a simple program using Visual Studio(C#). When I close my program(click at Х) the form is closed, but the process remains. I had to close it from Task Manager.
What is the command to close process?
My code:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
e.Cancel = true; means stop the form from closing. That's the main issue with your code.
You should use this parameter if you have a confirmation something like "Are you sure you want to close?" if the user selects "No" you set e.Cancel to true.
Just remove that code (or set Cancel to false) and your form will close, and if that's the last one of your application, it will end.
So the final solution for your problem is a simple
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
The e.Cancel=true will prevent your form from closing, so remove it.
if it still doesn't work, try this :
use the Application.Exit() method on the FormClosed event instead of FormClosing:
To do this, go into your form designer window : press maj + F7 or right click on your form in your solution explorer and click on Form designer (something like that)
Right Click on your Form, Select Properties, display the Events, then double-click on the Form Closed event and the designer will automaticaly register the event and generate your code.
You should have something like this without the Application.Exit() method generated :
private void Form1_FormClosed(object sender, FormClosingEventArgs e)
{
Application.Exit();
}

Categories