|
Use this
public bool IsNumeric(string s)<br />
{<br />
try<br />
{<br />
Int32.Parse(s);<br />
}<br />
catch<br />
{<br />
return false;<br />
}<br />
return true;<br />
}<br />
Nikhil
|
|
|
|
|
Thanks for your source code
just now i also found another way to validate the numeric field, I would like to share it with you :
public bool chkNumeric(string strText)
{
char[] chars = strText.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] > 57 || chars[i] < 48)
{
return false;
}
}
return true;
}
angela
|
|
|
|
|
this code wil not handle -ve numbers and number exceeding int max limit
Regards
Shajeel
|
|
|
|
|
|
Be careful. The exception police will get you. As has been explained to me, relying on exceptions is bad practice. Use TryParse instead of Parse and it returns a boolean to let you know if it worked or not.
|
|
|
|
|
Well you could just do like this:
try
{
Convert.ToInt32(txtstring);
}
catch
{
MessageBox.Show("You must input a numeric value!");
return;
}
-- modified at 2:53 Thursday 8th June, 2006
|
|
|
|
|
And whats about this...
if(!char.IsNumber(e.KeyChar)
e.Handled=true;
write it in the text control's KeyPress event.
_____________________________
Success is not something to wait for, its something to work for.
|
|
|
|
|
A couple of notes about the other replies:
1. You don't want to be catching exceptions as part of the normal logic flow. This is a well-accepted standard in software development.
2. You don't want to do this in a KeyPress event since that won't handle pasting code from another source.
For a more robust equivalent to VB's IsNumeric method see: http://www.tangiblesoftwaresolutions.com/Articles/CSharp%20Equivalent%20to%20IsNumeric.htm[^]
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
Clear VB: Cleans up VB.NET code
|
|
|
|
|
David Anton wrote: 2. You don't want to do this in a KeyPress event since that won't handle pasting code from another source.
Hi David Anton
I didn't understand this point!
_____________________________
Success is not something to wait for, its something to work for.
|
|
|
|
|
Standard Windows copy/paste doesn't trigger KeyPress events.
User the TextChanged event to capture pasting of text also.
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
Clear VB: Cleans up VB.NET code
|
|
|
|
|
David Anton wrote: Standard Windows copy/paste doesn't trigger KeyPress events.
User the TextChanged event to capture pasting of text also.
of course! I checked it last day and found the same situation. It means that we can't rely on only keyPress event for validation, we have to check some other events too; like, as you mentioned, TextChanged etc.
Thanks.
_____________________________
Success is not something to wait for, its something to work for.
|
|
|
|
|
this code may help you to check whether the given value is numeric or not
using System.Text.RegularExpressions;
public static bool CheckNumeric(string strText)
{
return CheckRegExPattern(@"^[0-9]+$", strText.Trim());
}
-- modified at 7:05 Friday 9th June, 2006
|
|
|
|
|
Suamal wrote: CheckRegExPattern
What is this? I didn't find it.
Suamal wrote: using System.Text.RegularExpressions;
public static bool CheckNumeric(string strText)
{
return CheckRegExPattern(@"^[0-9]+$", strText.Trim());
}
While taking idea from this I tryed to implement it like this:
using System.Text.RegularExpressions;<br />
public bool CheckNumeric(string strtext)<br />
{<br />
System.Text.RegularExpressions.Regex rr = new Regex(@"^[0-9]+$");<br />
return rr.IsMatch(strtext); <br />
}<br />
<br />
MessageBox.Show(this.CheckNumeric(TextBox1.Text).ToString());
Its working, but i'm unable to interpret this @"^[0-9]+$"
Thanks and Best Regards
_____________________________
Success is not something to wait for, its something to work for.
|
|
|
|
|
At last found the site
.NET Framework Regular Expressions [^]
Thanks God
and Thanks to the forum
_____________________________
Success is not something to wait for, its something to work for.
|
|
|
|
|
Hi,
I need to write a windows service that will take a backup of the database tables and their data to a file when the server goes down and at the end of day everyday. Can anybody tell me how do I go about it? For the back-up do I need to use SQL-DMO or is there some other way out?
Thanks in advance,
Aakar.
|
|
|
|
|
For my money, SQL-DMO is the way to go. It allows you to stay within the confines of the application and is just a cleaner implementation (IMHO!)
I attempted to invoke sqlo from within C# and it did work, but catching any exceptions was a bit awkward.
/dave
----
If you always do what you always done, you'll always get what you've always got - Anonymous
|
|
|
|
|
My application saves the Size and Location properties of the main form in a configuration file. When the program runs I first create the form, set the Size and Location properties, then call the form's Show method.
I noticed after closing and restarting the application the form would grow slightly larger, which puzzled me. If I output the value of the Size property immeadiately before and after I call the form's Show method I can see that the form grows in height by exactly 19 pixels. No matter what I set as the value of the Size property, the height always seemed to increase by 19 pixels.
I had a hunch maybe 19 pixels was the height of the title bar. I increased the size of the title bar font in Windows, but my form still grew by 19. However, when I increased the size of the menu font my form grew by a larger amount.
Other than the menu I have two controls in the form. A ListView control with the Dock property set to Fill and a StatusBar control with the Dock property set to Bottom.
Any ideas?
Thanks,
Chris
|
|
|
|
|
Maybe you can try saving and setting the form's ClientSize instead.
I haven't done any testing but AFAIK the ClientSize does not include title bar height and width etc.
Edbert
Sydney, Australia
|
|
|
|
|
I had similar problem long time ago, but I don't remember exactly how I solved it. It was sort of the caption bar that added up every time.
- It's easier to make than to correct a mistake.
|
|
|
|
|
I am definately using Size, not ClientSize. I also believe it is the height of the menu bar that is being added to the form, not the height of the title bar.
|
|
|
|
|
Hello,
I have 11 instances of a control (representing 11 audio channels). This control has, among others, two public boolean properties called SOLO and MUTE. These can be turned on and off by means of two checkboxes, say, chkSolo and chkMute.
This is what I need to do:
1) If the user decides to "solo a channel", i.e. set the SOLO property on one of the controls to TRUE, the MUTE property of the other 10 controls should be set.
2) Unchecking the chkSolo box (setting SOLO to FALSE) should "unmute" the other 10 controls.
What's the best way of doing this? Should I have a collection of controls? How do I identify which of the controls is being Soloed?
Thanks for your help!
|
|
|
|
|
I'd say the best way is to create 2 user controls.
The first control should contain the channel, the checkboxes Solo and Mute. Let's call it Channel.
When chkSolo is clicked, the control should raise an event to notify it.
The second control should handle the collection of the first controls. Let's call it Mixer.
This control should handle chkSolo click event (preferably a custom event returning your Channel control); Everytime a chkSolo is checked, it needs to save (either the Channel control's index or a reference to the Channel control) which of the channel control is checked, and if it is to be soloed to go through the collection and mute the rest of the controls. If it is to be unchecked the rest of the controls should be unmuted.
Therefore, your Mixer will have handled the mute/unmute and you can get the control being soloed (if any) from it.
Edbert
Sydney, Australia
|
|
|
|
|
Edbert, thanks for your reply! Could you give me an example of what the code for the event and the second control (the collection of controls) should look like? Just an example would be great.
Thanks again!
|
|
|
|
|
Here's a small example (I'm blind coding, it might have some syntax error):
For Channel, you need to raise an event whenever the checkbox is checked:
public event SoloChangedHandler SoloChanged;
public delegate void SoloChangedHandler(Channel sender);
private void OnSoloChanged()
{
if (SoloChanged != null)
SoloChanged(this);
}
private void chkSolo_Checked(object sender, EventArgs e)
{
OnSoloChanged();
}
public bool IsSolo
{
get { return chkSolo.Checked; }
set { chkSolo.Checked = value; }
}
For Mixer, you need to either keep a reference on the selected Channel, or the index, or both:
private int selectedIndex = -1;
private Channel selectedChannel;
private void ctrlSolo_SoloChanged(Channel sender)
{
if (sender.IsSolo)
{
this.selectedIndex = this.Controls.GetChildIndex(sender);
this.selectedChannel = sender;
foreach (Channel channel in this.Controls)
{
if (channel != sender)
{
channel.IsSolo = false;
channel.IsMute = true;
}
}
}
else
{
this.selectedIndex = -1;
this.selectedChannel = null;
foreach (Channel channel in this.Controls)
{
channel.IsSolo = false;
channel.IsMute = false;
}
}
}
That's it. Now you just need to add properties to access the collection of Channels, the SelectedIndex, and the SelectedChannel.
Hope that helps!
Edbert
Sydney, Australia
|
|
|
|
|
I want to make a program to view some XML files. Right now just looking at the files is very confusing and intimidating to most people. I would like to make it more intuitive and easier to work with (the xml).
I'm in a planning phase right now, and was wondering what would be he more graceful way of doing loading the XML? I just want to do it with style.
The xml files are like this..
1 "package" root node
any number of "method" nodes
- one "paramlist" node per method
- any number of "params" per "paramlist"
- one "actionlist" node per method
- any number of "action" nodes per actionlist
that's pretty much the XML each node has few attributes too. I need to make a schema for it too.
So what would be the best way to convey the XML to a user? The only thing I can think of is a tree control. Any other ideas?
So how would I load the xml file each time? how would I save it from the tree?
I'm thinking seralization, does that sound right?
/\ |_ E X E GG
|
|
|
|