Textbox help...

georgepatotk

Contributor
Joined
Mar 1, 2004
Messages
432
Location
Malaysia
Happy Chinese New Year...

I am using a textbox in my winform.

The default size of the font is 12 and the size of the textbox is 224,64.

I have thousands of records to be shown in this textbox, one by one.

I have this problem where sometimes the records are too long to fit into the textbox. Means 224,64 is not enough. Since i can't size of the textbox is fixed, so, I would like to change the font size accordingly.

I wish to do something likee this

While (it is not enough)
TextBox1.Font = New Font("Microsoft Sans Serif", TextBox1.Font.Size - 1)
End While

so, anyone could help me on this? Please..
 
Are you showing one record at a time in one textbox at a time?

Anyways, I would recommend using the System.Drawing.Graphics.MeasureString function to see if the text will fit in the box, and if it doesnt then... you can try decreasing the fontsize by one until it fits (provided that you have a point at which it will give up; it would be pointless to show your data at a size 1 font and this method may be somewhat error prone). If you absolutely can not increase the size of your textbox perhaps you can set the record text to a tool tip so that the user can place the mouse over the textbox and the record will pop up in a tooltip.
 
marble_eater said:
Are you showing one record at a time in one textbox at a time?

Anyways, I would recommend using the System.Drawing.Graphics.MeasureString function to see if the text will fit in the box, and if it doesnt then... you can try decreasing the fontsize by one until it fits (provided that you have a point at which it will give up; it would be pointless to show your data at a size 1 font and this method may be somewhat error prone). If you absolutely can not increase the size of your textbox perhaps you can set the record text to a tool tip so that the user can place the mouse over the textbox and the record will pop up in a tooltip.

Good idea for the tooltip.
and for the System.Drawing.Graphics.MeasureString, I don't really know how to use this. Can explain more to me? By the way, I will search it fhru net as well..
 
Can show me a simple example for System.Drawing.Graphics.MeasureString? Just a simple example is more than enough. I just want to get started with it.
 
its really pretty simple. Assuming your textbox is named txtRecord...

Code:
    Dim gTextBox As Graphics = txtRecord.CreateGraphics()
    If gTextBox.MeasureString("", txtRecord.Font).Width > TextBox.Width Then
        'Do what you need to do
    End If
 
Back
Top