This is the list of all 'Keys' in .Net. They are valid for all languages.
http://msdn.microsoft.com/library/d...tml/frlrfsystemwindowsformskeysclasstopic.asp
That's a helpful link - thanks.
As for the event handler, here's what I cam up with:
1. Added event hander to txtCfgNam
this.txtCfgNam.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.txtCfgNam_KeyPress);
2. used this method in my Main form:
private void txtCfgNam_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = MakStr.chkStr(e);
}
Here is the MakStr class:
public class MakStr
{
public static bool chkStr (
System.Windows.Forms.KeyPressEventArgs e
) //constructor
{
if (
!Char.IsDigit(e.KeyChar) && //not a number
!Char.IsLetter(e.KeyChar) && //not a letter
e.KeyChar.ToString() != "_" && //not an underscore
!Char.IsControl(e.KeyChar) //not a delete, backspace, etc.
)
{ //so tell the CLR to ignore this event - we've handled it ourselves
return true;
}
else
{
return false;
}
}
Because there are other textboxes I want to enforce this rule on, this seems
to be a good approach.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
I am in need of another custom event handler for another situation, and
would appreciate your input. Here is the method that runs when text has
changed in txtCfgNam:
private void txtCfgNam_TextChanged(object sender, System.EventArgs e)
{
string oldCfgNam = "strCfgNam"; //this is actually done programmatically
elsewhere
txtCfgNam.Text = MakStr.str22(txtCfgNam.Text.ToString()); //potentially
changes the string
Configuration cfg = new Configuration();
cfg.newCfgNam(xdd.DataSet, oldCfgNam, txtCfgNam.Text.ToString()); //adds
new config to dataset
}
Now, the key purpose of static method MakStr.str22 is to make sure the
string entered into the text box - perhaps via cut/paste - is less than or
equal to 22 characters and contains no unacceptable characters (the method
runs code similar to MakStr.chkStr). The problem is that the current
handler for the "Text Changed" event is fired off a second time if
MakStr.str22 shortens or modifies the string (if no modification is made to
the string, no problem).
How can I prevent the event handler from firing the second time? Do I need
a custom event handler, or just some way to make e.Handler = true in the
MakStr.str22 method? Again, the problem is that MakStr.str22 changes the
txtCfgNam.Text value if a string over 22 chars (or containing unacceptable
characters) is pasted into the txtCfgNam text box - this is interpreted as a
"Text Changed" event and causes the txtCfgNam_TextChanged method to run a
second time. I want to intercept the TextChanged event handler (perhaps in
the MakStr.str22 method) and prevent the second pass through the method.
Any suggestions welcome!
Thanks in advance.