473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Change the TextBox width to fit my text?


Visual Studio 2003 .NET C#

I am trying to change the width of a textbox at run time, to fit in the text
it contains. How can I do this?

Thanks

Steve
Aug 22 '05 #1
4 13312

"Steve" <St***@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...

Visual Studio 2003 .NET C#

I am trying to change the width of a textbox at run time, to fit in the
text
it contains. How can I do this?


System.Windows.Forms.TextBox myTextBox = new System.Windows.Forms.TextBox();
myTextBox.Width = x;
Aug 22 '05 #2
but how do I calculate the value of x? All I have is the Length of my
string, the lenght for instance of the string "Pumping Station" is 15, but if
I were to set the Width of my text box to 15, then this would not be the
correct width, as the width is in pixels I think. I guess the question is,
how do you calculate the width of length ;-)

"Jeff Connelly" wrote:

"Steve" <St***@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...

Visual Studio 2003 .NET C#

I am trying to change the width of a textbox at run time, to fit in the
text
it contains. How can I do this?


System.Windows.Forms.TextBox myTextBox = new System.Windows.Forms.TextBox();
myTextBox.Width = x;

Aug 22 '05 #3
Well, this is one way of accomplishing it:

Graphics g = Graphics.FromHwnd(textBox1.Handle);
SizeF f = g.MeasureString("String to fit", textBox1.Font);
textBox1.Width = (int)(f.Width);
// textBox1.Height = (int)(f.Height); // if required...
textBox1.Text = "String to fit";

"Steve" <St***@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...

Visual Studio 2003 .NET C#

I am trying to change the width of a textbox at run time, to fit in the text
it contains. How can I do this?

Thanks

Steve
Aug 22 '05 #4

"Steve" <St***@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
but how do I calculate the value of x?


Sorry Steve, guess I should have guessed you already knew that. Haven't
checked Siva's code, but that's the idea you want....
Aug 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: S.W. Rasmussen | last post by:
A trivial (?) question: does anyone know how to change the shape of the cursor in a RichTextBox control from the normal vertical line to an underscore?
3
by: Ronald S. Cook | last post by:
Hi all, I have an ASP.NET DataGrid wherein there is an edit link for each row. Upon clicking the link, certan fields in that row display in text boxes so that they may be edited. I would like...
4
by: Rodrigo DeJuana | last post by:
Howdy, I'm new to this .net stuff and really have little to no training. Im trying to create a new page for a web form, so i have been pretty much jsut coping code. I having some issue with...
2
by: Luis Esteban Valencia | last post by:
Hello I have a datagrid with a dropdownlist that has the products, another column has the price of the product and when the user changes the product it also must change the price how can I achieve...
2
by: Tor Inge Rislaa | last post by:
How to change row height in a DataGrid I have DataGrid that is filled with data from a table in a DataSet. The content of the cells is text of more than one line (as a note field). What I...
4
by: Steve | last post by:
Visual Studio 2003 .NET C# I am trying to change the width of a textbox at run time, to fit in the text it contains. How can I do this? Thanks Steve
0
by: Jacob Donajkowski | last post by:
Once the user logs in I want to have the users switch from the Roster View to the Profile View and enter their profile infomation and save it. Then the next time they login and go to the Profile...
0
KalariaNitya
by: KalariaNitya | last post by:
Hello, could anybody help me? i have gridview, inside gridview i have one Update button & textbox update button update the quantity entered in textbox. i want to update the quantity on textbox on...
2
by: mrutyunjaya | last post by:
hello, i have two textbox column and one button control in gridvie when i click button it will ask plese enter price in first textbox . when enter values it is asking again. how to pass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.