I want to move items back and forth between a ComboBox and a ListBox using C# 2010 (form)
My code seems to work. However, when I move the items back to the ComboBox (from the ListBox) I have a space in between the items. If anyone has a suggestion on how to remove the space between the items in the ComboBox I would greatly appreciate it.
private void stateslistcomboBox_SelectedIndexChanged(object sender, EventArgs e)
{
stateslistBox.Items.Add(statescomboBox.SelectedItem);
statescomboBox.Items.RemoveAt(statescomboBox.SelectedIndex);
}
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
statescomboBox.Items.Add(item);
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
The statescomboBox.Items.Add(item); triggers Another SelectIndexChanged that adds an empty item.
Try
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
statescombobox.SelectIndexChanged -= stateslistBox_SelectedIndexChanged;
statescomboBox.Items.Add(item);
statescombobox.SelectIndexChanged += stateslistBox_SelectedIndexChanged;
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
alternatively, you can prevent empty items being added.
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
if (!string.IsNullOrEmpty(item)
{
statescomboBox.Items.Add(item);
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
}
Related
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
string val = listBox1.Text.Trim();
if (listBox1.Items.Contains(val)) {
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
else
{
MessageBox.Show("There is no items present");
}
}
elements are entered from text box to list box, If entered the same data,. how to check? or msg box should display and
while deleting items from the list box if there is no items how to i get to know.
You can check if the value entered in the textbox is already in the listbox or not:
bool listContainsItem = Listbox.Items.Any(item => item.Value == textboxValue);
if(listContainsItem)
{
// ... item is in listbox, do your magic
}
else
{
// ... item is not in listbox, do some other magic
}
You can do this in the Onchange event of your textbox, or when clicking a button, ... give us more context so we can provide you a better solution.
You can use a HashSet as data source to make sure your list contains unique elements.
In example :
HashSet<string> ListBoxSource = new HashSet<string>();
private void button2_Click(object sender, EventArgs e)
{
string val = listBox1.Text.Trim();
// ListBoxSource.Add(val) Return true if val isn't present and perform the adding
if (ListBoxSource.Add(val))
{
// DataSource needs to be a IList or IListSource, hence the conversion to List
listBox1.DataSource = ListBoxSource.ToList();
}
else
{
MessageBox.Show("Item is already in list");
}
}
You may check for duplicated item by looping through each item in the list to be compared with name of item to be added when add button is clicked:
private void addBtn_Click(object sender, EventArgs e)
{
bool similarItem = false;
if (!String.IsNullOrEmpty(itemText.Text.Trim()))
{
foreach (string listItem in itemListBox.Items)
{
if (listItem == itemText.Text)
{
MessageBox.Show("Similar item detected");
similarItem = true;
break;
}
}
if(!similarItem)
itemListBox.Items.Add(itemText.Text);
}
}
To prompt user when delete button is clicked when there is no item, the selected index will be -1, u may use that as the condition to prompt user:
private void deleteBtn_Click(object sender, EventArgs e)
{
if (itemListBox.SelectedIndex > -1)
itemListBox.Items.RemoveAt(itemListBox.SelectedIndex);
else
MessageBox.Show("No item exist in the list box, operation fail");
}
I have two ListBoxes. I want to copy SelectedItem from the first ListBox into Second one.
Why this code does not work ?
private void frm_addDispatchBoard2_Load(object sender, EventArgs e)
{
using(propertiesManagementDataContext db = new propertiesManagementDataContext())
{
var Buildings = db.Buildings.Select(q => new { q.BuildingLandNumber, q.BuildingId });
listBox_allBuildings.DataSource = Buildings;
listBox_allBuildings.DisplayMember = "BuildingLandNumber";
listBox_allBuildings.ValueMember = "BuildingId";
}
}
private void btn_addBuilding_Click(object sender, EventArgs e)
{
if(listBox_allBuildings.SelectedIndex > 0)
{
listBox_selectedBuildings.Items.Add(listBox_allBuildings.SelectedItem);
}
}
The result I got:
try this I am not sure why you are looking for a Contains but if you really need that look at the difference between SelectedValue and SelectedItem
Use this code right here as a test to see if the expected value shows up in a MessageBox
string selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
MessageBox.Show(selected);
this should help you to see the values in the Listbox on the right
private void btn_addBuilding_Click(object sender, EventArgs e)
{
if(listBox_allBuildings.SelectedIndex != -1)
{
var selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
listBox_selectedBuildings.Items.Add(selected);
}
}
I have a CheckedListBox with items from a database.
When I check an item in the CheckedListBox and after that I close the form and open the form again, the item is not checked any more, i.e. the "check" has not been saved.
How can I accomplish that if I check an item and then close the form and open it again, that the item is still checked?
I tried this:
void deliveries_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < deliveries.ClbOrdersCheckDelivery.Items.Count; i++)
{
if (deliveries.ClbOrdersCheckDelivery.GetItemChecked(i) == true)
{
Properties.Settings.Default.CheckedItems = deliveries.ClbOrdersCheckDelivery.GetItemChecked(i);
}
}
}
]\
You need to write
Properties.Settings.Default.Save();
To save the settings.
Write it after the for loop.
EDIT
I tried the following code to save all checked items to settings file. It works. Please check.
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.CheckedItems = string.Empty;
foreach (var item in checkedListBox1.CheckedItems)
{
Properties.Settings.Default.CheckedItems += item + "," ;
}
Properties.Settings.Default.Save();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(Properties.Settings.Default.CheckedItems);
}
private void Form1_Load(object sender, EventArgs e)
{
var checkedItems = Properties.Settings.Default.CheckedItems.ToString().Split(',');
foreach (var item in checkedItems)
{
var index=checkedListBox1.FindString(item);
if(index>=0)
{
checkedListBox1.SetItemChecked(index, true);
}
}
}
I have been working on this project for a few days, it’s a C# Windows Visual Studio 2010 form and I have been posting different questions that relate to the same project; as I was told to post different questions instead on having them all in the same post. So this is the project: create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. After the user clicks a button, display "Hello" in the selected font and size.
This time I’m having a problem getting the message in the textbox to display according to the font type and size that the user selected. Here is where I’m at in the coding:
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
remove this method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
in the button_click event of your button, add this :
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
you might want to remove the selectedindexchanged methods in your code if you are going to use a button tho. depends on what you want.
edit:
public Form2()
{
InitializeComponent();
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
if you just use the above code everything should work as you want it to. I tried it out myself and it's working fine for me
This was my final submission. Thanks for all of the advice guys.
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
As the titles suggest I am interested in obtaining the value from 4 combo boxes. All 4 combo boxes are the same in the that they list numbers from 0-9. I would like to take those numbers and assigning them to a single string variable. So for example if users selects (CB1 = 4)(CB2 = 3) (CB3 = 2) (CB4 = 1) I would like to take those selection and assign them to a string variable. Thanks in Advance.
-Nogard
if you are using winforms
string s"";
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox2_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox3_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox4_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
or call only a selected index change event in all comboboxes since all are doing same