C# Updating Selected ComboBox Item Name from Textbox - c#

I want to update comboBox selectedItem name by changing textbox name. Without losing combobox value, How can I achieve it?
private void addItem_Click(object sender, EventArgs e)
{
nameItem.Enabled = true;
nameItem.Text = "Item " + counter.ToString();
nameItem.Focus();
comboBox1.Items.Add(nameItem.Text);
comboBox1.SelectedItem = nameItem.Text;
counter++;
}
private void nameItem_TextChanged(object sender, EventArgs e)
{
????????
}

This one is simple and it's working, but it maybe a little bit long.
Here I got a combo box, textBox1 and button for adding value to combo box,
and textBox2 for editing selected item.
string[] items = new string[99];
int a = 0;
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
items[i] = textBox1.Text;
i++;
comboBox1.Items.Clear();
for (int n = 0; n < items.Length; n++) {
if(items[n] != null) comboBox1.Items.Add(items[n]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
a = comboBox1.SelectedIndex;
MessageBox.Show(a.ToString());
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
items[a] = textBox2.Text;
comboBox1.Items.Clear();
for (int n = 0; n < items.Length; n++)
{
if (items[n] != null) comboBox1.Items.Add(items[n]);
}
}
How it works: we have an array for items and two integar variables. one for count of added items and other for selected Items index
Button 1 just addes new item, clears all items and update em again
when u edit text of textbox2, It will update items from 'items' array, and then update the combo box, ez

EDIT: Did not see winform tag - this will not work - will leave in case any ASP people come across.
public void TextBox1_OnTextChanged(object sender, EventArgs e)
{
ddl.DataSource = null;
ddl.DataBind();
ddl.DataTextField = "Text";
ddl.DataValueField = "Value";
ddl.DataSource = (from ListItem b in ddl.Items
select b.Selected ? new ListItem(TextBox1.Text, b.Value) : b).ToList();
ddl.DataBind();
}
ddl is name of the dropdown box
Textbox1 is the name of the textbox.
This will change the name of the selected item. If you need more code let me know in comments.

Thank you for your time and kind answers.
I solved the problem as it looks below;
private void addItem_Click(object sender, EventArgs e)
{
nameItem.Enabled = true;
comboBox1.Items.Add("Item " + counter.ToString());
comboBox1.SelectedItem = "Item " + counter.ToString();
nameMacro.Text = "Item " + counter.ToString();
//comboBox1.SelectedItem = nameItem.Text;
//nameItem.Focus();
// Ad degistirme -> comboBox1.Items[comboBox1.FindStringExact("string value")] = "New Value";
counter++;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.Items[comboBox1.SelectedIndex] = nameItem.Text;
}

it's better to update the source then bind it again.
try this also
private void nameItem_TextChanged(object sender, EventArgs e)
{
string value = nameItem.Text;
var list = (List<KeyValuePair<String, String>>)comboBox1.DataSource;
list.Add(new KeyValuePair<string, string>(value,value));
comboBox1.DataSource = list;
comboBox1.DataBind();
}

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
}

How to disable button by selecting comboBox value

I have a comoboBox that is binded to sql database, and I added a default text at index 0 like this
string s = "< -------------Select an application ----------->";
applicationComboBox.Items.Insert(0, s);
applicationComboBox.SelectedIndex = 0;
I am wondering if there is a way to disable my button if the the string s at index 0 is select? In my comboBox, I binded the data with the while(SQLReader.Read()) method instead of using ValueMember and `DisplayMember
Here is what I tried but no luck
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
for (int i = 1; i < applicationComboBox.Items.Count; i++)
{
string value = applicationComboBox.GetItemText(applicationComboBox.Items[0]);
string s = "<------------- Select an application ----------->";
if (value == s)
{
exportButton.Enabled = false;
MessageBox.Show(value); //nothing happen
this.teacherCheckListBox.DataSource = null;
teacherCheckListBox.Items.Clear();
}
else
{
exportButton.Enabled = true;
}
}
}
}
Use SelectedIndex property to know which item is selected and disable the button if it is first item.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
exportButton.Enabled = false;
}
}

Get values of dynamically created controls (comboboxes)

I've got panel on which by default are two comboboxes and one "+" button which creates two new combo boxes bellow the first one, I can create multiple (n) rows with two combo boxes and everything is working, I just can't figure out how to get values of those boxes?
Here's code for creating (adding) controls
private void btnCreateFilter_Click(object sender, EventArgs e)
{
y += comboBoxHeight;
ComboBox cb = new ComboBox();
cb.Location = new Point(x, y);
cb.Size = new Size(121, 21);
panelFiltri.Controls.Add(cb);
yDrugi += comboBoxHeight;
ComboBox cbSql = new ComboBox();
cbSql.Location = new Point(xDrugi, yDrugi);
cbSql.Size = new Size(121, 21);
panelFiltri.Controls.Add(cbSql);
btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + 25);
btnSaveFilter.Location = new Point(btnSaveFilter.Location.X, btnSaveFilter.Location.Y + 25);
}
And here's code where I'm lost:
private void btnSaveFilter_Click(object sender, EventArgs e)
{
int i;
foreach (Control s in panelFiltri.Controls)
{
//GOT LOST
}
}
You can get the text in the ComboBox as
private void btnSaveFilter_Click(object sender, EventArgs e)
{
foreach (Control control in panelFiltri.Controls)
{
if (control is ComboBox)
{
string valueInComboBox = control.Text;
// Do something with this value
}
}
}
I don't really know what you're trying to achieve... Maybe this will help you along...
private void btnSaveFilter_Click(object sender, EventArgs e)
{
foreach (ComboBox comboBox in panelFiltri.Controls)
{
var itemCollection = comboBox.Items;
int itemCount = itemCollection.Count; // which is 0 in your case
}
}

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 can i move between items in listBox and show the item name on a label.Text?

I have a listBox1 with 4 items inside. I can use the keys to move up-down between the items or click with the mouse once on each item in both cases the selected items will be highlight with blue marked .
I want when I click on an item or when I move the keys up and down over the items it will change the label.Text with the current item name.
For example in on the item moses so label1.Text will contain moses.
Moved to the next item with arrow key up so now label1.Text contain daniel.
Clicked with the mouse on the item number 3 now label1.Text will contain dana.
Tried with this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
//listBox1.Items.Add(fsi[i].Name + Environment.NewLine);
label2.Text = listBox1.Items[i].ToString();
}
}
But its not worrking.
Works for me.
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Item1");
listBox1.Items.Add("Item2");
listBox1.Items.Add("Item3");
listBox1.Items.Add("Item4");
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = listBox1.SelectedItem.ToString();
}
You really expected your code to work? Why iterate over the whole collection, if you just need to check for the currently selected item?
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
label2.Text = lbi.Content.ToString();
}
or if you're using webforms:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label2.Text = listBox1.SelectedItem.Text;
}
If you are using List<CustomClass>/ObservableCollection<CustomClass> as ItemSource for ListBox try the following way in listbaox selected index changed event
var listTapped = sender as ListBox;
var selectedUser = listTapped.SelectedItem as CustomClass;
if (selectedUser == null)
return;
label2.Text = selectedUser.Name; //
ListBox contains event SelectedIndexChanged. It raises on such conditions. I think you should use it. Then you should use SelectedValue property to get correct string.

Categories