简体   繁体   中英

how to set start with for textbox when combobox is changed in user control

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?

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;
    }

}

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.

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".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM