How to set the first few characters of a WinForms text field read-only?

I have a form with a text field that is used to enter a url. I need to add (http: //) as a predefined value to this text box and want it to be read-only so that the user cannot delete http: //, but after it he can write.

enter image description here

Any help would be greatly appreciated.

+48
c # winforms
Jun 14 2018-12-14T00:
source share
10 answers

Here are a few options:

  • A simple way is to simply create a label outside the text box (left) with these characters. (simple and user friendly)

  • Create a second text box for reading, which will be used at the beginning, create it so that it matches the input and aligns them next to each other. Yes, you will get one pixel line to separate them both, but I think it will add to the user experience so that it is obvious that this is not for messing around with (I would personally choose this option)

  • If you need a style, you can flip your own custom control , which uses a panel, shortcut, and text box with an appropriate set of styles as needed. (the best way to get the style you want)

  • The fourth, more annoying way is to process one of the key events (for example, KeyDown ) in the text field itself. With this, you can do numerous checks and change the position of the carriage to make it work, but believe me, this will help you in making it work perfectly! (too hard work to get right)

To summarize, I think option 2 is the best here. Of course, if you were to use WPF, you would undoubtedly have much more design flexibility.

+60
Jun 14 2018-12-14T00:
source share

Have you considered placing a tag next to it using "http: //" as text? and then, accepting user input, you can simply add "http: //" with your text box. Text.

Here is another idea:

In each backspace, specify the number of characters in the text box. If it is == 7, then ignore the backspace. If it is larger, check the number of characters after the space. If the number of characters is less than 7, clear the text box and reset the text.

private void a_keyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)8) { if (myTextbox.Text.Length == 7) // do stuff.. } else if //do stuff... } 
+20
Jun 14 2018-12-14T00:
source share

You can also not even display http: // and just add it to Textbox.Text code. First check that it does not start with this.

To clarify my last point:

 string sURL = txtURL.Text.StartsWith("http://") ? txtURL.Text : "http://" + txtURL.Text; 
+12
Jun 14 2018-12-14T00:
source share

Something like that?

 private void textBox1_TextChanged(object sender, TextChangedEventArgs e) { var textBox = sender as TextBox; if (!textBox.Text.StartsWith("http://")) { textBox.Text = "http://"; textBox.Select(textBox.Text.Length, 0); } } 
+6
Jun 14 2018-12-14T00:
source share

Instead, you can use RichTextBox, this allows you to protect the text:

  public Form1() { InitializeComponent(); richTextBox1.Text = "http://"; richTextBox1.SelectAll(); richTextBox1.SelectionProtected = true; richTextBox1.SelectionStart = richTextBox1.Text.Length; richTextBox1.DetectUrls = false; // optional } 

But unfortunately, this will not work if the Multiline property is set to False.

The pragmatic way to do this with a TextBox is to simply return it the way you want. Also works with paste removal and highlighting:

  string protect = "http://"; private void textBox1_TextChanged(object sender, EventArgs e) { if (!textBox1.Text.StartsWith(protect)) { textBox1.Text = protect; textBox1.SelectionStart = textBox1.Text.Length; } } 
+5
Jun 15 2018-12-12T00:
source share

Note. I misunderstood the question, because of something I came here from the "HTML" -tag. But if you want to do something similar using HTML / CSS, this might be one solution.

You can do something like this:

 <style> label.mylabel, input.myinput { display: block; float: left; height: 20px; margin: 0; padding: 10px 5px 0px 5px; border: 1px solid #ccc; font-size: 11px; line-height: 11px; } label.mylabel { border-right: 0; } input.myinput { border-left: 0; } </style> <label class="mylabel" for="myinput">http://</label> <input id="myinput" class="myinput" name="myinput" value=""> 

Thus, it has two advantages:

  • it looks like a single input field
  • when the user clicks "http", the actual form field will be focused

And, of course, you must add "http: //" manually after submitting the form.

All this has one drawback. What if your user wants to insert 'https: //'? :)

Cheers philipp

+4
Jun 14 2018-12-14T00:
source share

If you need a CSS approach (combined with a background image), you can try something like this :

 Enter URL: <input type="text" size="50" class="url" value="www.google.com" /> <style> input[type="text"].url { background: url(http://s18.postimage.org/4wkjdpidh/http.png) no-repeat left top transparent; text-indent: 34px; } </style> 

Then it is just a matter of adding http:// back to the input value when processing it.

+4
Jun 14 2018-12-14T00:
source share

You can place the label only on the left in the text text and set its text property to "http: //". or you can add 2 text fields that one is read, only the other is not only readable. and write http: // in what is read-only.

+1
Jun 14 '12 at 14:45
source share

Create a delegate for the keyup event (and possibly others, such as the clipboard), and check the start of the value.

0
Jun 14 2018-12-14T00:
source share
 $('#myField').bind('keypress', function(event) { if (event.charCode == 0 && event.currentTarget.value.length <= 7) { return false; } }); 
0
Jun 14 2018-12-14T00:
source share



All Articles