After clearing textbox, saved value is lost. [c#]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    After clearing textbox, saved value is lost. [c#]

    Ok, so I'm just messing around with c# at the moment, learning some stuff -- it is surprisingly similar to JavaScript :S -- and I hit a snag: I'm grabbing the value of a texbox and then setting the textbox to have zilch in it. Consider the below code (lines 38 - 42)

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            string Name;
            string Nameref;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void setName_Click(object sender, EventArgs e)
            {
                // set the value from nameInput to Name
                // we shall check for a value first
                int NameLength = nameInput.Text.Length;
                Regex Pattern = new Regex( "[^a-zA-Z]" );
                if ( NameLength < 1 )
                {
                    MessageBox.Show( "Please enter a name", "Error" );
                }
                else if( Pattern.IsMatch( nameInput.Text ) )
                {
                    MessageBox.Show( "Please enter only letters", "Error" );
                }
                else
                {
                    Name = nameInput.Text;
                    nameInput.Text = "";
                    this.Text = "Name set";
                }
            }
    
            private void nameReminder_Click(object sender, EventArgs e)
            {
                // check if Name is set
                int NameLength = nameInput.Text.Length;
                if( NameLength < 1 )
                {
                    MessageBox.Show( "Name has not been set yet. Please do so now", "Error" );
                    nameInput.Focus();
                }
                else
                {
                    MessageBox.Show( Name, "Your name" );
                }
            }
        }
    However, when I check the value of Name it's empty. I know this is because I'm setting the nameInput (textbox) to empty, so is there a way I can hold on to the value?

    PS: Anyone find it annoying that line numbers are disabled by default?! >=[
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    So the value disappears only if the name was set?
    Why don't you just remove the line nameInput.Text = ""; then?

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      If I'm reading this right, you have a member variable named "Name".
      You have a textbox named "nameInput" .
      You set your variable to the text of the text box then clear the text of the textbox.

      You say "whenever I check the value of Name its empty"... but I don't see anyplace you check the value of "Name". I see a few places you check against the value of the textbox.text that you emptied. But unless I'm blind, I don't see any comparrisons against "Name"

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by tlhintoq
        If I'm reading this right, you have a member variable named "Name".
        You have a textbox named "nameInput" .
        You set your variable to the text of the text box then clear the text of the textbox.

        You say "whenever I check the value of Name its empty"... but I don't see anyplace you check the value of "Name". I see a few places you check against the value of the textbox.text that you emptied. But unless I'm blind, I don't see any comparrisons against "Name"
        You're right; after revising my code, I realised I was comparing against the textbox and not Name.

        Revised code:
        Code:
                private void nameReminder_Click(object sender, EventArgs e)
                {
                    if( Name == null )
                    {
                        MessageBox.Show( "Name has not been set yet. Please do so now", "Error" );
                        nameInput.Focus();
                    }
                    else
                    {
                        MessageBox.Show( Name, "Your name" );
                    }
                }

        Comment

        Working...