Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Return String from a CheckListBox Control

Return String from a CheckListBox Control

Scheduled Pinned Locked Moved C#
databasehelpdata-structures
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PDTUM
    wrote on last edited by
    #1

    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++;

    L 1 Reply Last reply
    0
    • P PDTUM

      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++;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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[^]

      P 1 Reply Last reply
      0
      • L Lost User

        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[^]

        P Offline
        P Offline
        PDTUM
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • P PDTUM

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You're welcome :-D

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups