KeyCode Constants for C#?

D

deko

You can use the same System.Windows.Forms.Keys.* enumeration in C# too.

How about just not handling the event if the key pressed is not acceptable?

private void cfgNamChkKey()
{
this.txtCfgNam.KeyPress += new KeyPressEventArgs(keyPressed);
}

private void keyPressed(Object o, KeyPressEventArgs e)
{
//next line is pseudo code... any help here is appreciated.
if (e.KeyChar == Char.IsNumber || e.KeyChar == Char.IsLetter ||
e.KeyChar == Char.Parse("_"))
{
e.Handled = true;
}
}

private void txtCfgNam_TextChanged(object sender, System.EventArgs e)
{
do stuff;
}
 
M

Morten Wennevik

Hi deko,

This is the list of all 'Keys' in .Net. They are valid for all languages.

http://msdn.microsoft.com/library/d...tml/frlrfsystemwindowsformskeysclasstopic.asp

What you are trying to do is easier with the KeyPressEventArgs' KeyChar
property.

protected override void OnKeyPress(KeyPressEventArgs e)
{
if(Char.IsDigit(e.KeyChar) || Char.IsLetter(e.KeyChar) || e.KeyChar ==
'_')
//DoStuff
}

Or,

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(!Char.IsLetterOrDigit(e.KeyChar) && e.KeyChar != '_')
e.Handled = true;
}
 
S

Shiva

if (Char.IsNumber(e.KeyChar) || Char.IsLetter (e.KeyChar) || (int)e.KeyChar
== 95)

{

e.Handled = true;

}

deko said:
You can use the same System.Windows.Forms.Keys.* enumeration in C# too.

How about just not handling the event if the key pressed is not acceptable?

private void cfgNamChkKey()
{
this.txtCfgNam.KeyPress += new KeyPressEventArgs(keyPressed);
}

private void keyPressed(Object o, KeyPressEventArgs e)
{
//next line is pseudo code... any help here is appreciated.
if (e.KeyChar == Char.IsNumber || e.KeyChar == Char.IsLetter ||
e.KeyChar == Char.Parse("_"))
{
e.Handled = true;
}
}

private void txtCfgNam_TextChanged(object sender, System.EventArgs e)
{
do stuff;
}
 
D

deko

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

Morten Wennevik

Well, what about something like:

string newCfgNam = MakStr.str22(txtCfgNam.Text); // no need to use
ToString as the Text property IS a string
if(String.Compare(newCfgNam, txtCfgNam.Text) != 0)
{
txtCfgNam.Text = newCfgNam;
}
else // equal
{
Configuration cfg = new Configuration();
cfg.newCfgNam(xdd.DataSet, oldCfgNam, txtCfgNam.Text.ToString());
}

It's not particularly pretty but it should work.
 
D

deko

Well, what about something like:
string newCfgNam = MakStr.str22(txtCfgNam.Text); // no need to use
ToString as the Text property IS a string
if(String.Compare(newCfgNam, txtCfgNam.Text) != 0)
{
txtCfgNam.Text = newCfgNam;
}
else // equal
{
Configuration cfg = new Configuration();
cfg.newCfgNam(xdd.DataSet, oldCfgNam, txtCfgNam.Text.ToString());
}

Using "if(String.Compare(newCfgNam, txtCfgNam.Text) != 0)" as a condition is
a good idea - sort of like a TextChanged event handler. Thanks for the tip.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top