Grey Wolf: CHEERS MATE for replying. I will explain alittle better. I'm try to create my own class called myTextboxName (which is dervived from mytexbox another one of my classes. I will go through what i have done so far with it piece by piece:
The following bit of code is where i overridethe IsInputChars so i can test for TAB:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
_gc class myTextBoxName : public myTextBox
{
protected :
Control *nextControl;
virtual bool IsInputKey(Keys keyData) //override
{
if (Keys::Tab == keyData)
{
return true;
}
else
{
return false;
}
//check keyData is key value and return true or false
//return false;
}
|
The next bit of code is setting up my prototypes etc for the class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public :
bool nonNumEnt;
bool Empty;
bool SpecialChar;
void textboxLostFoc(Object *Sender, EventArgs *Args);
void validateKeyDown(Object *, System::Windows::Forms::KeyEventArgs *);
void validateKeyPress(Object *, System::Windows::Forms::KeyPressEventArgs *);
void validateKeyUp(Object *, System::Windows::Forms::KeyEventArgs *);
void trimText(TextBox *);
myTextBoxName();
myTextBoxName(Int32, Int32, Int32);
};
|
The next bit of code from my class is the overloaded contructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
myTextBoxName::myTextBoxName(Int32 x, Int32 y, Int32 w) : myTextBox(x,y,w)
{
/* Now we will validate which key pressed:
a) KeyDown = what key is being pressed. It will set nonNumEnt to TRUE if
a Alpha character is pressed.
b) KeyPress = Will handle the nonNumEnt and if it is false, wont accept the
non character into the textbox
*/
this->AcceptsTab = true;
this->TabStop = true;
KeyDown += new KeyEventHandler(this,&myTextBoxName::validateKeyDown);
KeyPress += new KeyPressEventHandler(this, &myTextBoxName::validateKeyPress);
KeyUp += new KeyEventHandler(this, &myTextBoxName::validateKeyUp);
nextControl = new Control();
}
|
The next bit is my KeyDown event. Here i test the to makesure the inputs are only alphas and no numbers and sets the nonNumEnt too TRUE if the input is alphas only, if not is should output an error. ALso is SpecialChar = true, if the KeyData = TAB. In addition if the Text property is empty then sets the Empty flag to true
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
void myTextBoxName::validateKeyDown(Object *, System::Windows::Forms::KeyEventArgs * e)
{
nonNumEnt = false;
Empty = false;
SpecialChar = false;
SpecialChar = IsInputKey(e->KeyCode);
if(SpecialChar == true && this->Text->Length == 0)
{
nonNumEnt = false;
Empty = true;
myErrorCapture * ErrorDialog = new myErrorCapture(S"tab and length N= 0",2 ,this);
}
if ( e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9 )
{
// Determine whether the keystroke is a number from the keypad.
if ( e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9 )
{
// Determine whether the keystroke is a backspace.
//if ( e->KeyCode = Keys::Back )
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumEnt = true;
}
}
if (nonNumEnt == false)
{
//myErrorCapture * ErrorDialog = new myErrorCapture(S"Error Message - 1",1 ,this);
}
}
|
The next is my KeyPress event which checks for the nonNomEnt. if it is false, the it HANDLES the key press which keeps the cursor in the same control whithout leaving:
1 2 3 4 5 6 7 8 9 10 11
|
void myTextBoxName::validateKeyPress( Object * /*sender*/, System::Windows::Forms::KeyPressEventArgs *e )
{
// Check for the flag being set in the KeyDown event.
if (nonNumEnt == false)
{
// Stop the character from being entered into the control since it is non-numerical.
e->Handled = true;
//myErrorCapture * ErrorDialog = new myErrorCapture(S"E = HANDLED",2 ,this);
}
}
|
The next is my KeyUp event. This is where im trying too leave the control IF the SpecialChar = true (tab pressed) and Empty = false (Not empty!)
What im doing is using GetNextControl to get the next control into nextControl. Im using this because i could have comboboxes etc on the form, and because this is in the class, i dont k now what the next control will be (Could instantiate anything on the form).
Then i use SelectNextControl using the nextControl as a parameter.
Then i use InvokeLostFocus to change focus to the nextControl.
Then i instantiate a Leave event.
i
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
void myTextBoxName::validateKeyUp(Object *, System::Windows::Forms::KeyEventArgs *e)
{
if(SpecialChar == true && Empty == false)
{
//nextControl = ;
if(nonNumEnt == false)
{
nextControl = this->GetNextControl(this,true);
this->SelectNextControl(nextControl, true, true, true, true);
this->InvokeLostFocus(nextControl,e);
Leave+= new EventHandler(this,&myTextBoxName::textboxLostFoc);
}
}
/*
if(this->SelectNextControl(this,true,false,true,false) == true)
{
if(nonNumEnt == false)
{
Leave+= new EventHandler(this,&myTextBoxName::textboxLostFoc);
}
}
*/
}
|
The leave event code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void myTextBoxName::textboxLostFoc(Object *Sender, EventArgs *Args)
{
if(this->Text->Length > 1)
{
trimText(this);
}
else
{
myErrorCapture * ErrorDialog = new myErrorCapture(S"Error Message - 2",2 ,this);
}
}
void myTextBoxName::trimText(TextBox __gc *data)
{
data->Text->Trim();
data->Text = data->Text->ToLower();
data->Text = String::Concat(data->Text->Substring(0,1)->ToUpper(),data->Text->Substring(1,data->Text->Length-1));
}
|
My problem is that i cannot get the focus to change to the new control. I have instantiated 2 myTextBoxName called Surname/Forename on a form, but the cursor remains within the first, which is Surname. I cannot get the InvokeLostFocus etc to work!!