Results 1 to 5 of 5

Thread: [RESOLVED] (2005)Having problems from the start

  1. #1

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Resolved [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.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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:
    1. // Validate my TextBox.
    2.         private void textBox1_Validating(object sender, CancelEventArgs e)
    3.         {
    4.             int result;
    5.             if (!int.TryParse(this.textBox1.Text, out result))
    6.             {
    7.                 MessageBox.Show("That's not an integer.");
    8.                 e.Cancel = true; // Resets the focus.
    9.             }
    10.         }
    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.
    c# Code:
    1. this.textBox1.Focus();

    Hope that helps.

  3. #3

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    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);
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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.

  5. #5

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [RESOLVED] (2005)Having problems from the start

    So much to learn...
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width