473,569 Members | 2,601 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 13343

"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
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
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...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
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...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
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.