C# Select multiple values listbox - c#

I've a Listbox but when I say :
txtSelectedTables.Text += lbxTafels.GetItemText(lbxTafels.SelectedValue);
It only shows one thing in my textbox (even though I've selected multiple rows). So if I want to select multiple rows it doesn't put the values in txtSelectedTables.Text (it only shows one item).
So how can I select multiple rows and show it in a textbox.

Like RadioSpace mentioned you probably want to look at the selectedItems property
Here is an example
foreach (var item in lbxTafels.SelectedItems)
{
txtSelectedTables.Text += item.ToString();
}
Here is my full test function. If you are seeing a DataViewRow type name in the text box then maybe you have more going on than a ListBox and TextBox.
ListBox lbxTafels = new ListBox();
System.Windows.Forms.TextBox txtSelectedTables = new TextBox();
//Add 2 items
lbxTafels.Items.Add("Hello");
lbxTafels.Items.Add("World");
//Select both items
lbxTafels.SetSelected(0,true);
lbxTafels.SetSelected(1, true);
//Set the textbox
foreach (var item in lbxTafels.SelectedItems)
{
txtSelectedTables.Text += item.ToString();
}
Console.WriteLine(txtSelectedTables.Text);
The above function prints out
HelloWorld

Related

how to print the checked items from the checked listbox to a label in c#?

I'm Trying to fetch items from checked listbox when the user checks the listbox item it should be displayed in a label on a button click. I tried using this:
foreach (object item in checkedlistbox1.CheckedItems)
{
labelto.Text += checkedlistbox1.SelectedItem.ToString();
}
But I'm getting this exception:
List that this enumerator is bound to has been found, enumerator can only be used if the list does not change.
How to print the checked items from the checked listbox to a label?
This is not a complicated task, why don't you make try with the following:
string displayText = "";
foreach(object item in checkedListBox1.CheckedItems)
{
DataRowView castedItem = item as DataRowView;
displayText += castedItem["boundPropertyNameHere"];
}
labelto.Text = displayText;
Please note:
Where boundPropertyNameHere be the name of property that used to bind the collection.

Display selectedvalues of listbox as label - multiple values

I have got a list box called lstPTLNameDHOD which has multiple PTL names which gets selected using Ctrl. I want to display the selected names in a label or some way that the person submitted the form can see who exactly they are submitting it for.
My problem is I can only get one name to display on the label.
// Items collection
foreach (ListItem item in lstPTLNameDHOD.Items)
{
if (item.Selected)
{
lbl1stPTL.Text = item.Value.ToString();
}
}
This is being called on post back on the reason dropdown being changed.
You are only getting one name to display because your current code would always display name of the last item that is selected.
I don't have a visual studio handy but you could try this:
StringBuilder sbText = new StringBuilder();
// Items collection
foreach (ListItem item in lstPTLNameDHOD.Items)
{
if (item.Selected)
{
lbl1stPTL.Text = sbText.Append(item.Value.ToString()).ToString();
}
}
You can probably refine this by adding a space or a comma between two item names but I hope you get the idea and it helps!
Again, sorry for not having VS handy!

How to store list of items temporarily?

I am trying to store a list of items i.e. a value selected from dropdown and text from textbox temporarily and then adding them 1 by one to database using foreach loop on listbox.
code behind button add event:
ListBox ListBoxFeatures = new ListBox();
ListBoxFeatures.Items.Add(new ListItem(txtBoxDescription.Text, ddlFeatures.SelectedValue));
and then i have used a foreach loop over it to grab all stored values and store in database but it always pick 1 row, means that i store only 1 row.
foreach(ListItem li in ListBoxFeatures.Items)
{
String txt= li.Text;
Int Value= li.SelectedValue.ToInt32();
//database logic i.e. InsertMethod
}
Your problem is you create a new listbox in each click event. You need to move the initialization outside of the button click. Assuming that ListBoxFeatures already exists before the button click, you can probably remove the whole line of ListBox ListBoxFeatures = new ListBox(); from your click method.
Shouldn't it be:
foreach(ListItem li in ListBoxFeatures.Items)
{ ... }
UPDATE
Also, the button event always creates a new listbox and then inserts 1 value to it... Why would you have more than one value?

Cannot Controls.Find a ComboBox inside a GroupBox

On my Visual C# Form application, I have a combobox inside a groupbox to help organize / look neat. However, once I put the combobox inside the groupbox, I am no longer able to find it by looping through all of the controls on my form.
For example, if I run this code with the Combobox inside the Groupbox I get a different result than if its outside the group box:
foreach (Control contrl in this.Controls)
{
richTextBox1.Text += "\n" + contrl.Name;
}
If the combobox is inside the groupbox, it won't find it.
I also noticed in the Form1.Designer.cs file that whenever I add the combobox inside the groupbox, the following line of code appears to the groupbox:
this.groupBox4.Controls.Add(this.myComboBox);
..
this.groupBox4.Location = new System.Drawing.Point(23, 39);
this.groupBox4.Name = "groupBox4";
... etc...
And this line will be removed:
this.Controls.Add(this.myComboBox);
If I try to edit it manually, it automatically switches back once I move the combobox back inside the groupbox.
Any help would be appreciated! Thanks!
Brian
As you said, you added combo box to group box, so it is added to Controls collection of group box and the designer generates this code:
this.groupBox4.Controls.Add(this.myComboBox);
So if you want to find the combo box programmatically, you can use this options:
Why not simply use: this.myComboBox ?
Use var combo = (ComboBox)this.Controls.Find("myComboBox", true).FirstOrDefault();
Use var combo = (ComboBox)this.groupBox4.Controls["myComboBox"]
Also if you want too loop, you should loop over this.groupBox4.Controls using:
foreach(Control c in this.groupBox4.Controls) {/*use c here */}
this.groupBox4.Controls.Cast<Control>().ToList().ForEach(c=>{/*use c here */})
Just like the Form object, the Group object can hold a collection of controls. You would need to iterate through the Group control's controls collection.
One more idea for getting at all or one ComboBox in a GroupBox, in this case groupBox1. Granted Resa's suggestion for using Find with FirstOrDefault would be best to access one combobox.
List<ComboBox> ComboBoxes = groupBox1
.Controls
.OfType<ComboBox>()
.Select((control) => control).ToList();
foreach (var c in ComboBoxes)
{
Console.WriteLine(c.Name);
}
string nameOfComboBox = "comboBox1";
ComboBox findThis = groupBox1
.Controls
.OfType<ComboBox>()
.Select((control) => control)
.Where(control => control.Name == nameOfComboBox)
.FirstOrDefault();
if (findThis != null)
{
Console.WriteLine(findThis.Text);
}
else
{
Console.WriteLine("Not found");
}
You can use the ControlCollections Find Method, it has a parameter that will search the parent and its Children for your control.
ComboBox temp;
Control[] myControls = Controls.Find("myComboBox", true); //note the method returns an array of matches
if (myControls.Length > 0) //Check that it returned a match
temp = (ComboBox)myControls[0]; //use it

how to get data of check box list with text boxes

I try to get data from the textboxex that is related to the ckeckboxex in the checkboxlist ..
in order to calculate the value of the selected box
this is my code :
foreach (ListItem item in listOthers.Items)
{
if (item.Selected)
{
sum += Convert.ToInt32(TextBox1.Text);//textbox of selected checkbox
}
}
lblSum.Text = sum.ToString();
What do I need to do to get the value of the textbox related to the checkbox?
Based on your comments, you have a CheckBoxList with a fixed size of 10 items and along with that 10 Textboxes (named TextBox1, TextBox2 and so on). The users marks some CheckBoxes and also enters a text into the corresponding Textboxes. You want to convert the Text of the Textboxes to an integer and calculate the sum of the marked entries.
You need to dynamically access the Textboxes by calling FindControl:
var index = 0;
foreach (ListItem item in listOthers.Items)
{
index++; // Increase here as the Textbox numbers start at 1
if (item.Selected)
{
var txt = FindControl("TextBox" + index.ToString()) as TextBox;
if (txt != null)
sum += Convert.ToInt32(txt.Text); //text of selected Checkbox
}
}
lblSum.Text = sum.ToString();
Please note that it is important to call FindControl on the container of the Textboxes. The call in the sample works if the Textboxes are located directly on the page. If they are located on a Panel named Panel1, you'd have to call:
var txt = Panel1.Findcontrol("TextBox" + index.ToString()) as Textbox;
An alternative to using a CheckBoxList that is prepared for variable sizes by design and having a fixed list of 10 TextBoxes next to it, would be to create a Repeater with a CheckBox and a TextBox in its ItemTemplate. Downsize is that you'd have to create some DTOs for DataBinding and also had to use some more dynamic control resolution (both for the CheckBox and the TextBox ).

Categories