[RESOLVED] (2005)Having problems from the start
In an attempt to learn C#.NET, I took an existing (very small) application and decided to rewrite it.
The VB code looks like this (on text changed event):
if Trim(txtunits.text) <> "" then
.....If Not isnumeric(txtunits.text) then
.........Msgbox("Units must be numeric", msgboxstyle.exclamation)
.........txtunits.setfocus
.....endif
endif
It works fine. If they try to enter a non-numeric character they get an error message.
Now, in C#, I have this so far:
private void txtUnits_TextChanged(object sender, EventArgs e)
{
.....if (txtUnits.text != "")
..........if (!isnumeric(txtUnits.Text))
..........{
...............MessageBox.Show("Units must be numeric", "TVAL", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
...............txtUnits.Focus;
..........}
}
I am getting these errors (error 3 refers to the set focus command)
Error 1 'System.Windows.Forms.TextBox' does not contain a definition for 'text' I:\VB NET Source Code\C#\TVAL\TVAL\TVAL\Form1.cs 25 25 TVAL
Error 2 The name 'isnumeric' does not exist in the current context I:\VB NET Source Code\C#\TVAL\TVAL\TVAL\Form1.cs 26 22 TVAL
Error 3 Only assignment, call, increment, decrement, and new object expressions can be used as a statement I:\VB NET Source Code\C#\TVAL\TVAL\TVAL\Form1.cs 29 21 TVAL
Can someone give me a start? I use code as templates. So, once this is figured out, I can propogate it over my project. I have a few manuals and tutorials, but none seem to address this specifically.
Re: (2005)Having problems from the start
1) C# is case sensitive! Text does not equal text.
2-3) The IsNumeric function is a VB function. It does not exist in C#. If you are looking for a integer, for example, you could try using Integer.Parse instead; or perhaps a regular expression to match what you are looking for. You can put the code that is validating your TextBox in its Validating event as well
c# Code:
// Validate my TextBox.
private void textBox1_Validating(object sender, CancelEventArgs e)
{
int result;
if (!int.TryParse(this.textBox1.Text, out result))
{
MessageBox.Show("That's not an integer.");
e.Cancel = true; // Resets the focus.
}
}
3) Watch out for this. You'll have to pay a little bit closer of attention as the C# IDE will not add the () for you.
Hope that helps. :)
Re: (2005)Having problems from the start
I found this. It seems to work.
string strScore;
int intScore;
strScore = txtUnits.Text;
bool blnInput;
blnInput = Int32.TryParse(strScore, out intScore);
if (blnInput == false)
MessageBox.Show("Units must be numeric", "TVAL", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
Re: [RESOLVED] (2005)Having problems from the start
Yep. There it is. TryParse
:)
EDIT: I noticed I said Parse in my notes, but TryParse in the code. TryParse is the one.
Re: [RESOLVED] (2005)Having problems from the start