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) -
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?! >=[
3 1816
So the value disappears only if the name was set?
Why don't you just remove the line nameInput.Text = ""; then?
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"
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: -
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" );
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| | |