473,569 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13342

"Steve" <St***@discussi ons.microsoft.c om> wrote in message
news:C5******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c om> wrote in message
news:C5******** *************** ***********@mic rosoft.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.FromHw nd(textBox1.Han dle);
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***@discussi ons.microsoft.c om> wrote in message
news:C5******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c om> wrote in message
news:E8******** *************** ***********@mic rosoft.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
9430
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
9455
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 some textboxes to be wider, some narrower. By default, they are all the same pre-defined width. Does anyone know how I can do this? Thanks very...
4
2893
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 some textboxes not updating when i a hit save. for example I have this code in my aspx.cs file: declared:
2
2337
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 that. Thanks Datagrid html
2
3362
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 want is to set the height of the row based on the number of lines in it. The row height of each row in the table may be different from each other. Is...
4
386
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
2390
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 View I want the form populated from there profile on the sql server. The code to save the profile works fine. But when the user logs back in they data...
0
2525
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 ENTER event. i had done below code: page_load()
2
12861
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 textboxid in javascript .my code is function validation() { alert("hi")
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.