WinForms ComboBox - How to Check Values - c#

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();

Related

How to prevent a user from selecting an item in a listbox, but still letting code select an item? Prefer not to use enabled = false

I am making a Windows Forms application with algorithms for school and I want to add some nice functionality to display that the algorithm is working well. One of those things is that when the user selects an item in one listbox, the items that are part of that one item get automatically selected in another listbox. This is done by the application.
I would like it if the user could not select another item in the listbox that is automatically monitored, but enabled = false sets the color to gray which makes the text invisible when an item is automatically selected.
Is there any other way to achieve this?
What you could do is
When the program selects an entry in the second list, set a flag
When an entry in the list is selected, read that flag
If the flag is not set, unselect the item
Unset the flag
Code wise, this equates to something like the following (please note that I would not write code like this in a real-world szenario, but to get the gist of it, it should suffice)
private bool _valueIsSetProgrammatically = false;
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
this._valueIsSetProgrammatically = true;
this.listBox2.SelectedItem = this.listBox1.SelectedItem;
}
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (!this._valueIsSetProgrammatically)
{
this.listBox2.SelectedItem = null;
}
this._valueIsSetProgrammatically = false;
}
Please note that this snippet unselects the second listbox. If you'd like to retain the selected item, you could change the second method to
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (!this._valueIsSetProgrammatically)
{
this.listBox2.SelectedItem = this.listBox1.SelectedItem;
}
this._valueIsSetProgrammatically = false;
}
(Technically the flag is not needed in this case, you could simply set the SelectedItem of listBox2 to the SelectedItem of listBox1.)

Disable Command button until comboBox value is selected

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.

App-wide Context Menu

I have created a context menu for a text-box in my .NET application, and it works great. Now I would like to use that same context-menu for all text-boxes in my application. Currently, I am using code like the following to work with the existing functionality:
private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
// Disable Undo if CanUndo property returns false
if (mainTextBox.CanUndo)
{
contextMenuStrip1.Items["Undo"].Enabled = true;
}
else
{
contextMenuStrip1.Items["Undo"].Enabled = false;
}
}
My question is how do I go about converting this code so that all text-boxes can use the same code, rather than rewriting this over and over again for all text-box instances. I have assigned the same contextmenu to all of the text boxes, but more specifically, how do I pass the name of the calling text-box into the function? I was hoping it would be simple like perhaps:
// Disable Undo if CanUndo property returns false
if (this.CanUndo)
{
contextMenuStrip1.Items["Undo"].Enabled = true;
}
else
{
contextMenuStrip1.Items["Undo"].Enabled = false;
}
Unfortunately, this isn't working. Any ideas?

select combobox item by pressing enter key in c# windows aplication

In c#, I want to select combo box items by keyboard and when i press enter after selecting one that item should be selected. how to do it?
try something like this ..., this will explain how to change the items using mouse and key board ,....
I found this method worked fine in all the conditions. But I m not sure if anything more accurate than this method is available.
bool IsMouse = false;
private void cmbMy_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (IsMouse)
{
//Write the logic if selection is changed by mouse
}
else
{
//Write the logic if selection is changed by keyboard
}
IsMouse = false;
}
private void cmbMy_IsMouseCapturedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
IsMouse = true;
}
In isMouseCapturedChanged event of combo box i made a bool variable true and when selection changed of the combo box i m checking the bool doing the required task and then setting isMouse to false.
Or you need to bulid your own custom combobox ..
You need build a custom ComboBox class and override the Control.ProcessKeyEventArgs Method.

Disabling buttons problem on C#

Ok so I'm trying to move items from one listbox to another by using multiple buttons i.e
I have 2 buttons cmdRight and cmdRight2 which are both disabled on form load
If the user selects a single item on the first listbox a cmdRIght button enables but cmdRight2 is still disabled , if the user selects multiple items on the first listbox a cmdRight2 button enables but cmdRight is disabled.
I've got the move buttons to work but the problem I'm having is after moving multiple items with the cmdRight2 button the cmdRight button enables (which it shouldn't it should only enable after selecting a single item in the listbox). I've tried numerous if statements etc. and yet it still happens.
I'm new to C# so any help would be appreciated.
Thank You
private void lbList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.lbList1.SelectedItems != null)
{
cmdRight.Enabled = true; //enable cmdRight
cmdClear.Enabled = true; //enable cmdClear
if (this.lbList1.SelectedItems.Count > 1)//if multiple items selected
{
cmdRight.Enabled = false;
cmdRight2.Enabled = true; //enable cmdRight2
}
}
}
private void cmdRight2_Click(object sender, EventArgs e)
{
foreach (int i in lbList1.SelectedIndices)
{
lbList2.Items.Add(lbList1.Items[i].ToString());
}
while (lbList1.SelectedItems.Count > 0)
{
lbList1.Items.Remove(lbList1.SelectedItems[0]);
}
cmdRight2.Enabled = false;
}
private void cmdRight_Click(object sender, EventArgs e)
{
lbList2.Items.Add(lbList1.SelectedItem); //Add selected item from list1 to list2
lbList1.Items.Remove(lbList1.SelectedItem);//remove the selected item in list1
cmdRight.Enabled = false; //disable cmdRight
}
How about creating one method EnableButtons that enables/disables to buttons according to given criteria like "enable cmdRight2 only if.... is true".
Then, call the method whenever some of the criteria might change. The advantage of this over the way you're doing it now is that the criteria within the method are "absolute" (in that the buttons are either enabled or disabled in one go) instead of "relative" (enable the button when the user does this or that).
You could also call this method from the Application.Idle event instead of calling it in response to some user action.
EDIT
Declare the following method:
private void EnableButtons()
{
controlX.Enabled = (<condition...>);
controlY.Enabled = (<condition...>);
}
You can either invoke that method from the positions in code where something should change in the buttons' enabled states, or you can do the following in the constructor of the form:
public Form1()
{
// Other code...
Application.Idle += new <The respective event handler>;
}
Then, declare a method with the respective signature for the event and call EnableButtons there. This method would be called in situations where your application is "idle" (waiting for user actions).
I think you want
if (this.lbList1.SelectedItems.Count == 1)
{
}
else if(this.lbList1.SelectedItems.Count > 1)
{
}
else
{
}
instead of
if (this.lbList1.SelectedItems != null)
Then you could place all of this in a method called "EnableButtons" as mentioned elsewhere
The problem is that you are removing the items one by one, so when only one item is left, you essentially have one item selected so your program enables the cmdRight. The easiest way around this is to have
cmdRight2.Enabled = false;
cmdRight.Enabled = false;
at the end of the cmdRight2_Click method.

Categories

Resources