In Windows form I have Button1, Button2 and Button3. These buttons represent a series of actions that should be carried out in a order from starting action to final action. So that normally I can handle this as follows.
Form1: Form
{
Form_Load(Object sender, event Args e)
{
Button1.Enabled = true;
Button2.Enabled = false;
Button3.Enabled = false;
}
Button1_click(Object sender, event Args e)
{
//Actions
Button2.Enabled = true;
Button1.Enabled = false;
}
Button2_click(Object sender, event Args e)
{
//Actions
Button3.Enabled = true;
Button2.Enabled = false;
}
Button3_click(Object sender, e)
{
//Actions
Button3.Enabled = false;
Button1.Enabled = true;
}
}
In several places I'm doing it in this way. Is this the standard way?
EDIT:
And also in a simple situation like you should have clicked button1 before clicking button2, the above approach is acceptable?
To expand on what Bjarke said, I'm providing a code example.
Form1: Form
{
List<Button> listButtons = new List<Button>();
public void EnableButton(Button btnToEnable)
{
foreach(Button btn in listButtons)
{
//check button name.
//if it is the button to enable, enable it, if not then disable it
btn.Enabled = btn.Name == btnToEnable.Name;
}
}
Form_Load(Object sender, event Args e)
{
listButtons.Add(Button1);
listButtons.Add(Button2);
listButtons.Add(Button3);
EnableButton(Button1);
//Button1.Enabled = true;
//Button2.Enabled = false;
//Button3.Enabled = false;
}
Button1_click(Object sender, event Args e)
{
EnableButton(Button2);
//Actions
//Button2.Enabled = true;
//Button1.Enabled = false;
}
Button2_click(Object sender, event Args e)
{
EnableButton(Button3);
//Actions
//Button3.Enabled = true;
//Button2.Enabled = false;
}
Button3_click(Object sender, e)
{
EnableButton(Button1);
//Actions
//Button3.Enabled = false;
//Button1.Enabled = true;
}
}
Related
I have this code:
private void btnAddDataToCSV_Click(object sender, EventArgs e)
{
var myProcess = new Process();
this.Enabled = false;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(Excel_Exit);
myProcess.StartInfo.FileName = "D:\\MyCsvFile.csv";
myProcess.Start();
}
public void Excel_Exit(object sender, System.EventArgs e)
{
MessageBox.Show("Success!!");
this.Enabled = true;
}
The code works as far as graying out all the buttons on my C# user form and opening the csv file in Excel. When I close Excel the exit event fires displaying a message "Success!!" But it doesn't re-enable the buttons after closing Excel.
I am getting the error
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
on the this.Enabled = true; line.
As you can see from my comments below. I have now come to to realization that I cannot figure out how to disable even a single button.
private void btnAddDataToCSV_Click(object sender, EventArgs e)
{
var myProcess = new Process();
button1.Enabled = false;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(Excel_Exit);
myProcess.StartInfo.FileName = "D:\\MyCsvFile.csv";
myProcess.Start();
}
public void Excel_Exit(object sender, System.EventArgs e)
{
button1.Enabled = true;
}
The event is firing because the code below works... (displays success!!)
private void btnAddDataToCSV_Click(object sender, EventArgs e)
{
var myProcess = new Process();
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(Excel_Exit);
myProcess.StartInfo.FileName = "D:\\MyCsvFile.csv";
myProcess.Start();
}
public void Excel_Exit(object sender, System.EventArgs e)
{
MessageBox.Show("Success!!");
}
Any help would be greatly appreciated.
i have added 2 more controls to the form, and set it to disabled on opening of excel file.
private void btnAddDataToCSV_Click(object sender, EventArgs e)
{
var myProcess = new Process();
AddtoCsv.Enabled = false;
textBox1.Enabled = false;
checkBox1.Enabled = false;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(Excel_Exit);
myProcess.StartInfo.FileName = "E:\\MyCsvFile.csv";
myProcess.Start();
}
after that, fetched all the controls inside our form and pass it to SetButtonState method.
public void Excel_Exit(object sender, System.EventArgs e)
{
// Set Button Enable State True
var controls = this.Controls.Cast<Control>();
foreach (Control ctrl in controls)
{
SetButtonState(ctrl);
}
}
SetButtonState wil make call to invoke method of passed control and then we are able to make changes onto control.
private delegate void SafeCallDelegate(Control control);
private void SetButtonState(Control control)
{
// check if current thread is same which have created this control
if (control.InvokeRequired)
{
SafeCallDelegate d = new SafeCallDelegate(SetButtonState);
control.Invoke(d, control);
}
else
{
control.Enabled = true;
}
}
Replace this.Enabled = false; with AddtoCsv.Enabled = false;
private void btnAddDataToCSV_Click(object sender, EventArgs e)
{
var myProcess = new Process();
AddtoCsv.Enabled = false;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(Excel_Exit);
myProcess.StartInfo.FileName = "E:\\MyCsvFile.csv";
myProcess.Start();
}
Just call SetButtonStatemethod inside your Excel_Exit method, and do not forget to declare the delegate.
public void Excel_Exit(object sender, System.EventArgs e)
{
// Set Button Enable State True
SetButtonState();
}
private delegate void SafeCallDelegate();
private void SetButtonState()
{
// check if current thread is same which have created this control
if (AddtoCsv.InvokeRequired)
{
SafeCallDelegate d = new SafeCallDelegate(SetButtonState);
AddtoCsv.Invoke(d);
}
else
{
AddtoCsv.Enabled = true;
}
}
Ok... I couldn't get the entire form to disable. But based on other stuff I have found, I created a "panel" and put all my buttons inside the panel and was able to enable the entire panel.
So the solution below works. Special thanks to Paramjot Singh who posted the other solution which works great for a single button, I don't know if I would have figured this out without his help!!
private void btnAddDataToCSV_Click(object sender, EventArgs e)
{
var myProcess = new Process();
panel1.Enabled = false;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(Excel_Exit);
myProcess.StartInfo.FileName = "E:\\MyCsvFile.csv";
myProcess.Start();
}
public void Excel_Exit(object sender, System.EventArgs e)
{
// Set Button Enable State True
SetButtonState();
}
private delegate void SafeCallDelegate();
private void SetButtonState()
{
// check if current thread is same which have created this control
if (panel1.InvokeRequired)
{
SafeCallDelegate d = new SafeCallDelegate(SetButtonState);
panel1.Invoke(d);
}
else
{
panel1.Enabled = true;
}
}
I have three panels and three buttons. Upon clicking button1 I want to show Panel1 and hide Panel2 and Panel3.
The same process with the other buttons and panels. How can I do this?
I'm using a Window Forms application written in C#/.NET. This code is not working very well, because only two panels are showing.
private void frmMain_Load(object sender, EventArgs e)
{
pnlItems.Visible = true;
pnlCustomer.Visible = false;
pnlPOS.Visible = false;
}
private void btnItems_Click(object sender, EventArgs e)
{
pnlItems.Visible = true;
pnlCustomer.Visible = false;
pnlPOS.Visible = false;
}
private void btnCustomers_Click(object sender, EventArgs e)
{
pnlCustomer.Visible = true;
pnlItems.Visible = false;
pnlPOS.Visible = false;
}
private void btnPOS_Click(object sender, EventArgs e)
{
pnlPOS.Visible = true;
pnlCustomer.Visible = false;
pnlItems.Visible = false;
}
private void frmMain_Load(object sender, EventArgs e)
{
pnlItems.Visible = true;
pnlCustomer.Visible = false;
pnlPOS.Visible = false;
}
private void btnItems_Click(object sender, EventArgs e)
{
if(pnlItems.Visible != true)
{
pnlItems.Visible = true;
pnlCustomer.Visible = false;
pnlPOS.Visible = false;
}
}
private void btnCustomers_Click(object sender, EventArgs e)
{
if(pnlCustomer.Visible != true)
{
pnlCustomer.Visible = true;
pnlItems.Visible = false;
pnlPOS.Visible = false;
}
}
private void btnPOS_Click(object sender, EventArgs e)
{
if(pnlPOS.Visible != true)
{
pnlPOS.Visible = true;
pnlCustomer.Visible = false;
pnlItems.Visible = false;
}
}
Here I have updated the conditional statement for if your panel is not visible then act. I hope this will work.
I want to disable other buttons while my mouse is pointing to another button in C#
private void Form1_Load(object sender, EventArgs e){
foreach (Button btn in Controls.OfType<Button>())
{
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
btn.MouseLeave += new System.EventHandler(btn_MouseLeave );
}
}
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Enable = true; // not working
}
private void btn_MouseLeave (object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Enable = false; // not working
}
This may not be the best solution, but i'll try anyways.
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
foreach(var btn in this.Controls.OfType<Button>())
{
if (btn != senderButton)
btn.Enabled = false;
}
}
private void btn_MouseLeave(object sender, System.EventArgs e)
{
foreach(var btn in this.Controls.OfType<Button>())
{
btn.Enabled = true;
}
}
Instead of
senderButton.Enable = true;
you have to loop over all buttons like
foreach(Button btn in this.Controls.OfType<Button>())
{
if (btn != senderButton)
btn.Enable = false;
}
private void btn_MouseEnter(object sender, System.EventArgs e)
{
foreach (Button btn in Controls.OfType<Button>())
{
if (btn == ((Button)sender))
{
btn.Enabled = true;
}
else
{
btn.Enabled = false;
}
}
}
private void btn_MouseLeave(object sender, System.EventArgs e)
{
foreach (Button btn in Controls.OfType<Button>())
{
btn.Enabled = true;
}
}
i am working in treeview in which no option for the child node and parent node click event so plz suggest me to work on Both click event
my code is
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode = "Edit User")
{
label2.Visible = true;
}
else
{
label2.Visible = false;
}
}
Try This :
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Text = "Edit User")
{
label2.Visible = true;
}
else
{
label2.Visible = false;
}
}
I have created gui for windows form....!
one side i have groupbox with checkbox and two buttons ">>" and "<<" and on the other side i have one more groupbox.
I need to select one checkbox at a time if the user select more than one checkbox i need to raise error..
if the user selects checkbox and click on ">>" button i need to display number of messages in the other groupbox i.e in the "List of Selected Commands"...
and on the click of ">>" the selected list of message should be deleted from list.
I have included tab control in my gui on click of this tab i need to display some list of commands how can i do it...
can any one help me on this...
This is the code..
namespace Menu_Sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
groupBox1.Text = "MSC";
groupBox2.Text = "List Of Selected Commands";
checkBox1.Visible = false;
cb2.Visible = false;
cb3.Visible = false;
cb4.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
}
private void submenu1ToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox1.Text = "ICP";
checkBox1.Visible = true;
cb2.Visible = true;
cb3.Visible = true;
cb4.Visible = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
label1.Visible = true;
label1.Text = "ibit";
groupBox2.Controls.Add(label1);
label2.Visible = true;
label2.Text = "Cbit";
groupBox2.Controls.Add(label2);
label3.Visible = true;
label3.Text = "Kbit";
groupBox2.Controls.Add(label3);
label4.Visible = true;
label4.Text = "ibit";
groupBox2.Controls.Add(label4);
}
if (cb2.Checked == true)
{
label1.Visible = true;
label1.Text = "ibit";
groupBox2.Controls.Add(label1);
label2.Visible = true;
label2.Text = "Cbit";
groupBox2.Controls.Add(label2);
label3.Visible = true;
label3.Text = "Kbit";
groupBox2.Controls.Add(label3);
label4.Visible = true;
label4.Text = "ibit";
groupBox2.Controls.Add(label4);
}
}
private void btn6_Click(object sender, EventArgs e)
{
if (label1.Enabled==true)
{
label1.Text = "";
}
}
private void btn5_Click(object sender, EventArgs e)
{
label1.Text = "";
label2.Text = "";
label3.Text = "";
label4.Text = "";
}
private void submenu2ToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox1.Text = "MCP";
}
private void mDPToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox1.Text = "MDP";
}
private void mRPPToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox1.Text = "MRPP";
}
}
}
I am giving you an idea, that how we can do that.
You have to create a group box click event and then use loop for the controls in that groupbox and if that control is checkbox and it is checked, then you just count that no-other checkbox should be checked.
I will be available with the code very shortly.