1

I used form user control(instead of form in C# winform).

In this user control, i have one combo box and one textbox.

When combo box is changed, for selected value(1 or 2 or 3),text of textbox start with 1 or 2 or 3 respectively.

User can add 6 digit number in textbox but should not able to delete or change 1 or 2 or 3.

how do i do?

DoctorAV
  • 1,189
  • 1
  • 14
  • 40
user1770370
  • 267
  • 2
  • 7
  • 18

4 Answers4

1

See if this will work, it is handling the TextChanged Event to verify that the first Character is the value from the ComboBox selection.

public partial class UserControl1 : UserControl
{
    string mask;

    public UserControl1()
    {
        InitializeComponent();
        textBox1.MaxLength = 7;
        textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
        textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

    }

    void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsNumber(e.KeyChar) && !( e.KeyChar == 0x8) && !(e.KeyChar == 0xd))
            e.Handled = true;

    }

    void textBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;

        char[] temp = tb.Text.ToCharArray(); //Code to catch any cut and Paste non numeric characters
        foreach (var item in temp)
        {
            if (!(char.IsNumber(item)))
            {
                tb.Text = "";
                break;
            }
        }

        if (tb.TextLength == 0)
        {
            tb.Text = mask[0].ToString();
            tb.SelectionStart = tb.Text.Length;

        }
        else
        {
            if (tb.Text[0] != mask[0])
            {
                tb.Text = mask[0] + tb.Text;
                tb.SelectionStart = tb.Text.Length;
            }
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        mask = ((ComboBox)sender).SelectedValue.ToString();
        textBox1.Text = mask;
    }

}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • thank you...it work but in textbox, display "2]" !!! I don't want to show "]" in textbox – user1770370 Nov 17 '12 at 07:29
  • @user1770370 You will need to filter your KeyStrokes in your KeyPress Event, or look into something like this [SO question](http://stackoverflow.com/questions/508533/numeric-textbox). I will see if I can come up with something in a bit. – Mark Hall Nov 17 '12 at 07:35
  • @user1770370 I added a keypress event, see if that works for you. – Mark Hall Nov 17 '12 at 08:01
  • 1
    @user1770370 The code I added will handle all letters, your brackets, everything except for numbers, backspace and enter key. You should not need to handle the bracket 5B seperately – Mark Hall Nov 17 '12 at 08:10
  • @user1770370 I just added a little more error checking to catch any non numeric characters in the TextChanged Event, It will reset the TextBox to just your first number – Mark Hall Nov 17 '12 at 08:22
  • befor than tab go to textbox and user type this, when combo changed, in textbox , instead of 2 , show "2]"!!!! it dosen't relate with key press!! – user1770370 Nov 17 '12 at 08:23
  • @user1770370 I can not duplicate it, What are the exact steps/keypresses that you are using to duplicate the problem. and what is the exception that you got. The code that I gave you is working on my computer – Mark Hall Nov 17 '12 at 08:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19670/discussion-between-mark-hall-and-user1770370) – Mark Hall Nov 17 '12 at 08:39
  • I put break point in foreach loop,but it locates in first cells of array and didn't go to second cell!!!!!and get exception because it created loop!!!! – user1770370 Nov 17 '12 at 08:48
0

In the combobox selectedchanged event, set the textbox value as 1/2/3. Set up the textbox key pressed or keydown event to handle the case that the user cannot delete/edit the first character in the textbox. Setup your condition and set e.Handled = true if the user is trying to delete/edit the first character. Make sure to give some kind of warning to the user that he cannot edit the first letter.

cmbBox_SelectionChanged(object sender, SomeEventArgs args)
{
    txtBox.Text = "1";
}

txtBox_KeyPress(object sender, KeyPressEventArgs e)
{
     //Writing a little bit of pseudo code here 'cause I don't have VS on this system.
     if(KeyPressed is delete or if the txtBox.Text string is left with a single character)
         e.Handled = true;
}

You can similarly handle some other cases like if the user selects the entire text and deletes. Do check if I have missed any possible cases in the combo box too. Maybe, you'd just like to replace the first character of the existing string in the textbox when the user selects a new value. Stuff like that.

Harsha
  • 661
  • 1
  • 8
  • 21
0

If you really want the digit in the textbox, you're probably going to have to handle the KeyPress event, and make sure the user isn't deleting the digit you want to start with, or adding digits in front of your starting digit. Handling the KeyPress event will also allow you to filter for the length and to constrain the user to typing only numbers.

Another way to handle this would be to put a Label control just to the left of your TextBox. Put the digit in the Label and allow the user to edit the text in the TextBox. Manipulate the Value of your UserControl to concatenate the two together. If the ComboBox has '2' selected, then the text in the Label will be "2", and the user types "543", then set the Value property of your User control to "2543".

gmlobdell
  • 383
  • 3
  • 10
0

Don't bother with all the cases in the event. Just place label ahead of the textbox and set it with combobox value. Clean and simple to maintain.

Shahar G.
  • 1,440
  • 12
  • 22