I have my listbox1 set to use a datasource of a list called "serverList". When I run
public void button2_Click(object sender, EventArgs e)
{
if (folderPath != "\\realmlist.wtf" && folderPath != "none")
{
serverList.Add(newServer);
listBox1.DataSource = serverList;
File.WriteAllText(folderPath, "Set realmlist " + newServer);
}
}
the first string enters and shows up in the listbox just fine but when I try to add another string to the list it wont show up in the listbox but is actually within the list. What can I do to accomplish this?
if winform app
BindingSource source=new BindingSource();
source.DataSource=serverList;
listBox1.DataSource = source;
if not you have to use listBox1.DataBind()
After assigning DataSource, you should bind the data i.e. listBox1.DataBind()
Related
I am using several comboBoxes in my app and I am struggling with the following problem.
And when I change the value of any of the boxes it changes it on others too.
Just to get you guys in the flow of the app I am gonna explain whats going on. The "Primary Muscle" box has a crucial role. If I select "Chest" there it will change the DataSource of the four boxes below with only "chest exercise". The real problem is that when I select "Bench Press" for example on the first "Primary Exercise" it will change it for all of them below that one, like its showed on the screenshots.
private void metroComboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
if ((string)primaryMuscleBox.SelectedItem == "Chest")
{
AddItems(primaryExerciesBox1, chestExercies);
AddItems(primaryExerciesBox2, chestExercies);
AddItems(primaryExerciesBox3, chestExercies);
AddItems(primaryExerciesBox4, chestExercies);
}
else if ((string)primaryMuscleBox.SelectedItem == "Back")
{
AddItems(primaryExerciesBox1, backExercies);
AddItems(primaryExerciesBox2, backExercies);
AddItems(primaryExerciesBox3, backExercies);
AddItems(primaryExerciesBox4, backExercies);
}
else if ((string)primaryMuscleBox.SelectedItem == "Legs")
{
AddItems(primaryExerciesBox1, legsExercies);
AddItems(primaryExerciesBox2, legsExercies);
AddItems(primaryExerciesBox3, legsExercies);
AddItems(primaryExerciesBox4, legsExercies);
}
}
private void AddItems(MetroComboBox comboBox, List<string> name)
{
comboBox.DataSource = null;
comboBox.DataSource = name;
}
I forgot to mention that I am not using the standard comboBox, I am using the "metroComboBox".
It's because all your ComboBoxes are bound to same data source.
You have to set new BindingSource to each combo boxes:
private void AddItems(MetroComboBox comboBox, List<string> name)
{
comboBox.DataSource = new BindingSource(name, "");
}
Or you can use ToList() to create new List
private void AddItems(MetroComboBox comboBox, List<string> name)
{
comboBox.DataSource = name.ToList();
}
i want to add selected item from a data bounded listbox (listbox1) to another listbox (listbox2)
Here is the code on a click event of a button.
private void btnrgt_Click(object sender, EventArgs e)
{
string x = listBox1.SelectedItem.ToString();
listBox2.Items.Add(x.ToString());
txttestno.Text = listBox2.Items.Count.ToString();
}
When i run this code System.data.datarowview get displayed in the listbox2.
Kindly help.
thank you in advance.
When you bind a ListBox datasource to a DataTable every item inside the ListBox is a DataRowView, not a simple string. In the ListBox you see a string displayed because you set the DisplayMember property of the ListBox with the name of a column in that DataRowView.
So, taking the current SelectedItem doesn't return a string but a DataRowView and calling ToString() for a DataRowView returns the full qualified name of the class (System.Data.DataRowView).
You need something like this
private void btnrgt_Click(object sender, EventArgs e)
{
DataRowView x = listBox1.SelectedItem as DataRowView;
if ( x != null)
{
listBox2.Items.Add(x["NameOfTheColumnDisplayed"].ToString());
txttestno.Text = listBox2.Items.Count.ToString();
}
}
EDIT
It is not clear what is the source of the error stated in your comment below, however you could try to avoid adding an item from the first listbox to the second one if that item exists in the second listbox with code like this
private void btnrgt_Click(object sender, EventArgs e)
{
DataRowView x = listBox1.SelectedItem as DataRowView;
if ( x != null)
{
string source = x"NameOfTheColumnDisplayed".ToString();
if(!listbox2.Items.Cast<string>().Any(x => x == source))
{
listbox2.Items.Add(source);
txttestno.Text = listBox2.Items.Count.ToString();
}
}
}
This solutions works if your second listbox is really populated adding simple strings to its Items collection.
On click button use below code.
protected void btnGo_Click(object sender,EventArgs e) {
string x = ListBox1.SelectedItem.Text;
ListBox2.Items.Add(x);
}
See I have a HashSet with several values, this values can contain for example numbers like 4141234567, 4241234567, 4261234567 and so on. I put a radioButton1 in my UserControl and I want when I click this just the numbers with 414 and 424 remains on my ListBox, for that I wrote this code:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
var bdHashSet = new HashSet<string>(bd);
if (openFileDialog1.FileName.ToString() != "")
{
foreach (var item in bdHashSet)
{
if (item.Substring(1, 3) != "414" || item.Substring(1, 3) != "424")
{
listBox1.Items.Remove(item);
}
}
}
}
But when I run the code I get this error:
Items collection cannot be modified when the DataSource property is set.
What is the proper way to remove the non wanted items from the list without remove them from the HashSet? I'll add later a optionButton for numbers that begin with 0416 and 0426 and also a optionButton to fill the listBox with original values, any advice?
try
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
var bdHashSet = new HashSet<string>(bd);
listBox1.Datasource = null;
listBox1.Datasource = bdHashSet.Where(s => (s.StartsWith("414") || s.StartsWith("424"))).ToList();
}
Try this:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
var bdHashSet = new HashSet<string>(bd);
listBox1.Datasource = bdHashSet.Select(s => (s.Substring(1, 3) == "414" || s.Substring(1, 3) == "424"));
//After setting the data source, you need to bind the data
listBox1.DataBind();
}
I think that you can select the elements with linq and then reassign the listBox with the result. In that way you dont need to remove elements from the list and you can keep the elements of the HashSet.
You can use BindingSource object. Bind it with DataSource and then use the RemoveAt() method.
Try this :
DataRow dr = ((DataRowView)listBox1.SelectedItem).Row;
((DataTable)listBox1.DataSource).Rows.Remove(dr);
This is the code that was working fine until I switched the dropdownstyle to dropdownlist. Does anyone have any idea how I can rewrite the comboBox1.Text != "None" to work with the SelectedItem format?
public void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedItem = "None";
}
if (comboBox1.Text != "None")
{
checkBox1.Checked = true;
}
First, you need to have something in your combobox:
comboBox1.Items.Add("None");
comboBox1.SelectedItem = "None";
Once you do this, your if statement will work correctly.
Note that you can also add items to the combobox through the DataSource:
var cList = new List<String>();
cList.Add("One");
cList.Add("None");
comboBox1.DataSource = cList;
comboBox1.SelectedItem = "None";
if (comboBox1.Text != "None")
In this case, especially if you are binding a class object instead of a string, you will want to use the combobox's SelectedValue property instead of SelectedItem since SelectedItem will contain the actual class object.
My windows form has an ADD button which adds a combo box to the form after each click. The problem is, i am not able to bind it to a table column at run time. Using an existing databinding source selects the same value in all the combo boxes. I am coding in C#
here is the sample code :
ComboBox ocbNext = new ComboBox();
//HAVE set the rest of the properties right, the problem is with the databinding
ocbNext.DataSource = this.dummysubjectBindingSource;
ocbNext.DisplayMember = "sub_name";
ocbNext.ValueMember = "sub_name";
this.Controls.Add(ocbNext);
I added a DataSet to the solution and droped the Employees table (from Northwind) in the designer, which automatically created the employeesBindingSource. I dropped a combobox and a button on the Form and I set the DataSource and DataMember of the combo. Then I handled some events:
private void Form1_Load(object sender, EventArgs e)
{
this.employeesTableAdapter.Fill(this.dS.Employees);
}
private int _i = 0;
private void button1_Click(object sender, EventArgs e)
{
ComboBox combo = new ComboBox();
combo.DataSource = this.employeesBindingSource;
combo.DisplayMember = this.dS.Tables[0].Columns[++_i].ColumnName;
combo.Location = new Point(comboBox1.Location.X, comboBox1.Location.Y + comboBox1.Height * _i);
this.Controls.Add(combo);
}
So on each click, a new combo is added onto the form dynamically right under the previous combo. The combo is also bound to the next column in the Employees table (no boundary checks however).
As you can see, this is pretty easy stuff. Hope this helps.
Okay, so here is a variation of the code that could help you with that other question you asked in the comments of this answer.
It assumes you have a Form with a button and a DataSet with table Employees. On button click it creates a combo, and fills it with data (the Name column of Employees). Each time you add a combo, it gets its own copy of the data (this is important to be able to remove items from one combo at a time). Then, every time you select a value in the combo, the combo is disabled and the other combos don't have that selected value in their list.
private int _i = 0;
private void button1_Click(object sender, EventArgs e)
{
DataSet dataS = dS.Clone();
this.employeesTableAdapter.Fill((DS.EmployeesDataTable)dataS.Tables[0]);
BindingSource bindSource = new BindingSource(dataS, "Employees");
ComboBox combo = new ComboBox();
combo.Name = this.dS.Tables[0].Columns[0].ColumnName + (++_i).ToString();
combo.DataSource = bindSource;
combo.DisplayMember = this.dS.Tables[0].Columns[1].ColumnName; //This column is the Name of Employee
combo.Location = new Point(button1.Location.X, button1.Location.Y + combo.Height * _i);
combo.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
this.Controls.Add(combo);
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is ComboBox && ctrl != sender && ctrl.Enabled)
{
((BindingSource)((ComboBox)ctrl).DataSource).RemoveAt(((ComboBox)sender).SelectedIndex);
}
}
((ComboBox)sender).Enabled = false;
}
This is pretty close to what you require, or easily adaptable to meet your expectations. Enjoy and please select an answer as the accepted one. Thanks!
Option 1: Fill the combobox with strings:
this.comboBox1.Items.Add("Syed");
this.comboBox1.Items.Add("Baqar");
Option 2: Fill the combobox with an array of strings:
this.comboBox1.Items.AddRange(new object[] { "Syed", "Baqar" });
You need to add controls to the parent window first, and then set the data source.
ComboBox ocbNext = new ComboBox();
this.Controls.Add(ocbNext);
ocbNext.DisplayMember = "sub_name";
ocbNext.ValueMember = "sub_name";
ocbNext.DataSource = this.dummysubjectBindingSource;
Should be fine if you create a new local ComboBox variable in the clickevent. If you use a global variable for the ComboBox this might explain your problems. But without a sample how you're doing it's hard to see what's really happening, so think this is just a rough guess