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. ListBox.PreferredSize.Width interesting event

ListBox.PreferredSize.Width interesting event

Scheduled Pinned Locked Moved C#
helpquestion
2 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.
  • M Offline
    M Offline
    msn92
    wrote on last edited by
    #1

    Hello I think I've discovered some kind of bug here: I have a ListBox control. I fill its content through DataSource property. First time I set its width equal to ListBox.PreferredSize.Width, no matter what the real preferred width is, it is set to 120. From the second time use of PreferredSize.Width it works fine: the width is changed according to the content.

    class MyCtrl:UserControl
    {
    ListBox lb;
    public MyCtrl()
    {
    ...
    lb = new ListBox();
    lb.Visible = false;
    lb.Width = 50; //initially the width is set to 50
    this.Controls.Add(lb);
    ...
    }
    public ShowMyListBox(string[] data)
    {
    lb.DataSource = data;
    lb.Width = lb.PreferredSize.Width;
    //even though initially the Width is set to 50, lb.PreferredSize.Width returns 120 on the first call
    //but the following calls returns reliable preferred widths.
    lb.Visible = true;
    }
    }

    Interesting... Is it a bug? Is there any way to fix it?

    D 1 Reply Last reply
    0
    • M msn92

      Hello I think I've discovered some kind of bug here: I have a ListBox control. I fill its content through DataSource property. First time I set its width equal to ListBox.PreferredSize.Width, no matter what the real preferred width is, it is set to 120. From the second time use of PreferredSize.Width it works fine: the width is changed according to the content.

      class MyCtrl:UserControl
      {
      ListBox lb;
      public MyCtrl()
      {
      ...
      lb = new ListBox();
      lb.Visible = false;
      lb.Width = 50; //initially the width is set to 50
      this.Controls.Add(lb);
      ...
      }
      public ShowMyListBox(string[] data)
      {
      lb.DataSource = data;
      lb.Width = lb.PreferredSize.Width;
      //even though initially the Width is set to 50, lb.PreferredSize.Width returns 120 on the first call
      //but the following calls returns reliable preferred widths.
      lb.Visible = true;
      }
      }

      Interesting... Is it a bug? Is there any way to fix it?

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Until the control has been fully initialized the preferred size cannot be calculated so the default value is returned. Handling the Load event of the user control and testing PreferredSize in there gives the correct value so you may be better of doing your list box initialization in there.

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      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