Click here to Skip to main content
15,887,175 members
Home / Discussions / C#
   

C#

 
GeneralRe: conditions problem Pin
Luis Alonso Ramos4-Aug-05 19:12
Luis Alonso Ramos4-Aug-05 19:12 
GeneralRe: conditions problem Pin
nidhelp4-Aug-05 19:16
nidhelp4-Aug-05 19:16 
GeneralRe: conditions problem Pin
Christian Graus4-Aug-05 19:23
protectorChristian Graus4-Aug-05 19:23 
GeneralRe: conditions problem Pin
nidhelp4-Aug-05 19:37
nidhelp4-Aug-05 19:37 
GeneralRe: conditions problem Pin
Christian Graus4-Aug-05 19:45
protectorChristian Graus4-Aug-05 19:45 
GeneralRe: conditions problem Pin
Weiye Chen4-Aug-05 17:51
Weiye Chen4-Aug-05 17:51 
GeneralRe: conditions problem Pin
nidhelp4-Aug-05 18:41
nidhelp4-Aug-05 18:41 
GeneralRe: conditions problem Pin
J4amieC4-Aug-05 22:39
J4amieC4-Aug-05 22:39 
I got this to work with a combination of both ways. Using the Validating method, and a RegularExpression.

Just to confirm - the pattern you wanted was:

s,S,f or F at the start
7 numbers
and upper or lowercase letter at the end

which I represented as the reg ex: [sSfF]\d{7}[a-zA-Z]

In the form designer, highlight your textbox and look at the Properties grid. Swap it to events using the little lightning symbol. Scroll until you see an event named Validating. Double-Click inside the textbox to the right of the event name - this should do 2 things
1) Jump to the code-behind view
2) Enter the stub of an event...mine started life looking liek this:

private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
	
}


The two arguments passed to this method are very useful.

sender - is the thing that sent this event (the textbox in this case)
e - is the event arguments, in this case of type CancelEventArgs. This type of event arguments has a Cancel property which can be set to true for this event to indicate that the contexts of the text box are NOT valid (hence the event name validating.

So back to the problem. In pseudo-code:

if textbox is not valid<br />
set Cancel to true


this can be done in the following two lines

TextBox tb = sender as TextBox;<br />
e.Cancel = !Regex.Match(tb.Text,@"[sSfF]\d{7}[a-zA-Z]").Success;


However, to explain this

1) Cast the sender to type TextBox
TextBox tb = sender as TextBox

2)Determine if its text is valid:
bool isValid = Regex.Match(tb.Text,@"[sSfF]\d{7}[a-zA-Z]").Success;

3)if not valid, set Cancel to false
e.Cancel = !isValid;

end result: until you enter the correct sequence of characters into the textbox you are unable to tab out or close the form.
GeneralRe: conditions problem Pin
nidhelp4-Aug-05 23:24
nidhelp4-Aug-05 23:24 
GeneralRe: conditions problem Pin
J4amieC4-Aug-05 23:46
J4amieC4-Aug-05 23:46 
GeneralRe: conditions problem Pin
nidhelp5-Aug-05 0:02
nidhelp5-Aug-05 0:02 
GeneralRe: conditions problem Pin
mav.northwind5-Aug-05 2:59
mav.northwind5-Aug-05 2:59 
GeneralRe: conditions problem Pin
nidhelp7-Aug-05 15:14
nidhelp7-Aug-05 15:14 
GeneralRe: conditions problem Pin
nidhelp5-Aug-05 0:00
nidhelp5-Aug-05 0:00 
GeneralRe: conditions problem Pin
J4amieC5-Aug-05 0:18
J4amieC5-Aug-05 0:18 
Generalabout finding forms Pin
papyrus_lei4-Aug-05 16:34
papyrus_lei4-Aug-05 16:34 
GeneralRe: about finding forms Pin
Mohamad Al Husseiny4-Aug-05 17:15
Mohamad Al Husseiny4-Aug-05 17:15 
GeneralRe: about finding forms Pin
papyrus_lei4-Aug-05 17:25
papyrus_lei4-Aug-05 17:25 
GeneralRe: about finding forms Pin
Mohamad Al Husseiny4-Aug-05 17:27
Mohamad Al Husseiny4-Aug-05 17:27 
GeneralRe: about finding forms Pin
Mohamad Al Husseiny4-Aug-05 17:47
Mohamad Al Husseiny4-Aug-05 17:47 
GeneralRe: about finding forms Pin
papyrus_lei4-Aug-05 18:02
papyrus_lei4-Aug-05 18:02 
GeneralRe: about finding forms Pin
Mohamad Al Husseiny4-Aug-05 18:05
Mohamad Al Husseiny4-Aug-05 18:05 
QuestionExcluding xsd files from documentation Pin
David Wong4-Aug-05 11:22
David Wong4-Aug-05 11:22 
GeneralSaving new Word docs directly to SQL Server. Pin
roccom4-Aug-05 11:08
roccom4-Aug-05 11:08 
GeneralRe: Saving new Word docs directly to SQL Server. Pin
Mohamad Al Husseiny4-Aug-05 12:05
Mohamad Al Husseiny4-Aug-05 12:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.