|
Try this:
private void textBox1_KeyDown( object sender, KeyEventArgs e ) {
if ( e.Control ) {
switch ( e.KeyCode ) {
case Keys.X:
case Keys.C:
break;
default:
e.SuppressKeyPress = true;
break;
}
} else {
switch ( e.KeyCode ) {
case Keys.NumPad0:
case Keys.NumPad1:
case Keys.NumPad2:
case Keys.NumPad3:
case Keys.NumPad4:
case Keys.NumPad5:
case Keys.NumPad6:
case Keys.NumPad7:
case Keys.NumPad8:
case Keys.NumPad9:
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
case Keys.NumLock:
case Keys.Left:
case Keys.Right:
case Keys.Back:
case Keys.Delete:
break;
default:
e.SuppressKeyPress = true;
break;
}
}
}
private void textBox1_KeyPress( object sender, KeyPressEventArgs e ) {
Int16 num;
if ( Int16.TryParse( textBox1.Text.Trim() + e.KeyChar.ToString(), out num ) ) {
if ( !( num <= 10 && num >= 0 ) ) {
e.Handled = true;
// Handle the key press event and display nothing.
}
}
}
If it is a windows application.
I'm listening but I only speak GEEK.
|
|
|
|
|
Hi again,
If I enter a value greater than Max, I can by having keyUp event, get the NumericUpDown to validate efter each input but the value and text is reset to a valid value before I get to know what the nonvalid value actually was or even if the entered value was not valid.
Can I get the actual value before it is reset or get an indication that the value is reset?
Regards,
TooDoo
|
|
|
|
|
In the KeyPress Event the value would be e.KeyChar as a char datatype or e.KeyChar.ToString() for a string datatype.
I'm listening but I only speak GEEK.
|
|
|
|
|
Hi again,
by that I get the just entered char but and to check the total value I have the merge the already entered value and the just entered concidering position also.
But for a very short time before the value out of range is set to Max/Min you get to see the total value added. Can't I get it somehow?
Regards,
TooDoo
|
|
|
|
|
OK, then move the KeyPress code to the Validating Event and alter the following:
if ( Int16.TryParse( textBox1.Text.Trim() + e.KeyChar.ToString(), out num ) ) {
TO
if ( Int16.TryParse( textBox1.Text.Trim(), out num ) ) {
Make sure take the Textbox Property CausesValidation = true;
I'm listening but I only speak GEEK.
|
|
|
|
|
Dear All,
I want to add menuitems at run-time. If I use a menu, I can use the following method :
menuItemFile.MenuItems.Add(0, new System.Windows.Forms.MenuItem("-"));<br />
foreach (FileInfo XmlDescriptorFile in XmlDescriptorFiles)<br />
{<br />
try<br />
{<br />
blah blah ...
Is there a way to do exactly the same thing with a MenuStrip ?
Cheers,
|
|
|
|
|
DropDownItems.Add(...)
led mike
|
|
|
|
|
I can't for the life of me get this to work. My internet crawl has produced nothing.
Executing this code:
<br />
using (StreamReader sr = new StreamReader(f+"\\CDINDEX.txt"))<br />
{<br />
string line;<br />
<br />
while ((line = sr.ReadLine()) != null)<br />
{<br />
Console.WriteLine(line);<br />
}<br />
}<br />
I get the following exception: " The given path's format is not supported."
Using a messagebox the path clearly reads: "c:\test\CDINDEX.txt" which is the correct and true path to the file.
Can anyone help me as to why for some reason Microsoft decided today not to like me?
Thanks.
|
|
|
|
|
Lordveralix wrote: using (StreamReader sr = new StreamReader(f+"\\CDINDEX.txt"))
go with
using (StreamReader sr = new StreamReader(f+"\CDINDEX.txt"))
Hope it is the error.
Regards,
Satips.
|
|
|
|
|
The reason \\ is used is because \ denotes an escape sequence like \t or \n. A double slash \\ allows a slash. Using just \CDINDEX.txt returns an error for invalid escape.
|
|
|
|
|
There's a bunch of useful methods in System.IO.Path to make sure your path is correct. I'd use Path.Combine() just to be sure in this case...
Standards are great! Everybody should have one!
|
|
|
|
|
Try
string MyPath = f+"\\CDINDEX.txt";
Put a break after this and look at the value of MyPath. I would hope there would be some obvious error, but if not then copy the value and put it in the command prompt and make sure it works.
topcoderjax
|
|
|
|
|
What is f?
Could you quickly try
<br />
using (StreamReader sr = new StreamReader("C:\\test\\CDINDEX.txt"))<br />
instead?
Arti
|
|
|
|
|
Like Bekjong said, the correct way to put a path together would be to use the Path class and it's Combine method:
string filepath = Path.Combine(f, @"CDINDEX.txt");
using (StreamReader sr = new StreamReader(filepath)
|
|
|
|
|
I have added a group of controls to a form using code and added the controls to the form collection.
How can I later iterate through all of the controls on the form and drop the (Textbox) controls that I added earlier?
I can use a property in the control to do the ID, but I need code to iterate through all controls, and to drop selected controls.
A code sample would be great!
Joseph Stanish
|
|
|
|
|
The easiest way to remove a control is to keep the reference from when you created it, and use use the Remove method:
SomeParent.Controls.Remove(SomeTextbox);
---
single minded; short sighted; long gone;
|
|
|
|
|
(I guess I could dim a collection of textboxes and add each of my textbox controls to this collection, and then when it is time to remove these controls go throught this collection and drop the controls form the form ...
Is the correct?
That sounds like a very good idea! Could you please provide a brief code sample?
Joseph Stanish
|
|
|
|
|
What's so hard about iterating through your controls? If you can add the pro grammatically it's just as easy to remove them, isn't it?
this.SuspendLayout();
Collection<Control> temp = new Collection<Control>();
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox) && c.Tag == "drop me")
{
temp.Add(c);
}
}
foreach (Control c in temp)
{
this.Controls.Remove(c);
c.Dispose();
}
temp = null;
this.ResumeLayout();
Standards are great! Everybody should have one!
|
|
|
|
|
Perfect. Thanks.
Joseph Stanish
|
|
|
|
|
Hello,
Very nice idea to copy the instances in a seperate Collection instead of Cloning the Collection!
Never thought of this bevor!
Thanks for that!
All the best,
Martin
|
|
|
|
|
Bekjong wrote: if (c.GetType() == typeof(TextBox)
or better yet
if (c is TextBox && ... )
|
|
|
|
|
Umm. Iterate through the Controls collection of the Form and set the Parent to null for the control you want removed from the form.
Marc
Thyme In The CountryInteracxPeople are just notoriously impossible. --DavidCrow There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith
|
|
|
|
|
I need a code sample
Joseph Stanish
|
|
|
|
|
Maybe a "please" would help!
|
|
|
|
|
here you are ... "Please"
by the way, you do not need to reply; instead of searching the form, I am going to keep a list of added controls, I will then iterate through my own list to drop the correct controls.
Thanks.
Joseph Stanish
|
|
|
|