Thorin Oakenshield
Thorin Oakenshield

Reputation: 14692

Custom Windows Control Library in C#

I've created a customized Text Box using Windows Forms Control Library, and which have options for Numeric Text Box, Alpa Numeric Text Box, Decimal etc...

I've overridden the PreProcessMessage(ref Message msg) Method to do this.

Here is my sample snippet for Numeric Text Box

    public override bool PreProcessMessage(ref Message msg)
    {
        int WM_KEYDOWN = 0x0100;
        if (msg.Msg == WM_KEYDOWN)
        {
            Keys keys = (Keys)msg.WParam.ToInt32();
            bool bNumbers = false;
            switch (eType)
            {

                case enTextBoxTypes.Numeric:

                    bNumbers = ((keys >= Keys.D0 && keys <= Keys.D9) || (keys >= Keys.NumPad0 && keys <= Keys.NumPad9)) && ModifierKeys != Keys.Shift;
                    System.Diagnostics.Debug.Print(bNumbers.ToString());
                    if ((keys == Keys.OemMinus || keys == Keys.Subtract) && bAllowMinusSign && ModifierKeys != Keys.Shift)
                    {
                        bIsOEMMinus = (keys == Keys.OemMinus) | (keys == Keys.Subtract);
                        if ((this.Text.Length != this.SelectedText.Length || !bAllowMinusSign) && this.SelectionStart != 0)
                            bIsOEMMinus = false;
                    }

                    break;
            } 
            bool bDel = keys == Keys.Delete;
            bool bBack = keys == Keys.Back;
            bool arrows = (keys == Keys.Up) | (keys == Keys.Down) | (keys == Keys.Left) | (keys == Keys.Right);
            bool Enter = (keys == Keys.Enter);
            bool ESC = (keys == Keys.Escape);
            bool TAB = (keys == Keys.Tab);
            bool Home = (keys == Keys.Home);
            bool End = (keys == Keys.End);

            if (bNumbers  bBack | bDel | arrows | Home | End | bIsOEMMinus)
                return false;
            else if(TAB)
                return base.PreProcessMessage(ref msg);
            else
                return true;                
        }
        else
            return base.PreProcessMessage(ref msg);
    }

Now I need to implement the unit in the Textbox for this I've added a new enumeration in the TextBox type i.e. Distance. So if I entered a decimal number, and after that if I pressed the k key then the text box value should be appended with Km

for example if the text box's text is 100 and then I pressed k then the text box's text should be 100Km .

And also I need to provide the unit change functionality to the Up Down keys.

So if i press the down key in the text box with the above value , then the text box value should be 100000m.

How do I do this?

Upvotes: 0

Views: 826

Answers (2)

Julian
Julian

Reputation: 1113

Maybe you could just inherit the textbox control and alter the OnTextChanged and onkeydown events. Look at this link, he uses both the events. (Don't look at the name though)

If you don't think this will help you for whatever reason, please explain me what holds you.

Upvotes: 1

goldengel
goldengel

Reputation: 611

Perhaps you can use a masked Textbox if this is your question. Because I see you already have the KeyDown in use and I think you know how to handle. Walkthrough: Working with the MaskedTextBox Control

Me.MaskedTextBox1.Mask = "00/00/0000"
MaskedTextBox1.ValidatingType = GetType(System.DateTime)

Mask edit sample

Upvotes: 1

Related Questions