Link to home
Start Free TrialLog in
Avatar of larockd
larockd

asked on

ListView - Problems With Sizing

I have a ListView that contains eight columns.  When I maximize the form a magic 9th column appears (with no header label).  

The behavior I want is for my 8th column to extend over or even if all the columns resize equally to fill up the resized form.  

How do I stop the phantom 9th column from appearing.  What
can I do to make the last column I define extend the full distance instead of it creating another column?

Here is my basic setup so far

//Create The ListView
ListView listview = new ListView() ;
listview.Parent = this ;
listview.Dock = DockStyle.Fill ;
listview.View = View.Details ;
listview.GridLines = true ;
listview.FullRowSelect = true ;

//Get Configuration Data
    Configuration myConfiguration = new Configuration() ;

int iNumItems = myConfiguration.GetNumSites() ;
string[] strSites = myConfiguration.GetSites() ;


// Define Columns
Graphics grfx = CreateGraphics() ;

SizeF sizef = grfx.MeasureString(" ", Font ) ;
cxSiteColumn = sizef.Width + MaxWidth( strSites, grfx, Font ) ;


listview.Columns.Add( "Column1",(int)Math.Ceiling( cxSiteColumn ),
HorizontalAlignment.Left ) ;
listview.Columns.Add( "Col2", -2, HorizontalAlignment.Left ) ;
listview.Columns.Add( "Col3", -2, HorizontalAlignment.Left ) ;
listview.Columns.Add( "Col4", -2, HorizontalAlignment.Left ) ;
listview.Columns.Add( "Col5", -2, HorizontalAlignment.Left ) ;
listview.Columns.Add( "Col6", -2, HorizontalAlignment.Left ) ;
listview.Columns.Add( "Col7", -2, HorizontalAlignment.Left ) ;
listview.Columns.Add( "Col8" , -2, HorizontalAlignment.Left ) ;

grfx.Dispose() ;

//Initialize List View


for ( int i = 0 ; i < iNumItems ; i++ )
{
   ListViewItem lvi = new ListViewItem( strSites[ i ] ) ;
   listview.Items.Add( lvi ) ;
}

thanks
Avatar of CJ_S
CJ_S
Flag of Netherlands image

*smiles* That is not a ninth column. That is the default ending. Since you have set the width parameter to -2 you are setting the width of the column to the width of the header's text. This means that all your columns (except the first) are as width as the header.

After saying this the listview will expand itself in width when resized (the width of your columns stay the same). Since the listview itself is as big as the form you are working on it will add another column (which does not work that way) to your listview.

This is default behaviour.

CJ
Here's what I would do in your case:

Add the following code to your application:

          public void listviewresize(object sender, EventArgs e)
          {
               // ** check for visible column
               try
               {
                    if(listview.Columns[7].Text.Length>0)
                    {
                         // ** get total width
                         int iWidth = listview.Width;
                         int iLeft=0;
                         for(int i=0; i<7;i++)
                              iLeft += listview.Columns[i].Width;
                    listview.Columns[7].Width=iWidth-iLeft-4;
                    }
                         
               }
               catch (Exception b)
               {
                   
               }
          }

Now, make your listview global. So instead of declaring it within your function you take the line:
ListView listview = new ListView();
out and add it to the top. Make it there:
private ListView listview = new ListView() ;

Now in your initialization of the Listview you make it:
               listview.Parent = this ;
               
               listview.Dock = DockStyle.Fill ;
               listview.View = View.Details ;
               listview.GridLines = true ;
               listview.FullRowSelect = true ;
               listview.Resize += new EventHandler(listviewresize);

regards,
CJ
Avatar of larockd
larockd

ASKER

Avatar of larockd

ASKER

Avatar of larockd

ASKER

Avatar of larockd

ASKER

Avatar of larockd

ASKER

Sorry for so many "no text" posts, but it keeps telling me database error.  Maybe My Post is too long.  I will try and break it up.

I tried your comment out and it appears to not react well in my test.  What happens is when

