Sven Sven - 1 year ago 67
C# Question

Limiting size of input to the size of a TextBox in C#

I've seen quite a few questions about scaling a TextBox to the size of the text, but only found a single question which talked about the reverse here. That question is also from 2010, and I believe the language has evolved since then, and there might be better solutions.

I'll clarify that I do not want to limit my input to an arbitrary number of characters, as the input may include newlines/vertical space. (Which, if it contained a lot of vertical space, could stretch the text beyond the bounds.)

Here's the situation:

  • Form 1 has a textbox. I want this textbox to remain a fixed size. Any data beyond the size should be cut off from the input.

  • I want to save Form 1's textbox's contents to a file.

  • In Form 2, I want to open the file and pull what was Form 1's textbox's contents. These will be saved in a separate textbox local to Form 2.

My strategy right now is to find a way to limit the input to the dimensions of the textbox, so that the textboxes in Form 1 and Form 2 are equivalent, and do not overflow their respective dimensions.

So my question is: How would I go about doing that?

Edit: Sorry, it appears my question didn't provide enough information. I also mis-typed the situation, so I'll re-write it below.

I'll step back and describe more of what I'm trying to do.

Let's assume I have a single Form, with the following two objects:

  • Textbox

  • Label

Anything I type into the Textbox I want to see on the Label's text. Input can be any letters, numbers, or special characters, including spaces and newline characters.

For the sake of consistent sizing, I want the label to be of a fixed graphical size; regardless of the number of characters in the label's text, the label object should be no larger than (x, y), where x and y are arbitrary height and width sizes.

I do not want data in the Textbox that cannot fit within the bounds of the label's size. The user should be prevented from entering data into the Textbox that would extend beyond the label's size bounds.

Are there any strategies not mentioned in the linked question that can determine whether a Textbox's text meets or extends past an arbitrary width and height?

Answer Source

The properties of the textbox give you a max length, you can also do it in code like so

var tb = new TextBox();
tb.MaxLength = 10

if you don't want to use that then you should use

var str = tb.Text.Remove(0, 10) 

this will only give the str variable the first 10 characters no matter the length of whats actually in the textbox.

For what you want on form two, you need to give me more information about what you want.

edit after OP's edit

if you want the text in the textbox to be matched at the label level you want add a TextChanged Event to the property of the textbox and then have something like this

private void TextChanged(object sender, EventArgs e)
    label1.Text = textBox1.Text;

having the label to a fixed size no matter what is isn't going to be good, ive done it, but this also begs the question, why are you duplicating your textbox information into a label. Why not just have it in a textbox