1

good day guys, can anyone help me with my c# application. I want my first 7 characters/numbers in a textbox that cannot be deleted or backspace disable for the first 7 characters/numbers. And the rest will be inputted only by numbers. Is this really possible? I have a combobox and a texbox. If I select a value/text in the combo box that value/text will be in the textbox and then that value/text cannot be deleted. I am not really good at c#.

private void combo1_SelectedIndexChanged(object sender, EventArgs e)
    {

        String sent = "ABC-DEF";
        txtbox1.Text = sent.ToString();
        txtbox1.SelectionStart = txtbox1.Text.Length;
}
Luc Morin
  • 5,302
  • 20
  • 39
jLaw
  • 302
  • 3
  • 15
  • 1
    Winforms or ASP.NET? – D Stanley Oct 16 '15 at 14:28
  • 1
    Take a look at this: http://stackoverflow.com/questions/10253107/how-to-stop-the-first-character-in-a-text-box-from-being – Hugues Paquet Blanchette Oct 16 '15 at 14:29
  • 3
    Just to be sure I understand your scenario, the first 7 characters are going to be set by the software, and you want to prevent the user from deleting them ? Why not simply use a label placed beside the combobox to display that "immutable" string ? – Luc Morin Oct 16 '15 at 14:32
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Oct 16 '15 at 14:32
  • @DStanley - winforms – jLaw Oct 16 '15 at 15:13
  • @LucMorin - actually i been thinking that before i asked it here in SO. but as much as possible i wanted it to be textbox. I'm looking a way to solve it through textbox. I appreciate your help – jLaw Oct 16 '15 at 15:15

2 Answers2

2

Use KeyDown event (http://www.dotnetperls.com/keycode).

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) && textBox1.Text.Length == 7)
    {
        e.SuppressKeyPress = true;
    }
}

To validate numeric inputs: Get the char on Control.KeyDown?


You can use a JS lib : http://www.jqueryscript.net/form/jQuery-Plugin-For-Adding-Prefixes-To-Input-Values-Prefix-Input.html

Or make you own code, like:

HTML:

<input id="txt" name="teste" type="text" value="ABC-DEF" />

JS:

 $('#txt').on('keydown', function(e){
        if(e.keyCode == 8 && $('#txt').val().length == 7){
            e.preventDefault();
        }
     });

Test: http://jsfiddle.net/ecuovbca/

Community
  • 1
  • 1
Zack Stone
  • 643
  • 2
  • 10
  • 23
  • To accept only numbers after the txt prefix, use a jQuery mask (https://igorescobar.github.io/jQuery-Mask-Plugin/). Your mask can be like: "AAA-AAA00" 6 letters and 2 numbers. Customize to your liking. – Zack Stone Oct 16 '15 at 14:47
  • 1
    Winforms, not ASP.NET ;-) – Luc Morin Oct 16 '15 at 15:17
  • @Yitzhak - its winforms but thanks for the help I keep your answer in my notes for future reference If I will need help in web. Thank you – jLaw Oct 16 '15 at 15:18
  • Your WinForm code doesn't really stop a user from messing with those first seven characters. – LarsTech Oct 16 '15 at 16:22
  • 1
    @YitzhakStone - the code above help me. I change a few of my codes, you help me answered my question thank you – jLaw Oct 17 '15 at 11:00
0

I presume you're talking about a windows forms app, e.g. rather than ASP.net.

I'd keep the first 7 characters in a label (non-user editable control maps to non-user editable portion of data), and then the textbox should be responsible for the remaining user-editable characters.

To limit the textbox to only numbers, see here:

How do I make a textbox that only accepts numbers?

Community
  • 1
  • 1
Richard EB
  • 967
  • 10
  • 24