C# Windows Forms app - Clear ComboBox selected text from another ComboBox - c#

Windows Forms application (c#).
I have two ComboBoxes.
If I select an item in one, I want the text in the other one to be blank.
This is what I have:
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox2.Text = "";
}
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox1.Text = "";
}
... but as you can see, when I make a selection in one, the text in both ComboBoxes get cleared.
How to accomplish this?
Thank you.

Try setting the ComboBox.SelectedIndex to -1
A zero-based index of the currently selected item. A value of negative
one (-1) is returned if no item is selected.
or rather based on your specifications try something lie
private bool changed = false;
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!changed)
{
changed = true;
ComboBox2.Text = "";
changed = false;
}
}
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (!changed)
{
changed = true;
ComboBox1.Text = "";
changed = false;
}
}

Related

How to deselect item of listbox?

I have a ListBox that when an item is selected, it is shown in a label as well. However, when I want to remove the selected item, program breaks and shows a NullReferenceException.
My code:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = "Your Selected: " + listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = "";
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
It may appear, that there's no selected item in the listbox, so you have to check for that:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = null == listBox1.SelectedItem
? ""
: "Your Selected: " + listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e) {
// Looks redundant, listBox1_SelectedIndexChanged will do
//label1.Text = "";
// Deselect item, but not remove it
if (listBox1.SelectedIndex >= 0)
listBox1.SelectedIndex = -1;
// In case you want to remove the item (not deselect) - comment out the code below
// if (listBox1.SelectedIndex >= 0)
// listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
Edit: as for counting listbox items, there's no event fo this in the current listbox implementation. So you have to do it manually:
if (listBox1.SelectedIndex >= 0) {
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
lbItemsCount.Text = listBox1.Items.Count.ToString();
}
Another way is to use click event of the list box , if we do not want to double click the one list box item for the deselection of another list items.
ex:
private void ListBox_Right_Click(strong textobject sender, EventArgs e)
{
Btn_Left.Enabled = ListBox_Right.SelectedIndex >= 0;
ListBox_Left.ClearSelected(); // to clear the list selection/highlight
Btn_Right.Enabled = false; // for my specification
}
}
private void ListBox_Left_Click(object sender, EventArgs e)
{
Btn_Right.Enabled = ListBox_Left.SelectedIndex >= 0;
ListBox_Right.ClearSelected(); //to clear the list selection/highlight
Btn_Left.Enabled = false;// for my specification
}

Selecting a row of DataGridView programmatically

In my form application, there is a (buttonNEW) that selects NewIndexRow of DataGridView and I want to change index of datagridview with this button.
private void buttonNew_Click(object sender, EventArgs e)
{
if (dataGridView.CurrentRow.Index!=dataGridView.NewRowIndex)
{
dataGridView.ClearSelection();
dataGridView.Rows[dataGridView.NewRowIndex].Selected = true;
label1.Text = dataGridView.CurrentRow.Index.ToString();
}
}
But after clicking the button the index of DataGridView does not change.
What is the problem?
This should work :-
int numofRows = dataGridView.rows.count;
dataGridView.CurrentCell = dataGridView.Rows[numofRows - 1].Cells[0];
Or I think you could also do this :-
dataGridView.CurrentCell = dataGridView.Rows[dataGridView.NewRowIndex].Cells[0];
Try with Linc:
private void buttonNew_Click(object sender, EventArgs e)
{
dataGridView.Rows.OfType<DataGridViewRow>().Last().Selected = true;
}

How to show only a substring of an item in a ComboBox in C#

I have some Company names as Items in a ComboBox like this:
123 SomeCompany
Always a number between 100 and 999 plus the company name.
Now if the user selects an Item I want the TextBox to only show the number, not the company name. he company name should only be visible when he drops down the ComboBox...
I tried to set ComboBox1.Text = ComboBox.Text.Substring(0, 3) in the SelectedIndexChanged-Event and the TextChanged-Event, but it didn't do anything, there was always everything in the ComboBox...
AutocompleteMode is set to none.
What did I do wrong?
To always format the value, you could use the Format event (with FormattingEnabled = true)
private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = e.Value.ToString().Substring(0, 3);
}
But if you want the full value to be displayed when the dropdown is shown, you can temporarily disable the formatting:
private void comboBox1_DropDown(object sender, EventArgs e)
{
comboBox1.FormattingEnabled = false;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
comboBox1.FormattingEnabled = true;
}
Try this
textbox1.Text = ComboBox1.SelectedItem.ToString().SubString(0, 3);
Try something like this:
private void combox1_ SelectedIndexChanged(object sender,EventArgs e)
{
string value = (combox1.selectedItem != null) ?
combox1.selectedItem.ToString().Substring(0, 3) : string.Empty;
}

C# Winform Listview selected item issue

I'm trying to copy the selected item's filename and its path to the clipboard and then a textbox from a listview. I can't seem to get this one to work how i want. Here's the code I've been playing around with.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.Items.Count > 0)
{
listView1.Items[0].Selected = true;
Clipboard.SetDataObject(this.listView1.SelectedItems[0]);
textBox1.Paste();
}
}
Can someone get me on the right track?
I'm not sure why you're using the Clipboard here. You can do just fine without it.
listView1.Items[0].Selected = true;
textBox1.Text = this.listView1.SelectedItems[0].ToString();
private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
if (listView1.SelectedItems.Count > 0) {
textBox1.Text = listView1.SelectedItems[0].Text;
}
else {
textBox1.Text = string.Empty;
}
}

TextBox for Live Search in Windows Forms

How to create a Textbox which displays "search" in grey color when it is empty and standard behavior when user starts typing text into it?
Do it via TextBox Events Enter and Leave and Attributes:
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text.Trim().Length == 0)
{
textBox1.Text = "Search";
textBox1.ForeColor = Color.LightGray;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
See this thread at MSDN for a possible solution: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/93a67793-6426-4d4f-be9d-a9b79725efc8

Categories