473,413 Members | 1,802 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,413 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 13314

"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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.