Return String from a CheckListBox Control
-
Seems simple enough, and I have looked hard and found many code sources (because some here might say this has been asked and answered), but none of them seem to work (I am showing some below...there are more). Teh primary error seems to be the following error (unable to cast object of type 'system.data.datarowview' to type 'system.string ) or something similar to this. It seems like I solved this problem a while ago by giving up on the check list box and using a list view box with the show checkbox property added, but I cannot find my original code. So, in a nutshell....I want to simply return the string values for the checked items in the control and add them to an array or arraylist (either will do). BTW...the checklistbox control was filled using an SQL query (shown also), in case that makes a difference. You guys have almost always come up with a solution of sorts, and I thank you again in advance for your great assistance and talent...Regards, Pat //Query Code
//Create an Arraylist to hold the values
ArrayList al = new ArrayList();//Open a database connection for the reader Conn.Open(); //Create the reader and execute the command adding the values to the arraylist SqlDataReader dr = Comm.ExecuteReader(); if (dr != null) while (dr.Read()) { //fill arraylist al.Add(dr\[0\]); } //Close the connection Conn.Close(); //Use the list to fill the checkbox foreach(string s in al) { checkedListBoxServices.Items.Add(s); }
//ListCheckbox code I have tried
//This one returns just the first (or last) line. I tried incrementing the row but it will not work
//foreach (DataRowView drv in checkedListBoxServices.SelectedItems)
//{
// al.Add(drv.Row[0].ToString());
//}
/////////////////////////////////These do not work at all
//int i = 0;
//int count = checkedListBoxServices.CheckedItems.Count;
//object item = string.Empty;
/////////////////////////////////
//while (i < count)
//{
// item = checkedListBoxServices.SelectedItems.ToString();
// listServices.Add(item);
// i++; -
Seems simple enough, and I have looked hard and found many code sources (because some here might say this has been asked and answered), but none of them seem to work (I am showing some below...there are more). Teh primary error seems to be the following error (unable to cast object of type 'system.data.datarowview' to type 'system.string ) or something similar to this. It seems like I solved this problem a while ago by giving up on the check list box and using a list view box with the show checkbox property added, but I cannot find my original code. So, in a nutshell....I want to simply return the string values for the checked items in the control and add them to an array or arraylist (either will do). BTW...the checklistbox control was filled using an SQL query (shown also), in case that makes a difference. You guys have almost always come up with a solution of sorts, and I thank you again in advance for your great assistance and talent...Regards, Pat //Query Code
//Create an Arraylist to hold the values
ArrayList al = new ArrayList();//Open a database connection for the reader Conn.Open(); //Create the reader and execute the command adding the values to the arraylist SqlDataReader dr = Comm.ExecuteReader(); if (dr != null) while (dr.Read()) { //fill arraylist al.Add(dr\[0\]); } //Close the connection Conn.Close(); //Use the list to fill the checkbox foreach(string s in al) { checkedListBoxServices.Items.Add(s); }
//ListCheckbox code I have tried
//This one returns just the first (or last) line. I tried incrementing the row but it will not work
//foreach (DataRowView drv in checkedListBoxServices.SelectedItems)
//{
// al.Add(drv.Row[0].ToString());
//}
/////////////////////////////////These do not work at all
//int i = 0;
//int count = checkedListBoxServices.CheckedItems.Count;
//object item = string.Empty;
/////////////////////////////////
//while (i < count)
//{
// item = checkedListBoxServices.SelectedItems.ToString();
// listServices.Add(item);
// i++;using System;
using System.Windows.Forms;
using System.Collections.Generic;namespace test
{
class Program
{
public static void Main(string[] args)
{
using (var f = new Form())
{
var tb = new TextBox()
{
Multiline = true,
Dock = DockStyle.Top,
Height = 50
};var clb = new CheckedListBox() { Dock = DockStyle.Fill }; clb.Items.AddRange(new string\[\] { "One", "Two", "A half" }); clb.SetItemChecked(0, true); clb.SetItemChecked(2, true); f.Controls.AddRange(new Control\[\] { clb, tb }); foreach (var element in clb.CheckedItems) { tb.Text += ((string)element) + Environment.NewLine; } f.ShowDialog(); } } }
}
"CheckedItems" will return the objects that are checked. If you throw strings in there, then it's strings that it returns. Otherwise, it'll return the object, and you'd prolly need to call the "ToString" method on that object or build a string of it's properties.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
using System;
using System.Windows.Forms;
using System.Collections.Generic;namespace test
{
class Program
{
public static void Main(string[] args)
{
using (var f = new Form())
{
var tb = new TextBox()
{
Multiline = true,
Dock = DockStyle.Top,
Height = 50
};var clb = new CheckedListBox() { Dock = DockStyle.Fill }; clb.Items.AddRange(new string\[\] { "One", "Two", "A half" }); clb.SetItemChecked(0, true); clb.SetItemChecked(2, true); f.Controls.AddRange(new Control\[\] { clb, tb }); foreach (var element in clb.CheckedItems) { tb.Text += ((string)element) + Environment.NewLine; } f.ShowDialog(); } } }
}
"CheckedItems" will return the objects that are checked. If you throw strings in there, then it's strings that it returns. Otherwise, it'll return the object, and you'd prolly need to call the "ToString" method on that object or build a string of it's properties.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
YES YES YES YES YES YES YES YES ........... Thank You Eddy...VERY Much...I'd be too embarrassed to tell you how much time I spent of this yesterday. The following is the actual code that I extracted from your sample to resolve this:
foreach (var element in checkedListBoxServices.CheckedItems) { textBox1.Text += ((string)element) + Environment.NewLine; }
It seems that your use of the new context (var) replacing my use of string and then casting for the result later with (string) makes a big difference ... I need to look at this again to really understand it, but I know that I can thanks to you. Much appreciation and thanks...Best Regards, Pat ps: after searching for this yesterday, and finding 'solutions' that were a page long and STILL did not work, I am sure that hundreds of others will be wanting the thank you as well for a concise usable solution... :) Pat
-
YES YES YES YES YES YES YES YES ........... Thank You Eddy...VERY Much...I'd be too embarrassed to tell you how much time I spent of this yesterday. The following is the actual code that I extracted from your sample to resolve this:
foreach (var element in checkedListBoxServices.CheckedItems) { textBox1.Text += ((string)element) + Environment.NewLine; }
It seems that your use of the new context (var) replacing my use of string and then casting for the result later with (string) makes a big difference ... I need to look at this again to really understand it, but I know that I can thanks to you. Much appreciation and thanks...Best Regards, Pat ps: after searching for this yesterday, and finding 'solutions' that were a page long and STILL did not work, I am sure that hundreds of others will be wanting the thank you as well for a concise usable solution... :) Pat