I have a comboBox (cmbPortName) and a command button (btnConnect).
You'd use the drop-down in the comboBox to select a port you want to connect to and then click btnConnect.
I just want to disable the command button till a valid selection is made in ComboBox. I figured the best way to solve this by doing something like
btnConnect.Enabled = True;
until a selection is made in the Combobox.
Is there a better way of doing it? I am quite new to the programming and still learning stuffs.
You need to add a SelectedIndexChanged event handler for the combo box. In the Visual Studio design view for your form, double-click the combo box, or double-click the empty space to the right of the event name in the "Properties" window:
That will generate and bring you to this block of code in your form's .cs file:
private void cmbPortName_SelectedIndexChanged(object sender, EventArgs e)
{
}
And then add whatever code you want to conditionally enable your button:
private void cmbPortName_SelectedIndexChanged(object sender, EventArgs e)
{
// This will enable the button so long as the selected value
// is not null or an empty string.
if (cmbPortName.SelectedItem != null && !string.IsNullOrEmpty(cmbPortName.SelectedItem.ToString()))
btnConnect.Enabled = true;
else
btnConnect.Enabled = false;
}
Disable the button at first.
if(cmbPortName.SelectedIndex > 0)
{
btnConnect.Enabled = True;
}
There is an event for the combobox selected item change, you can write btnConnect.Enabled = True there.
Related
I want to show the list of items in a combo box when the user selects the text. I have a touch screen application and it's very difficult to hit the dropdown arrow so I figure I'd show the menu when the text is selected which is often what gets touched. I'm using VS 2008. and suggestions for a touch friendly numeric up down solution in VS2008?
You could use the ComboBox.Click event handler and the ComboBox.DroppedDown property and do something like this:
private void ComboBox1_Click(System.Object sender, System.EventArgs e)
{
ComboBox1.DroppedDown = true;
}
You could also use the same event handler for a numericUpDown and use the mouseposition as well as the position and height of the NumericUpDown to get whether or not the click was above or below the halfway-line of the control by doing something like this (not sure if my math here is perfect, but it worked when I tested it):
if ((MousePosition.Y - this.PointToScreen(NumericUpDown1.Location).Y < NumericUpDown1.Height / 2))
{
NumericUpDown1.Value += 1;
}
else
{
NumericUpDown1.Value -= 1;
}
HTH
I was working on a similar situation. We wanted to make the text area behave the same as the button on the right. (IE the user clicks and gets the drop down box)
davidsbro is similar to what I ended up doing, but we wanted it to close if they clicked again, so the value became dropDown.DroppedDown = !dropDown.DroppedDown;.
The issue with this is that if the user clicks the right button of the drop down box, the dialog box opens, then calls the onClick event.
I solved this situation by tracking the original state via the onmouseover event. If the value has changed, we have to assume that the button on the select box handled the click already.
private bool cbDropDownState = false;
private void dropDown_MouseEnter(object sender, EventArgs e)
{
cbDropDownState = dropDown.DroppedDown;
}
private void dropDown_Click(object sender, EventArgs e)
{
if (dropDown.DroppedDown == cbDropDownState )
dropDown.DroppedDown = !dropDown.DroppedDown;
}
Inside my MainForm there is many buttons and one combo box.
Before processing action when user select any button I want to force him to first select option from combo box.
For example combobox is cmbMyList and button is btnSave
Have the button disabled if no option in the combobox is selected. Enable it when a selection is made. Use events to detect when this happens, for example by use of the ComboBox.SelectedIndexChanged Event
I usually add checks to my Button_OnClick event. Just to keep it simple:
public void btnSave_Click(object sender, EventArgs e)
{
if (cmbMyList.SelectedIndex.CompareTo(n) == 0) // n - your empty value index
{
MessageBox.Show("Selected value is not valid.");
}
else
{
// proceed
}
}
Please read my question its not a duplicate one.
I've three radio buttons on windows form and all these buttons have common 'CheckedChanged' event associated. When I click any of these radio buttons, it triggers the 'CheckedChanged' event twice.
Here is my code:
private void radioButtons_CheckedChanged(object sender, EventArgs e)
{
//My Code
}
I inserted the breakpoint and the whole code within this event iterates twice.
Please tell me why it is behaving like this?
As the other answerers rightly say, the event is fired twice because whenever one RadioButton within a group is checked another will be unchecked - therefore the checked changed event will fire twice.
To only do any work within this event for the RadioButton which has just been selected you can look at the sender object, doing something like this:
void radioButtons_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb != null)
{
if (rb.Checked)
{
// Only one radio button will be checked
Console.WriteLine("Changed: " + rb.Name);
}
}
}
To avoid it, just check if radioButton is checked
for example:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
//your code
}
CheckedChanged is raised whenever the Checked property changes. If you select a RadioButton then the previously selected RadioButton is unchecked (fired CheckedChanged), and then the new RadioButton is checked (fired CheckedChanged).
It's triggering once for the radio button transition from checked to unchecked, and again for the radio button transitioning from unchecked to checked (i.e. any change in checked state triggers the event)
You could set the AutoCheck property true for each RadioButton then catch the Click event instead of the CheckChanged event. This would ensure that only one event is fired, and the logic in the handler can cast the sender to type RadioButton if needed to process the click. Often the cast can be avoided if the handler logic is simple. Here is an example which handles three controls, rbTextNumeric, rbTextFixed and rbTextFromFile:
private void rbText_Click(object sender, EventArgs e)
{
flowLayoutPanelTextNumeric.Enabled = rbTextNumeric.Checked;
txtBoxTextFixed.Enabled = rbTextFixed.Checked;
flowLayoutPanelTextFromFile.Enabled = rbTextFromFile.Checked;
}
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
int click = 0;
private void radioButton1_Click(object sender, EventArgs e)
{
click++;
if (click %2==1)
{
radioButton1.Checked = true;
}
if (click %2==0)
{
radioButton1.Checked = false;
}
if (radioButton1.Checked==true)
{
label1.Text = "Cheked";
}
if (radioButton1.Checked==false)
{
label1.Text = "Uncheked";
}
}
}
}
The other answers are correct but miss the reason for the underlying problem.
When a radio button is checked the first event sent is the change from the unchecked item
however if you check its state by its control name you will still see its old checked status because the form has not been updated yet. To see its true status you need to cast the sender object.
This allows you to perform any actions relating to the condition which is being deselected should you need to do so.
In the not uncommon scenario below multiple radio buttons are sent to the same handler event.
Simply checking the state of the sender for checked will not work here as we need to perform different actions depending on which radio button has been pressed.
So first we ignore any sender that has just been unchecked.
then we identify the checked sender by control name to process the correct action.
private void ModeChangedExample(object sender, EventArgs e)
{
// multiple radio buttons come here
// We only want to process the checked item.
// if you need to something based on the item which was just unchecked don't use this technique.
// The state of the sender has not been updated yet in the form.
// so checking against rdo_A check state will still show it as checked even if it has just been unchecked
// only the sender variable is up to date at this point.
// To prevent processing the item which has just been uncheked
RadioButton RD = sender as RadioButton;
if (RD.Checked == false) return;
if (rdo_A.Name == RD.Name)
{
//Do stuff
}
if (rdo_B..Name == RD.Name)
{
// Do other stuff
}
if (rdo_C.Name == RD.Name)
{
// Do something else
}
}
This problem of double checking happens when there is a series of RadioButton Clicks in succession.I had this same problem.The last click will give two results.To overcome this i made a dummy click in the end.The double click stopped.Try this method.
Venkatraman
I've got a combobox that opens a new form window with a datagridview, and I want the users to choose the items through that datagridview rather than through the combobox. I've got this code to achieve that:
private void comboBox1_DropDown(object sender, EventArgs e)
{
valSel.incBox = (ComboBox)sender;
valSel.Show();
if (this.comboBox1.DroppedDown)
{
MessageBox.Show("test");
SendMessage(this.comboBox1.Handle, CB_SHOWDROPDOWN, 0, 0);
}
}
As you see I'm also trying to hide the dropdown of the combobox but it isn't working. I assume it's because the combobox hasn't actually "dropped down" yet, so that part of the code is never run.
Is there an event or something I can cell when the combobox has fully "dropped down" so i can send the message to close it again?
You should be able to simply set the height of the ComboBox to something really small. Last time I looked at it, this determined the height of the popup part (the actual height of the control is determined by the UI/font size).
The more elegant way, however, would be using a custom control that just mimics the appearance of dropdown boxes (I'm rather sure that can be done some easy way).
In comboBox1.Enter set the focus to a different control if condition is met.
private void comboBox1_Enter(object sender, EventArgs e)
{
if (comboBox1.Items.Count < 1)
{
comboBox1.DroppedDown = false;
comboBox2.Focus();
MessageBox.Show("Select a list first");
comboBox2.DroppedDown = true;
}
}
1) create a KeyPress event on ComboBox from the properties.
2) write code
private void cmbClientId_KeyPress(object sender, KeyPressEventArgs e)
{
((ComboBox)sender).DroppedDown = false;
}
I am building a WinForms Application in C# .NET
The WinForms Application has a ComboBox where the DropDownStyle is set to DropDownList. When the App is launched, I read an XML file to populate the values of the ComboBox. And, at this time, nothing is selected in the ComboBox by default. As a result, buttons Change and Delete are disabled.
Now, when the user selects a value, I want the buttons Change and Delete to be enabled. So far I have accomplished (although, I am not sure that I have done it in the right way).
I have written the code in the SelectionChangeCommitted Event.
private void cbList_SelectionChangeCommitted(object sender, EventArgs e)
{
if (cbList.SelectedItem != null)
{
this.btnModify.Enabled = true;
this.btnRemove.Enabled = true;
}
else
{
this.btnModify.Enabled = false;
this.btnRemove.Enabled = false;
}
}
Now, when I chose a value...the buttons get enabled (as expected). The user then clicks on Delete button and we remove the selected value. Now, there is nothing Selected in the cbList but the buttons are still enabled?
What is the function/event where I check if a value is selected or not and then enable/disable the buttons.
At the moment, dont have Visual Studio, so I dont remember which events we have. But you can make this,
private void CheckButtons()
{
if (cbList.SelectedItem != null)
{
this.btnModify.Enabled = true;
this.btnRemove.Enabled = true;
}
else
{
this.btnModify.Enabled = false;
this.btnRemove.Enabled = false;
}
}
and use your func in event
private void cbList_SelectionChangeCommitted(object sender, EventArgs e)
{
CheckButtons();
}
as you said, after deleting, buttons are still visible, so you can put CheckButtons() function after your delete function like
DeleteX();
CheckButtons();