Solved

ListView - Problems With Sizing

Posted on 2002-06-08
22
331 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
Comment Utility
*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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:larockd
Comment Utility
0
 

Author Comment

by:larockd
Comment Utility
0
 

Author Comment

by:larockd
Comment Utility
0
 

Author Comment

by:larockd
Comment Utility
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
Comment Utility
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
Comment Utility
      //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
Comment Utility
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
Comment Utility
Testing input, since an Expert attempting to add helpful comments received errors on access.

Moondancer - EE Moderator
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:larockd
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
         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
Comment Utility
private ListView listview = new LVControl() ;
0
 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
    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
Comment Utility
learning..
0
 

Author Comment

by:larockd
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
PAQed, with points refunded (200)

Computer101
E-E Admin
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now