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. How to see the [Items] property of the Listbox that is inside UserControl

How to see the [Items] property of the Listbox that is inside UserControl

Scheduled Pinned Locked Moved C#
tutorialquestion
14 Posts 5 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.
  • _ Offline
    _ Offline
    _Q12_
    wrote on last edited by
    #1

    I made a custom UserControl and in it i put a Listbox. In Form1, in the properties of the UserControl I want to see the [Items] property of the Listbox that is inside UserControl. How to do that? thanks.

    R 1 Reply Last reply
    0
    • _ _Q12_

      I made a custom UserControl and in it i put a Listbox. In Form1, in the properties of the UserControl I want to see the [Items] property of the Listbox that is inside UserControl. How to do that? thanks.

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Create the property yourself, and delegate the get/set methods to the property on the listbox.

      Regards, Rob Philpott.

      _ 1 Reply Last reply
      0
      • R Rob Philpott

        Create the property yourself, and delegate the get/set methods to the property on the listbox.

        Regards, Rob Philpott.

        _ Offline
        _ Offline
        _Q12_
        wrote on last edited by
        #3

        If I was knowing how to make it, i did not cry for help. give me an example please.

        R 1 Reply Last reply
        0
        • _ _Q12_

          If I was knowing how to make it, i did not cry for help. give me an example please.

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          Ok something like (in your user control):

          public ListBox.Collection Items
          {
          get { return listbox1.Items; }
          set { listbox1.Items = value; }
          }

          ... (edit) although you probably don't want the setter thinking about it..

          Regards, Rob Philpott.

          _ 1 Reply Last reply
          0
          • R Rob Philpott

            Ok something like (in your user control):

            public ListBox.Collection Items
            {
            get { return listbox1.Items; }
            set { listbox1.Items = value; }
            }

            ... (edit) although you probably don't want the setter thinking about it..

            Regards, Rob Philpott.

            _ Offline
            _ Offline
            _Q12_
            wrote on last edited by
            #5

            ListBox.Collection does not exist! I found this but with a little problem:

            public ListBox.ObjectCollection Items
            {
            get { return listBox1.Items; }
            set { listBox1.Items = value; }//Error--System.Windows.Forms.ListBox.Items' cannot be assigned to -- it is read only
            }

            R 1 Reply Last reply
            0
            • _ _Q12_

              ListBox.Collection does not exist! I found this but with a little problem:

              public ListBox.ObjectCollection Items
              {
              get { return listBox1.Items; }
              set { listBox1.Items = value; }//Error--System.Windows.Forms.ListBox.Items' cannot be assigned to -- it is read only
              }

              R Offline
              R Offline
              Rob Philpott
              wrote on last edited by
              #6

              yeah, I edited the original - you can't set it, so just forget the second line and you should be there.

              Regards, Rob Philpott.

              _ 2 Replies Last reply
              0
              • R Rob Philpott

                yeah, I edited the original - you can't set it, so just forget the second line and you should be there.

                Regards, Rob Philpott.

                _ Offline
                _ Offline
                _Q12_
                wrote on last edited by
                #7

                I figure it out and it works:

                public ListBox.ObjectCollection Items
                {
                get { return listBox1.Items; }
                }

                But when i open Items property from the Property panel, a window appear (Object Collection Editor) and it let me add and remove items but with no capability of editing those items... how to solve that? I observe that when i open Items property for the actual listBox1 another window appear (String Collection Editor). How to add the (String...) instead of (Object...)? The ListBox have only these classes available: ___(and no String Collection)

                    //ControlAccessibleObject
                    //ControlCollection
                    //IntegerCollection
                    //ObjectCollection
                    //SelectedIndexCollection
                    //SelectedObjectCollection
                

                modified on Tuesday, June 21, 2011 8:59 AM

                B 1 Reply Last reply
                0
                • R Rob Philpott

                  yeah, I edited the original - you can't set it, so just forget the second line and you should be there.

                  Regards, Rob Philpott.

                  _ Offline
                  _ Offline
                  _Q12_
                  wrote on last edited by
                  #8

                  in Form1 I wrote this and is working all right: (BUT...I want to be able to add items not only programmatic, but from properties panel too.)

                  private void Form1_Load(object sender, EventArgs e)
                  {
                  StreamReader sr = new StreamReader(path + "\\Sessions.txt");
                  RichTextBox rtb = new RichTextBox();
                  rtb.Text = sr.ReadToEnd();

                          for (int i = 0; i < rtb.Lines.Length-1; i++)
                          {
                              session1.listBox1.Items.Add(rtb.Lines\[i\]);//session1 is my UserControl
                              //session1.aItems.Add(rtb.Lines\[i\]);   // this is working too
                          }
                          sr.Close();
                      }
                  
                  V 1 Reply Last reply
                  0
                  • _ _Q12_

                    in Form1 I wrote this and is working all right: (BUT...I want to be able to add items not only programmatic, but from properties panel too.)

                    private void Form1_Load(object sender, EventArgs e)
                    {
                    StreamReader sr = new StreamReader(path + "\\Sessions.txt");
                    RichTextBox rtb = new RichTextBox();
                    rtb.Text = sr.ReadToEnd();

                            for (int i = 0; i < rtb.Lines.Length-1; i++)
                            {
                                session1.listBox1.Items.Add(rtb.Lines\[i\]);//session1 is my UserControl
                                //session1.aItems.Add(rtb.Lines\[i\]);   // this is working too
                            }
                            sr.Close();
                        }
                    
                    V Offline
                    V Offline
                    V 0
                    wrote on last edited by
                    #9

                    you don't need the richttextbox, you should be able to iterate through the lines of the StreamReader (ReadLine method)

                    V.

                    _ 1 Reply Last reply
                    0
                    • V V 0

                      you don't need the richttextbox, you should be able to iterate through the lines of the StreamReader (ReadLine method)

                      V.

                      _ Offline
                      _ Offline
                      _Q12_
                      wrote on last edited by
                      #10

                      "you don't need the richtextbox"" And how do I take the lines from file (here:[for (int i = 0; i < rtb.Lines.Length-1; i++)]?

                      P 1 Reply Last reply
                      0
                      • _ _Q12_

                        "you don't need the richtextbox"" And how do I take the lines from file (here:[for (int i = 0; i < rtb.Lines.Length-1; i++)]?

                        P Offline
                        P Offline
                        phil o
                        wrote on last edited by
                        #11

                        Just take a look at the documentation of StreamReader ; there should be a ReadLine() method. Well, here's the link : StreamReader.ReadLine()

                        _ 1 Reply Last reply
                        0
                        • P phil o

                          Just take a look at the documentation of StreamReader ; there should be a ReadLine() method. Well, here's the link : StreamReader.ReadLine()

                          _ Offline
                          _ Offline
                          _Q12_
                          wrote on last edited by
                          #12

                          thx (with the while will do nice :)|) But my first problem still is in debate :)

                          P 1 Reply Last reply
                          0
                          • _ _Q12_

                            thx (with the while will do nice :)|) But my first problem still is in debate :)

                            P Offline
                            P Offline
                            phil o
                            wrote on last edited by
                            #13

                            Sorry, I don't know how to customize the collection editor for your needs. This should be possible as there are some attributes that you can add to your USerControl that will let you customize their behavior. Here's another link that could help you get the point Hope this points you in the right direction.

                            1 Reply Last reply
                            0
                            • _ _Q12_

                              I figure it out and it works:

                              public ListBox.ObjectCollection Items
                              {
                              get { return listBox1.Items; }
                              }

                              But when i open Items property from the Property panel, a window appear (Object Collection Editor) and it let me add and remove items but with no capability of editing those items... how to solve that? I observe that when i open Items property for the actual listBox1 another window appear (String Collection Editor). How to add the (String...) instead of (Object...)? The ListBox have only these classes available: ___(and no String Collection)

                                  //ControlAccessibleObject
                                  //ControlCollection
                                  //IntegerCollection
                                  //ObjectCollection
                                  //SelectedIndexCollection
                                  //SelectedObjectCollection
                              

                              modified on Tuesday, June 21, 2011 8:59 AM

                              B Offline
                              B Offline
                              BobJanova
                              wrote on last edited by
                              #14

                              Set an Editor attribute

                              [Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")]

                              (lifted from here[^])

                              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