Solved

ListView - Problems With Sizing

Posted on 2002-06-08
22
333 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

14 Experts available now in Live!

Get 1:1 Help Now