Take the 2-minute tour ×

I was just wondering if anyone has come across how to apply an input mask for a Tool Strip Combo Box in C#?

My drop down box gets populated over time with IP addresses, and I would like to restrict what the users are able to write in (Ex: can only input 3 '.'s, only numbers, etc).

So if anyone is able to help out I would really appreciate it!

Thanks in advance.


My design has changed so I now need to have a ToolStripComboBox

share|improve this question
Maybe I can apply a Regular Expression? But how would I go about doing that? –  Feytality Sep 18 '12 at 16:39
There should not be any difference between ToolStipComboBox and ComboBox are pretty minimal. Both solutions below should work the same between the two options. –  Trisped Sep 19 '12 at 16:33
@Trisped This is not true because ToolStripComboBox does not inherit ListControl which is what knows about Formatting, from what I understand. –  Feytality Sep 19 '12 at 18:38
I updated my answer below to address this. –  Trisped Sep 19 '12 at 23:27

2 Answers 2

up vote 1 down vote accepted

You could try catching the KeyUp event, then check that the input is valid. If not revert it to the last valid input. You would probably want to do something similar with the Validating event (make sure CausesValidation is true).

Another option would be to create a MaskedTextBox and place it so it covers the text box portion of the drop down menu. You would then need to wire up the events so the two form controls remained synced.

You could also look into the ErrorProvider class.

There are a couple of other ways (like a timer which runs ever .3 seconds), but those are usually performance hogs or difficult to maintain.

Update for regular expression comment:
If I was to do this I might use a regular expression or I might manually parse the string. Either way the KeyUp and Validating events is where I would check the validation of the control. The KeyUp event gives me the option to check as they type while the Validating event allows me to validate when the control loses focus. Which you use will depend on what you want the user experience to be.

If you do not use the KeyUp event to validate, you could add a timer which runs 5 seconds after the last key press. This way the control would not have to lose focus for the error to show.

Update for edited question and comment:
You could not use Format event as your question was on how to format user input, not how things are added to the list. As such that solution does not work with ToolStripComboBox or with ComboBox.

After reading the documentation for ToolStripControlHost, you might be able to cast ToolStripComboBox to ComboBox. If not then you could use the ToolStripControlHost to place the ComboBox onto your form. - This is incorrect or unnecessary, please see update below the quote.

ToolStripControlHost is the abstract base class for ToolStripComboBox, ToolStripTextBox, and ToolStripProgressBar. ToolStripControlHost can host other controls, including custom controls, in two ways:

  • Construct a ToolStripControlHost with a class that derives from Control. To fully access the hosted control and properties, you must cast the Control property back to the actual class it represents.

  • Extend ToolStripControlHost, and in the inherited class's default constructor, call the base class constructor passing a class that derives from Control. This option lets you wrap common control methods and properties for easy access in a ToolStrip.

Use the ToolStripControlHost class to host your customized controls or any other Windows Forms control.

To customize a ToolStripItem, derive from ToolStripControlHost and create a custom implementation. You can override methods such as OnSubscribeControlEvents to handle events raised by the hosted controls, and you can put custom functionality into properties to enhance the hosted control.

Update: According to the ToolStripComboBox documentation you can access the underlying ComboBox through ToolStripComboBox's ComboBox property.

This is why I usually read the documentation on a control before I use it. I might not understand it, but at least I will have an idea what to look for. :)

share|improve this answer

You should create Format event like this:

    private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
        e.Value = GetFullIpFormat((string)e.Value);

And here is code for formating values:

    string GetFullIpFormat(string value)
        string[] ip = new string[4];
        for (int i = 0; i < ip.Length; i++)
            ip[i] = GetIpPart(i, value);
        return string.Format("{0:###}.{1:###}.{2:###}.{3:###}", ip[0], ip[1], ip[2], ip[3]);

    string GetIpPart(int partNumber, string ip)
        string result = "000";
        int iLen = 3;
        ip = ip.Replace(".", "");
        int iStart = partNumber * iLen;
        if (ip.Length > iStart)
            result = ip.Substring(iStart);
            if (result.Length > iLen)
                result = result.Substring(0, iLen);
        return result;

This will do formating for you. Alternativly you can check input on same event for numbers. This will do the job for you, happy coding! :)

share|improve this answer

Your Answer


