Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ListView - Problems With Sizing

Posted on 2002-06-08
22
Medium Priority
?
341 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:larockd
22 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 7064576
*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
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7064592
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
0
 

Author Comment

by:larockd
ID: 7064975
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:larockd
ID: 7064976
0
 

Author Comment

by:larockd
ID: 7064978
0
 

Author Comment

by:larockd
ID: 7064979
0
 

Author Comment

by:larockd
ID: 7064981
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
0
 

Author Comment

by:larockd
ID: 7064982
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
0
 

Author Comment

by:larockd
ID: 7064983
      //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 ) ;
       }
     }
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7065106
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.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7065589
Testing input, since an Expert attempting to add helpful comments received errors on access.

Moondancer - EE Moderator
0
 

Author Comment

by:larockd
ID: 7065659
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
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7068352
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
0
 

Author Comment

by:larockd
ID: 7068922
CJ,

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

Darrell
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7069205
         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
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7069206
private ListView listview = new LVControl() ;
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7069207
    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());
               }
          }
     }
0
 

Expert Comment

by:dhaya
ID: 7075995
learning..
0
 

Author Comment

by:larockd
ID: 7076042
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..  

0
 
LVL 22

Expert Comment

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

CJ
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9485045
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
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9537570
PAQed, with points refunded (200)

Computer101
E-E Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question