Jump to content

Is it possible to adjust the size of the textbox as the amount of text in it increases? (C#)

so i have a read only textbox that displays text from a column in the database.if there is a lot of text to be shown i have to manually scroll down it to see the remaining part.is it possible to automatically expand the textbox  according to the amount of content to be displayed? Any help would be greatly appreciated. 

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't touched C# in a long time, but when the user focus is in the text box, create a thread that will count the characters inside it and resize it as necessary. The thread should be killed when the user loses focus of the text box.

 

When making threads that change the UI I think you need some intent-invoker, I forgot what it's called, but I remember it being on the MSDN C# documentation

Speedtests

WiFi - 7ms, 22Mb down, 10Mb up

Ethernet - 6ms, 47.5Mb down, 9.7Mb up

 

Rigs

Spoiler

 Type            Desktop

 OS              Windows 10 Pro

 CPU             i5-4430S

 RAM             8GB CORSAIR XMS3 (2x4gb)

 Cooler          LC Power LC-CC-97 65W

 Motherboard     ASUS H81M-PLUS

 GPU             GeForce GTX 1060

 Storage         120GB Sandisk SSD (boot), 750GB Seagate 2.5" (storage), 500GB Seagate 2.5" SSHD (cache)

 

Spoiler

Type            Server

OS              Ubuntu 14.04 LTS

CPU             Core 2 Duo E6320

RAM             2GB Non-ECC

Motherboard     ASUS P5VD2-MX SE

Storage         RAID 1: 250GB WD Blue and Seagate Barracuda

Uses            Webserver, NAS, Mediaserver, Database Server

 

Quotes of Fame

On 8/27/2015 at 10:09 AM, Drixen said:

Linus is light years ahead a lot of other YouTubers, he isn't just an average YouTuber.. he's legitimately, legit.

On 10/11/2015 at 11:36 AM, Geralt said:

When something is worth doing, it's worth overdoing.

On 6/22/2016 at 10:05 AM, trag1c said:

It's completely blown out of proportion. Also if you're the least bit worried about data gathering then you should go live in a cave a 1000Km from the nearest establishment simply because every device and every entity gathers information these days. In the current era privacy is just fallacy and nothing more.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Don't need to create threads to do it. Could just monitor onClick or something like that, on the text box.

 

Counting the characters is very basic and often not enough.  Not all characters have the same width, especially in the case of UTF-8 data where some characters can be much wider or taller than others , or someone enters t̨̳͍̩e̡̹̬̫̣x̷t͏ ̭̠͍l̩̦̙i̶̮͕̱k̲é̦ ̯̥̦̣͖̲͜t͈̗̺̮̺h̻̮̼̯̰͈͙i̥s̢̩̙͖̼̞̺  

 

There are some Windows API functions (possibly also in the .net base) which can determine the width of a segment of text based on the font properties used in the text box, and possibly could also be dpi aware but I'm not sure (if user has 4k monitor but it's only 27" he may set the fonts to 125% or something like that which would screw up the measurements of those functions).

So you could call that api function to figure out if the text you entered exceeds the width of the text box and if so, you could change the property of the text box to multiLine or something like that (and add vertical scrollbar if you prefer)  or create a copy of the text in memory, add ENTER characters at various points and then calculate the height of the text segment and set the height of the text box to that.

 

See

 

 

https://msdn.microsoft.com/en-us/library/windows/desktop/dd145122(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/dd144821(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/12w624ff(v=vs.110).aspx

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

All GUI elements in have events you can use to take advantage of this. As mentioned before you could use onClick or (I forget if it's there) textChange or something.

 

Either way, create an event handler from an appropriate event and change the size of the text box as necessary.

 

Threads may not also work because from my experience .NET tends to be picky about cross thread data accesses. You could use delegates to get around this, but again, there's already a facility that creates events that can be handled.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×