listview column width at runtime


M

mp

changing a control size/location at runtime may be a bad idea
depending on other control sizes/locations
but in this case i want to size a column so i can read the entries
in this context is it a bad idea to try and get the size from the entries
themselves in some way?

is there a less amature(sp?) way to set column0.width to fit the longest
entry?
(lvCriteriaList is a listview)
(_criteriaList is a SortedDictionary<string,string>)

lvCriteriaList.Columns.Add("Criteria");
lvCriteriaList.Columns.Add("Code");
lvCriteriaList.View = View.Details;
lvCriteriaList.CheckBoxes = true;

foreach (KeyValuePair<string,string> kvp in _criteriaList)
{
int longestItem=0;
int magicNumber=18;<<<determined by trial/error during design-testing
with expected values in dict.
if (kvp.Value.Length > longestItem)
longestItem = kvp.Value.Length;

ListViewItem lvitem= lvCriteriaList.Items.Add(new
ListViewItem(kvp.Value));
lvitem.SubItems.Add(kvp.Key);
lvCriteriaList.Columns[0].Width = longestItem*magicNumber;//<<<bad i know
lvCriteriaList.Width = ???determine based on above somehow....

}
 
Ad

Advertisements

M

mp

Peter Duniho said:

sheesh, I shulda seen that :-/
I scrolled through the properties several times,
but i was looking at the column props not the listview itself-duh

interesting, now i'm trying to size the listview itself to the column
sizes...

lvCriteriaList.AutoResizeColumn(0,ColumnHeaderAutoResizeStyle.ColumnContent);
lvCriteriaList.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.HeaderSize);

int scrollbarWidth= SystemInformation.VerticalScrollBarWidth;
int borderwidth = SystemInformation.BorderSize.Width ;

//this is close but still has horiztontal scrollbar...just need to scooch it
over a tad...but how other than playing with magic number?
lvCriteriaList.Width = lvCriteriaList.Columns[0].Width +
lvCriteriaList.Columns[1].Width + scrollbarWidth + (borderwidth *2);
//this is about right but still not the right way to do it
lvCriteriaList.Width = lvCriteriaList.Columns[0].Width +
lvCriteriaList.Columns[1].Width + scrollbarWidth + (borderwidth * 2)
+5;//<<<<had to add 'magic number' bad bad bad :-(

thanks
mark
 
Ad

Advertisements

M

mp

Peter Duniho said:
[...]
lvCriteriaList.Width = lvCriteriaList.Columns[0].Width +
lvCriteriaList.Columns[1].Width + scrollbarWidth + (borderwidth * 2)
+5;//<<<<had to add 'magic number' bad bad bad :-(

Just create a new constant:

const int listViewWidthAdjustment = 5;

Then use that instead of "5". Viola, not magic any more. :)

Actually, it may be that you can get the results you want by sizing not
the entire control width, but rather the ClientSize property. For
example:

lvCriteriaList.ClientSize = new Size(
lvCriteriaList.Columns[0].Width +
lvCriteriaList.Columns[1].Width +
SystemInformation.VerticalScrollBarWidth +
SystemInformation.BorderSize.Width * 2,
lvCriteriaList.ClientSize.Height);

Or something like that. The "client" area is the area within the control
that actually displays the real content of the control, and so by sizing
it you should at least not have to worry about the metrics of parts of the
control that are just border/frame/etc.

Pete

I had tried that also but no better results, still had to use a fudge
factor.
but now i see it was before i added the borders, actually that's working now
thanks

mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

code reuse 1

Top