Problems with text display in listbox, VB.NET.

ashgopal2000

Member
Joined
Jun 24, 2005
Messages
12
Programming Experience
1-3
Hi,



I have been designing some Window Forms with VB.NET.



I have a listbox control and a label on top to display the headers for the listbox items.



On resizing the form, the font sizes of the displayed text are not automatically resized. So, I initialize the fontsize of the texts as follows:



Label.font = new font(“Courier New”, font_size, FontStyle.Bold)

Lstbox.font = new font(“Courier New”, font_size, FontStyle.Bold)



Where font_size is calculated by another logic, which is not relevant here.



Now even though I use “Courier New” font, I get different character width in the Label and the listbox for the same font size.



A more detailed code is:



lbW = Label8.Width

lbH = Label8.Height

Label8.Font = New Font("Courier New", font_size, FontStyle.Bold)

‘get back the original size, in case the font has been resized

‘This does not affect the font size

Label8.Size = New Size(lbW, lbH)



lbW = lstAgedCase.Width

lbH = lstAgedCase.Height

lstAgedCase.Font = New Font("courier new", font_size, FontStyle.Bold)

lstAgedCase.Size = New Size(lbW, lbH)



But, when I display the font size of the listbox and the label after this statement, I find that they have the same font sizes.



I have been battling with this problem for weeks and any attempt to help on this will be highly appreciated. Is there a way to display a text of a particular character width?



Thanks
 
Very confusing (if i may) !!! Even, you gave us a lot info about your problem i think it's still unclear what are you trying to do. Now, could you concentrate on the problem and explain only your intention/idea please. Especially this one: Is there a way to display a text of a particular character width?

Cheers ;)
 
Hi,

I am sorry for the confusing reply.

All I am trying to do is to display texts of same font size in a listbox and its header on a label, as shown below:

label: Case ID Clerk Error Codes IND BR Tot Msg

listbox: 109-222-1071 xyu 2222 a 23 4
209-233-1083 QEu 2332 b 43 5

However, even though the displayed font size is the same and the font used is "Courier New", the texts displayed in the listbox are of a different character width than the ones in the label and so there is misalignment, as shown below.

label: Case ID Clerk Error Codes IND BR Tot Msg

listbox: 109-222-1071 xyu 2222 a 23 4
209-233-1083 QEu 2332 b 43 5

The code to allocate the font is:

Label.font = new font(“Courier New”, font_size, FontStyle.Bold)

Lstbox.font = new font(“Courier New”, font_size, FontStyle.Bold)


Any help will be appreciated. Thanks.
 
font resizing problem

OOPS, sorry for the confusion again...my reply came out differently in the editor and the reply!!!

Please consider the "X" below as white spaces...
Again the displayed texts are:

XXXCaseXIDXXXXClerkXXCodesXXINDXXBRXXTotMsg

X109-222-1071XXxyuXXX2222XXXXaXX23XXXXX4
X209-233-1083XXqwuXXX2332XXXXbXX43XXXXX5

and:


XXXCaseXIDXXXXClerkXXCodesXXINDXXBRXXTotMsg

109-222-1071XxyuXX2222XaX23X4
209-233-1083XQEuXX2332XbX43X5

However the problem is not only because of the whitespaces, but due to different character widths.

Also, the problem seems to be with the listbox control.

Thanks.
 
Font Resizing problem

Hi everybody,


If the above descriptions have been confusing, let me try it one last time....sorry for bothering.:D

I have been designing some WINDOW FORMS with VB.NET.



I have a LISTBOX CONTROL that displays some data and a LABEL on top to display the headers for the listbox items, which is supposed to be as shown below.




XXCaseXIDXXXXXClerkXXXXCodesXXXXINDXXBRXXXTot Msg (label:header)

109-222-1071XXXxyuXXXXX2222XXXXXXaXXX23XXXXXX4(listbox: data)
209-233-1083XXXQEuXXXXX2332XXXXXXbXXX43XXXXXX5