the app loads the last column is the only column that is displayed.

Quick testing I found that the following line throws it off from my app.

myListTest.ClientSize = new System.Drawing.Size(500, 300);

Not sure why?

I have another handler that is the same name as your handler but I added a "1" at the end

of it.  It only contains a width for column 7 to -2.  This seems to work for the most part

until you start resizing the application down (scroll bars pop out)

Anythoughts?

Thanks
Darrell
Avatar of larockd

ASKER

using System;
using System.Drawing ;
using System.Windows.Forms;


   class ListTest : Form
   {
      //Declare Variables
       
       //Controls
       ListView listview ;
       
      public ListTest()
       {
         
          //Create The ListView
          listview = new ListView() ;
          listview.Parent = this ;
          listview.Dock = DockStyle.Top ;
          listview.View = View.Details ;
          listview.GridLines = true ;
          listview.FullRowSelect = true ;
         
         
          // Define Columns
         
          listview.Columns.Add( "Col1",-2, HorizontalAlignment.Left ) ;
          listview.Columns.Add( "Column2", -2, HorizontalAlignment.Left ) ;
          listview.Columns.Add( "Col3", -2, HorizontalAlignment.Left ) ;
          listview.Columns.Add( "Another", -2, HorizontalAlignment.Left ) ;
          listview.Columns.Add( "Test Col", -2, HorizontalAlignment.Left ) ;
          listview.Columns.Add( "Col6", -2, HorizontalAlignment.Left ) ;
          listview.Columns.Add( "Col7", -2, HorizontalAlignment.Left ) ;
          listview.Columns.Add( "Status" , -2, HorizontalAlignment.Left ) ;
         
          //Initialize List View
          string[] strSites = new string[] { "Location1", "Location2", "Loc3",

"Loc4", "Loc5" } ;
         
         
          for ( int i = 0 ; i < 5 ; i++ )
          {
             ListViewItem lvi = new ListViewItem( strSites[ i ] ) ;
             lvi.SubItems.Add("14542") ;
             lvi.SubItems.Add("3.45") ;
             lvi.SubItems.Add("45452.34") ;
             lvi.SubItems.Add("0.45") ;
             lvi.SubItems.Add("6.76") ;
             lvi.SubItems.Add("4:54 PM") ;
             lvi.SubItems.Add("OK") ;
             listview.Items.Add( lvi ) ;
          }
         
          //Add EventHandler
          listview.Resize += new EventHandler(listviewresize);
               
       } // End of constructor
Avatar of larockd

ASKER

      //Event Handlers
       
         public void listviewresize(object sender, EventArgs e)
         {
              // ** check for visible column
              try
              {
                   if(listview.Columns[7].Text.Length>0)
                   {
                        // ** get total width
                        int iWidth = listview.Width;
                              Console.WriteLine ("iWidth:=" + iWidth );
                        int iLeft=0;
                        for(int i=0; i<7;i++)
                              {
                             iLeft += listview.Columns[i].Width;
                                    Console.WriteLine ("iLeft:" +

iLeft) ;
                              }
                   listview.Columns[7].Width = iWidth - iLeft - 4 ;
                   }
                       
              }
              catch (Exception b)
              {
                   
              }
         }
           
                    public void listviewresize1(object sender, EventArgs e)
         {

                   listview.Columns[7].Width = -2 ;
         }



      static void Main(string[] args)
       {
          ListTest myListTest = new ListTest() ;
           myListTest.ClientSize = new System.Drawing.Size(500, 300);
           Application.Run( myListTest ) ;
       }
     }
The weird thing with this listview is that you need to reset the width after you have added your items (just found that out). So, after your initialization 9with data) you add the following loop:

for(int i=0;i<7;i++)
   listview.Columns[i].Width=-2;

This solved the problem you are having.
Testing input, since an Expert attempting to add helpful comments received errors on access.

Moondancer - EE Moderator
Avatar of larockd

ASKER

CJ

