473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Markus
6,050 Expert 4TB
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)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Text.RegularExpressions;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         string Name;
  16.         string Nameref;
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void setName_Click(object sender, EventArgs e)
  24.         {
  25.             // set the value from nameInput to Name
  26.             // we shall check for a value first
  27.             int NameLength = nameInput.Text.Length;
  28.             Regex Pattern = new Regex( "[^a-zA-Z]" );
  29.             if ( NameLength < 1 )
  30.             {
  31.                 MessageBox.Show( "Please enter a name", "Error" );
  32.             }
  33.             else if( Pattern.IsMatch( nameInput.Text ) )
  34.             {
  35.                 MessageBox.Show( "Please enter only letters", "Error" );
  36.             }
  37.             else
  38.             {
  39.                 Name = nameInput.Text;
  40.                 nameInput.Text = "";
  41.                 this.Text = "Name set";
  42.             }
  43.         }
  44.  
  45.         private void nameReminder_Click(object sender, EventArgs e)
  46.         {
  47.             // check if Name is set
  48.             int NameLength = nameInput.Text.Length;
  49.             if( NameLength < 1 )
  50.             {
  51.                 MessageBox.Show( "Name has not been set yet. Please do so now", "Error" );
  52.                 nameInput.Focus();
  53.             }
  54.             else
  55.             {
  56.                 MessageBox.Show( Name, "Your name" );
  57.             }
  58.         }
  59.     }
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?! >=[
Oct 16 '08 #1
3 1816
r035198x
13,262 8TB
So the value disappears only if the name was set?
Why don't you just remove the line nameInput.Text = ""; then?
Oct 16 '08 #2
tlhintoq
3,525 Expert 2GB
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"
Oct 16 '08 #3
Markus
6,050 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1.         private void nameReminder_Click(object sender, EventArgs e)
  2.         {
  3.             if( Name == null )
  4.             {
  5.                 MessageBox.Show( "Name has not been set yet. Please do so now", "Error" );
  6.                 nameInput.Focus();
  7.             }
  8.             else
  9.             {
  10.                 MessageBox.Show( Name, "Your name" );
  11.             }
  12.         }
  13.  
Oct 16 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
5
by: Steve S | last post by:
Heres what I want to do...User types into a texbox, clicks a button, the button saves that text to a file. The problem is that when I click the submit button, any changes made to the textbox are...
4
by: Tom Vergote | last post by:
Hi all, I have this page with quite a few textboxes which require viewstate ON. However, i execute some Control.Addatributes on them so the take very much viewstate space (three times a normal...
1
by: Shahid Juma | last post by:
Hi, I have a form and when the user clicks save, it will add all the details to the database. However, when I add it to the database, how do I clear the form values? What happens is that it...
1
by: Michael via .NET 247 | last post by:
I have a datagrid with the columns defined in the aspx <columns> <asp:TemplateColumn HeaderText="Hours" ItemStyle-Width="1%"ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:TextBox...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
0
by: Dabbler | last post by:
I have a FormView with a bunch of fields on it. One of those fields is used to add a value to a DropDownList on the FormView by allowing the user to enter a date and clicking a button. In the...
1
by: David | last post by:
Hi, I have a continuous form with 'x' amount of records. 1 field on each record is a number, of which I have a field at the top of the form which just shows the running sum. If I enter a new...
1
by: swatipriyadarsani | last post by:
hi!!! i m creating website ...i m using ajax.When the page loads..there are 2 combo box one for state and other for city.on select of state city will get populated and on select of city my age will...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.