[X's represent whitespace, for just displaying here and are not a part of my project]

On resizing the form an appropriate font size is calculated and assigned to the two controls as follows:



Label.font = new font(“Courier New”, font_size, FontStyle.Bold)

Lstbox.font = new font(“Courier New”, font_size, FontStyle.Bold)



Where font_size is the new font size, the calculation of which is not relevant here.



But, the displayed data after resizing is misaligned with the header in label, as shown:



XXXCaseXIDXXXXXXXXClerkXXXXXCodesXXXXXINDXXXBRXXXXTotXMsg



109-222-1071XXXXxyuXXXXX2222XXXXXXaXXX23XXXXXX4

209-233-1083XXXXQEuXXXXX2332XXXXXXbXXX43XXXXXX5




  • After tracing out the problem, the misalignment has been identified due to different character widths, even though the displayed text is of Courier New font and of same font size.
  • The listbox seems to be the culprit, as the label displays the texts of same width as other controls
  • The misalignment happens either ways, i.e, the listbox text is either greater or smaller than the label text, depending on the character width is greater or smaller.
How to work-around this misallignment problem? Also, is there a way to display the characters in the listbox of the same width as in the label, as this will solve the problem?



Thanks
 
Everything is quite clear now except (one scrupule) the fact I still don't understand why you are using the Label and ListBox if you have available ListView control.

Just take a look at the attached project ... I think you will change your mind once you see ListView.


Cheers ;)
 

Attachments

  • ListViewControl.zip
    24.3 KB · Views: 47
If you want them to line up that way, then you need to use a Fixed Width Font.... Courier isn't.... it's a TrueType font.

Or use a list view, or a grid of some sort....

Tg
 
re: font resizing problem

Hi Everybody,

The Listview is a very nice option, Kulrom!! Thank you so much for your patience.

The reason why I didn't use datagrids or any other controls is because this is a porting project and the .NET code is of the same design of the original VB code. The listbox control is there everywhere in the code and so tampering it will not only require lots of changes but may also involve incompatibility issues with other COM components that is being used.

But listview seems to have almost the same properties/methods as the listbox control. I am analysing the feasibility of the replacement now and most probably this should work!!!

Thanks again Kulrom and all those who took time reviewing the Loooooong problem and thinking abt it.

TechGnome,

Just curious...I thought that Courier New was a Fixed Width Font it works properly...just that it shrinks in case of the listbox.

Can you give me some examples of Fixed Width Fonts?
 
listview performance problem

Hi Kulrom and others,


The listview is working fine. However it seems to be too slow. The thing I do with listview control is:

1. Display data
2. Then adjust the column width, by dynamically assigning the column width property of each column header.


The 2nd step is taking a heck a lot of time, and it is a big problem if the user continuously resizes it.

Please let me know if anybody has come across the problem and has a workaround.

Thanks.
 
Hi Kulrom,

I can post the code for populating the list. However, this may not be relevant. When the user resizes the screen, the listview is not repopulated, only resized and arranged for fitting the column size. The resizing does not take any time.

However, when I try to dynamically adjust the column width like:

lstablist.Columns(0).Width = sz.Width

lstablist.Columns(1).Width = sz.Width

etc. the width size is perfect, but it takes lots of time to adjust the listview again, when the codes above are executed.

Thanks.
 
ashgopal2000 said:
Hi Kulrom,

I can post the code for populating the list. However, this may not be relevant. When the user resizes the screen, the listview is not repopulated, only resized and arranged for fitting the column size. The resizing does not take any time.

However, when I try to dynamically adjust the column width like:

lstablist.Columns(0).Width = sz.Width

lstablist.Columns(1).Width = sz.Width

etc. the width size is perfect, but it takes lots of time to adjust the listview again, when the codes above are executed.

Thanks.
Try this, just before adjusting the column widths....
Turn off the redraw (.Redraw = False) then suspend the layout (.SuspendLayout) ... do all your column adjustments, then reenable the layout (.ResumeLayout) and reset the redraw (.Redraw = True)..... That migh help speed it up a little. What may be happening is that the ListView has to be redrawn everytime there is a colwidth change. By turning off the Redraw and suspending the layout, the list won't (shouldn't) be redrawn until the very end when the redraw is turned back on.

Tg
 
RE: listview problem...

Techgnome,

The solution works great! Yes, the speed improved a lots.

Also, could you suggest some fixed width fonts...just for my GK?

Thanks a lot.
 
Actualy I went back and checked..... and you are right... Courier is fixed width.... imagine my surprise.... typicaly when I needed monospaced fonts, I used System or Monotype....
mea cupla.

Tg
 
Back
Top