opening folder browser dialog on button click c# - c#

im trying to trigger a folder browser dialog with a button in c#
ive tried
private void Button2_Click(object sender, EventArgs e)
{
DialogResult auswahl2 = folderBrowserDialog2.ShowDialog();
if (auswahl2 == DialogResult.OK)
{
TextBox5.Text = folderBrowserDialog2.SelectedPath;
}
}
but neither debug nor release will open one on button click.
What am i missing ?
Thanks in Advance

Go to your form, and double-click the button in question, see if it pointed to the above function, or a new event function is created.
If it was the second, copy everything above to the new function and run the program again, everything should be fine after that.
If you want to do it manually, go to YourForm.Designer.cs, and add this.Button2.Click += new System.EventHandler(this.Button2_Click); under InitializeComponent().
Or go to the code of your form, select InitializeComponent under the constructor, and press F12, and it will bring you to the right place.

Your code works perfectly but check if Button2_Click(object sender, EventArgs e) event is plugged.
Mehdi

Related

How to add an event on a menu in C# project?

I have a C# Windows Forms App that contain a menu bar.
I want to display a Help Message when I press on the "HELP" menu button.
All that I can see when I press view code is this:
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I think that I need to create inside the function a MessageBox or an event that will display the desired message.
Do you have any idea how should I do this, please?
Below should work for what your asking. If you are on your form you can double click the button you want to interact with, and Visual Studio should take you to the empty method.
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("This is supposed to be helpful");
}

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.

How do I fire up CellContentDoubleClick in C#?

I'm using VS 2010 C#.
I have a form that only has a grid connected to a table. Basically, my idea is to select a row by double clicking it. Then later copy the ID and close the form to proceed to another form. But it is not working as per my test on doubleclick event on grid. It is suppose to show a Message box but it is not triggering.
I'm still new on C# and I've browsed the net for similar problem but most of the example are in VB, there was even one suggestion for me to make dgv a readonly=false (implemented on code).
Here is my code:
...
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cISDataSet.tbl_Person' table. You can move, or remove it, as needed.
this.tbl_PersonTableAdapter.Fill(this.cISDataSet.tbl_Person);
this.dataGridView1.ReadOnly = false;
}
private void DataGridView1_CellContentDoubleClick(Object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("test");
}
...
I do not have any syntax error on my program.
Add this line to your form1_load. (after this.dataGridView1.ReadOnly = false;)
dataGridView1.CellContentDoubleClick += DataGridView1_CellContentDoubleClick;
You only need to tell the data grid view where to go when a double click happens.

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