There is still one thing that I find abnormal.  If you maximize the window to full screen the listview sizes properly.  Now hit the maximize button again the form resizes back to the normal size and so does the listview but the listview scrollbars come out.  

Why did the listview scrollbars come out?

Also, with the added code from your last comment when I use your resize method and the app loads all that is displayed is the last column the full width of app.  The following line in my app seems to throw your resize handler off

myListTest.ClientSize = new System.Drawing.Size(500, 300);

dl



dl
I did some searches and found this: http://discuss.develop.com/archives/wa.exe?S2=dotnet&L=DOTNET&q=&s=bug+functionality+control+resize+withevents&f=&a=&b=

I tried the methods they've tried, but they failed. Size seems to stay the previous size. First it seems alright, but then it gets set back.

I can't seem to fix it :-( Sorry.

The second problem you are having is something I do not encounter in the test application I've built. Where does that line appear? Adding that line gives me a full screen with all the columns, when resized there's no scrollbar ever.

:-/

Weird problem. I'll see if I can do anything about it.

CJ
Avatar of larockd

ASKER

CJ,

Can you post your test app.  I would love to try it here and see what the difference between the two is.

Darrell
         public void ListTest()
          {
         
               //Create The ListView
               listview = new LVControl() ;
               listview.Parent = this ;
               listview.Dock = DockStyle.Top ;
               listview.View = View.Details ;
               listview.GridLines = true ;
               listview.FullRowSelect = true ;
         
               // Define Columns
         
               listview.Columns.Add( "Col1",-1, HorizontalAlignment.Left ) ;
               listview.Columns.Add( "Column2", -1, HorizontalAlignment.Left ) ;
               listview.Columns.Add( "Col3", -2, HorizontalAlignment.Left ) ;
               listview.Columns.Add( "Another", -2, HorizontalAlignment.Left ) ;
               listview.Columns.Add( "Test Col", -2, HorizontalAlignment.Left ) ;
               listview.Columns.Add( "Col6", -2, HorizontalAlignment.Left ) ;
               listview.Columns.Add( "Col7", -2, HorizontalAlignment.Left ) ;
               listview.Columns.Add( "Status" , -2, HorizontalAlignment.Left ) ;
         
               //Initialize List View
               string[] strSites = new string[] { "Location1", "Location2", "Loc3", "Loc4", "Loc5" } ;
         
         
               for ( int i = 0 ; i < 5 ; i++ )
               {
                    ListViewItem lvi = new ListViewItem( strSites[ i ] ) ;
                    lvi.SubItems.Add("14542") ;
                    lvi.SubItems.Add("3.45") ;
                    lvi.SubItems.Add("45452.34") ;
                    lvi.SubItems.Add("0.45") ;
                    lvi.SubItems.Add("6.76") ;
                    lvi.SubItems.Add("4:54 PM") ;
                    lvi.SubItems.Add("OK") ;
                    listview.Items.Add( lvi ) ;
               }

               //listview.ClientSize = new Size(200,300);
               for(int i=0;i<8;i++)
                    listview.Columns[i].Width=-2;
         
             
          } // End of constructor
private ListView listview = new LVControl() ;
    public class LVControl : System.Windows.Forms.ListView
     {
          public LVControl() : base()
          {
               
          }

          protected override void OnResize(EventArgs e)
          {
               base.OnResize(e);
               int iWidth = 0;
               for(int i=0;i<this.Columns.Count-1;i++)
               {
                    iWidth += this.Columns[i].Width;
               }
               try
               {
                    if(this.Columns[7].Text.Length>0 && base.Size.Width>iWidth) this.Columns[this.Columns.Count-1].Width = base.Size.Width-iWidth-4;
               }
               catch(Exception b)
               {
                    Console.Write(b.ToString());
               }
          }
     }
learning..
Avatar of larockd

ASKER

Learning - Your exactly right..  Unfortunanlty, through all the testing and help I have been given there is no real easy way to control the listview..  

Cannot make it work properly. the above is as far as I could get. i think this new ListView contains some bugs.

CJ
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
PAQ/Refund
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheAvenger
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial