Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

c# - Textbox disabled with a huge string

I'm having a issue with a textbox when it's typed a huge string.

In my case, the textbox is used to write email addresses, and it doesn't have a limit of chars. So, I've made this test: I wrote 200 email addresses in a notepad, and I pasted the text on the textbox and the text disappeared, but if I change the focus of the control, the text is displayed again. I already saw this link, but it doesn't helped me. I already tried to change the MaxLength property to 0 (how the microsoft says to do), but it doesn't worked here too.

Considering a email address with 50 chars, the MaxLength property wouldn't be a problem, because 200 email addresses multiplying with 50 chars (each email), I'll have 10000 chars, and the default value of TextBox.MaxLength is 32767.

And, before you ask for the code, I only set the text to a string.

myEmailObject.Address = txtEmail.Text;

Should I use RichText, or what?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a known limitation in the Windows EDIT control, the native control that's wrapped by the TextBox class. I know the limitation is present in Windows 7 SP1 and Windows 8, earlier versions almost surely have it too.

You'll exercise this limit once you display more than about 5000 characters in a single-line TextBox. Give or take, it is based on the total width in pixels of the displayed text. So you can fit a lot more when you use a smaller font or have a lot more i than W characters. Afaik, you'll trip the limit when the width exceeds 32767 pixels, a number that pops up in a few places in the USER32 api and dates back to Windows versions <= 3 which were 16-bit. Keeping EDIT compatible across major releases for 30 years was a major effort.

This limit does not get put to the test very often, stuffing that many characters in a single-line text box isn't practical. There is no reasonable way the user can do anything useful with that much text in so little space. Cannot possibly read it, reliable editing is certainly out of the question.

Do consider a more practical and user-friendly UI, the limitation simply stops being an issue. Use Multiline = true or just display a place-holder string that uses ellipsis.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...