Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. conditions problem

conditions problem

Scheduled Pinned Locked Moved C#
questionhelp
23 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nidhelp
    wrote on last edited by
    #1

    i've to set some conditions to my program. user has to enter a string of 2 letters + 7 numbers in this order eg. x1234567y how do i set the conditions to make sure the order is correct? thanks

    M W 2 Replies Last reply
    0
    • N nidhelp

      i've to set some conditions to my program. user has to enter a string of 2 letters + 7 numbers in this order eg. x1234567y how do i set the conditions to make sure the order is correct? thanks

      M Offline
      M Offline
      Mohamad Al Husseiny
      wrote on last edited by
      #2

      use regular expression MCAD

      N 1 Reply Last reply
      0
      • M Mohamad Al Husseiny

        use regular expression MCAD

        N Offline
        N Offline
        nidhelp
        wrote on last edited by
        #3

        what do u mean by regular expression? i was told to do onkeypress event on textbox.. how do i start doing that, do u know?

        C 1 Reply Last reply
        0
        • N nidhelp

          what do u mean by regular expression? i was told to do onkeypress event on textbox.. how do i start doing that, do u know?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          nidhelp wrote: what do u mean by regular expression? A regular expression is a language that's used to define the format of an input string. Google will tell you more. nidhelp wrote: i was told to do onkeypress event on textbox.. how do i start doing that, do u know? In the designer, click on the text box, in the properties, tick the lightning flash. I'd suggest that textchanged is perhaps a better event to go for. What's your level of experience here ? Are you just starting ? Why do you have this requirement ? Is it homework ( I'm still happy to help you if it is ) ? Once you have an event that tells you the text has changed, a regular expression is still the way to know the format of the string. Look up Expresso on this site, it's an app that helps you build regex, and a great learning tool. Christian Graus - Microsoft MVP - C++

          N 1 Reply Last reply
          0
          • N nidhelp

            i've to set some conditions to my program. user has to enter a string of 2 letters + 7 numbers in this order eg. x1234567y how do i set the conditions to make sure the order is correct? thanks

            W Offline
            W Offline
            Weiye Chen
            wrote on last edited by
            #5

            If you are using a textbox, you can handle the key press event and process the necessary logic to allow/disallow certain characters from being entered. Here's some code below. I have not tested it but the logic is there i believe:

            private void tbEditor_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
            // Always allow processing of backspace character
            if(e.KeyChar == (char)8)
            e.Handled = false;

            // Let system process key if it is a letter and we are now entering the 1st or 9th character
            else if(Char.IsLetter(e.KeyChar) == true && (tbEditor.Text.Length == 0 || tbEditor.Text.Length == 8))
            e.Handled = false;

            // Let system process key if it is a digit and we are now entering the 2nd to 8th character
            else if(Char.IsDigit(e.KeyChar) == true && (tbEditor.Text.Length > 0 || tbEditor.Text.Length < 8))
            e.Handled = false;

            // Do not process anything else
            else
            e.Handled = true;
            }

            Weiye Chen Life is hard, yet we are made of flesh...

            N 1 Reply Last reply
            0
            • C Christian Graus

              nidhelp wrote: what do u mean by regular expression? A regular expression is a language that's used to define the format of an input string. Google will tell you more. nidhelp wrote: i was told to do onkeypress event on textbox.. how do i start doing that, do u know? In the designer, click on the text box, in the properties, tick the lightning flash. I'd suggest that textchanged is perhaps a better event to go for. What's your level of experience here ? Are you just starting ? Why do you have this requirement ? Is it homework ( I'm still happy to help you if it is ) ? Once you have an event that tells you the text has changed, a regular expression is still the way to know the format of the string. Look up Expresso on this site, it's an app that helps you build regex, and a great learning tool. Christian Graus - Microsoft MVP - C++

              N Offline
              N Offline
              nidhelp
              wrote on last edited by
              #6

              What's your level of experience here ? Are you just starting ? Why do you have this requirement ? Is it homework? i am v new to c#. for more details, i've replied u in my other post regarding tab page that day. =) ok i've looked through some codes on regular expression.. do i have to simply copy paste the codes i need in my program and it'll know what format i want, or do i've to go somewhere to click/select/fill in something to specify my format of an input string? also how's a textchanged better than onkeypress? thanks!

              C 1 Reply Last reply
              0
              • N nidhelp

                What's your level of experience here ? Are you just starting ? Why do you have this requirement ? Is it homework? i am v new to c#. for more details, i've replied u in my other post regarding tab page that day. =) ok i've looked through some codes on regular expression.. do i have to simply copy paste the codes i need in my program and it'll know what format i want, or do i've to go somewhere to click/select/fill in something to specify my format of an input string? also how's a textchanged better than onkeypress? thanks!

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                nidhelp wrote: i am v new to c#. for more details, i've replied u in my other post regarding tab page that day. =) Ah... welcome back :-) BTW, you can highlight my replies and hit the 'Quote selected text' button to get the format you see when I quote you. nidhelp wrote: do i have to simply copy paste the codes i need in my program and it'll know what format i want, or do i've to go somewhere to click/select/fill in something to specify my format of an input string? \d is a number, \w is a character in a word ( alpha ), and {} allow you to show number of items If this regex matches anything \d\w{6}\d and the number of characters is 8, then you have a number, six letters and a number. Was that the format you wanted ? Christian Graus - Microsoft MVP - C++

                N 1 Reply Last reply
                0
                • C Christian Graus

                  nidhelp wrote: i am v new to c#. for more details, i've replied u in my other post regarding tab page that day. =) Ah... welcome back :-) BTW, you can highlight my replies and hit the 'Quote selected text' button to get the format you see when I quote you. nidhelp wrote: do i have to simply copy paste the codes i need in my program and it'll know what format i want, or do i've to go somewhere to click/select/fill in something to specify my format of an input string? \d is a number, \w is a character in a word ( alpha ), and {} allow you to show number of items If this regex matches anything \d\w{6}\d and the number of characters is 8, then you have a number, six letters and a number. Was that the format you wanted ? Christian Graus - Microsoft MVP - C++

                  N Offline
                  N Offline
                  nidhelp
                  wrote on last edited by
                  #8

                  o thanks for teaching me about quote selected text =) my format is a letter, 7 numbers, a letter >> \w\d{7}\w correct? hmmmm.. i need to ensure that the user only types s/S or f/F for the first letter.. how is that possible? i clicked on the lightning button n looked through the onkeypress already but it still gives me no clue as to how to even use the onkeypress to do my app..

                  C 1 Reply Last reply
                  0
                  • W Weiye Chen

                    If you are using a textbox, you can handle the key press event and process the necessary logic to allow/disallow certain characters from being entered. Here's some code below. I have not tested it but the logic is there i believe:

                    private void tbEditor_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
                    {
                    // Always allow processing of backspace character
                    if(e.KeyChar == (char)8)
                    e.Handled = false;

                    // Let system process key if it is a letter and we are now entering the 1st or 9th character
                    else if(Char.IsLetter(e.KeyChar) == true && (tbEditor.Text.Length == 0 || tbEditor.Text.Length == 8))
                    e.Handled = false;

                    // Let system process key if it is a digit and we are now entering the 2nd to 8th character
                    else if(Char.IsDigit(e.KeyChar) == true && (tbEditor.Text.Length > 0 || tbEditor.Text.Length < 8))
                    e.Handled = false;

                    // Do not process anything else
                    else
                    e.Handled = true;
                    }

                    Weiye Chen Life is hard, yet we are made of flesh...

                    N Offline
                    N Offline
                    nidhelp
                    wrote on last edited by
                    #9

                    hi im not supposed to disable any keys from being entered into the textbox.. the user is free to type n my app shd only open up a form when he types in a correct format.. correct format is a letter, 7 numbers, a letter.. i also have to make sure the first letter is S or s or F or f.. as long as the first letter is not correct, the form will not open..

                    J 1 Reply Last reply
                    0
                    • N nidhelp

                      o thanks for teaching me about quote selected text =) my format is a letter, 7 numbers, a letter >> \w\d{7}\w correct? hmmmm.. i need to ensure that the user only types s/S or f/F for the first letter.. how is that possible? i clicked on the lightning button n looked through the onkeypress already but it still gives me no clue as to how to even use the onkeypress to do my app..

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      nidhelp wrote: hmmmm.. i need to ensure that the user only types s/S or f/F for the first letter.. how is that possible? A regex can contain more specific info, [] is used for character groups. [sSfF]\d{7}[sSfF] will do it. OnKeyPress will give you a function that runs when a key is pressed. In that function, you need to get the text from the textbox and check it. Something like this: TextBox tb = sender as TextBox; string input = tb.Text; if (input.Length == 9 && System.Text.RegularExpressions.Regex.Match(input, @"[sSfF]\d{7}[sSfF]").Success) { // The string is correct } else { // Don't forget that unless the length is 9, they could still be entering, so don't put a warning box here. A better place for that would be the lose focus event. } Christian Graus - Microsoft MVP - C++

                      L N 2 Replies Last reply
                      0
                      • C Christian Graus

                        nidhelp wrote: hmmmm.. i need to ensure that the user only types s/S or f/F for the first letter.. how is that possible? A regex can contain more specific info, [] is used for character groups. [sSfF]\d{7}[sSfF] will do it. OnKeyPress will give you a function that runs when a key is pressed. In that function, you need to get the text from the textbox and check it. Something like this: TextBox tb = sender as TextBox; string input = tb.Text; if (input.Length == 9 && System.Text.RegularExpressions.Regex.Match(input, @"[sSfF]\d{7}[sSfF]").Success) { // The string is correct } else { // Don't forget that unless the length is 9, they could still be entering, so don't put a warning box here. A better place for that would be the lose focus event. } Christian Graus - Microsoft MVP - C++

                        L Offline
                        L Offline
                        Luis Alonso Ramos
                        wrote on last edited by
                        #11

                        Christian Graus wrote: // Don't forget that unless the length is 9, they could still be entering, so don't put a warning box here. A better place for that would be the lose focus event. I recommend the Validating event. That's what it's for, and if you set e.Cancel = true, focus won't leave the control. -- LuisR


                        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

                        1 Reply Last reply
                        0
                        • C Christian Graus

                          nidhelp wrote: hmmmm.. i need to ensure that the user only types s/S or f/F for the first letter.. how is that possible? A regex can contain more specific info, [] is used for character groups. [sSfF]\d{7}[sSfF] will do it. OnKeyPress will give you a function that runs when a key is pressed. In that function, you need to get the text from the textbox and check it. Something like this: TextBox tb = sender as TextBox; string input = tb.Text; if (input.Length == 9 && System.Text.RegularExpressions.Regex.Match(input, @"[sSfF]\d{7}[sSfF]").Success) { // The string is correct } else { // Don't forget that unless the length is 9, they could still be entering, so don't put a warning box here. A better place for that would be the lose focus event. } Christian Graus - Microsoft MVP - C++

                          N Offline
                          N Offline
                          nidhelp
                          wrote on last edited by
                          #12

                          in >> TextBox tb = sender as TextBox what is sender? i have to replace sender with some other things right? can i write the conditions which u typed under private bool. do i need to type something in OnKeyPress property? thanks i've learnt quite a lot from u =)

                          C 1 Reply Last reply
                          0
                          • N nidhelp

                            in >> TextBox tb = sender as TextBox what is sender? i have to replace sender with some other things right? can i write the conditions which u typed under private bool. do i need to type something in OnKeyPress property? thanks i've learnt quite a lot from u =)

                            C Offline
                            C Offline
                            Christian Graus
                            wrote on last edited by
                            #13

                            nidhelp wrote: what is sender? i have to replace sender with some other things right? No, the event has an object called sender. Did you not create the event yet ? I did it this way instead of using a variable directly precisely so that you didn't need to change the code I gave you. nidhelp wrote: can i write the conditions which u typed under private bool. You can set up a property if you'd like. Then you need to change the variable name though. nidhelp wrote: do i need to type something in OnKeyPress property? OnKeyPress lets you check every time the user types. If they are pressing a button to do something and you need THEN to check if the code is valid, forget the events ( although the Validating event may be worth a look, I know nothing about that ), and then it would make sense to make it a private property, to encapsulate this piece of functionality. nidhelp wrote: thanks i've learnt quite a lot from u =) No worries, always glad to help. Christian Graus - Microsoft MVP - C++

                            N 1 Reply Last reply
                            0
                            • C Christian Graus

                              nidhelp wrote: what is sender? i have to replace sender with some other things right? No, the event has an object called sender. Did you not create the event yet ? I did it this way instead of using a variable directly precisely so that you didn't need to change the code I gave you. nidhelp wrote: can i write the conditions which u typed under private bool. You can set up a property if you'd like. Then you need to change the variable name though. nidhelp wrote: do i need to type something in OnKeyPress property? OnKeyPress lets you check every time the user types. If they are pressing a button to do something and you need THEN to check if the code is valid, forget the events ( although the Validating event may be worth a look, I know nothing about that ), and then it would make sense to make it a private property, to encapsulate this piece of functionality. nidhelp wrote: thanks i've learnt quite a lot from u =) No worries, always glad to help. Christian Graus - Microsoft MVP - C++

                              N Offline
                              N Offline
                              nidhelp
                              wrote on last edited by
                              #14

                              sorry but i dont get u... do i create KeyPressEvent n put those codes inside it?

                              C 1 Reply Last reply
                              0
                              • N nidhelp

                                sorry but i dont get u... do i create KeyPressEvent n put those codes inside it?

                                C Offline
                                C Offline
                                Christian Graus
                                wrote on last edited by
                                #15

                                If you want to validate whenever a key is pressed, yes. If you always need a valid code, then I'd go with the code that accepts nothing else ( unless it's some sort of licence key ). If you don't always need it, then I'd not bother with the event, because people can type what they like, you can proceed with and without a valid value ( although it would be better for the UI to tell people the value was not valid ). Then you can instead do this check when the button is pressed that causes the value to be used. I'm about to leave for 2 days, so that's the end of my help for now. Good luck... Christian Graus - Microsoft MVP - C++

                                1 Reply Last reply
                                0
                                • N nidhelp

                                  hi im not supposed to disable any keys from being entered into the textbox.. the user is free to type n my app shd only open up a form when he types in a correct format.. correct format is a letter, 7 numbers, a letter.. i also have to make sure the first letter is S or s or F or f.. as long as the first letter is not correct, the form will not open..

                                  J Offline
                                  J Offline
                                  J4amieC
                                  wrote on last edited by
                                  #16

                                  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 set Cancel to true this can be done in the following two lines TextBox tb = sender as TextBox; 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.

                                  N 2 Replies Last reply
                                  0
                                  • J J4amieC

                                    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 set Cancel to true this can be done in the following two lines TextBox tb = sender as TextBox; 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.

                                    N Offline
                                    N Offline
                                    nidhelp
                                    wrote on last edited by
                                    #17

                                    i just finished reading n doing the top half of what u typed only but i want to thank u already.. u r v detailed n i cld follow through n understand your instructions easily.. hey thanks! =) i'll be reading n doing the bottom half after i type this.. hopefully it works.. =)

                                    J 1 Reply Last reply
                                    0
                                    • N nidhelp

                                      i just finished reading n doing the top half of what u typed only but i want to thank u already.. u r v detailed n i cld follow through n understand your instructions easily.. hey thanks! =) i'll be reading n doing the bottom half after i type this.. hopefully it works.. =)

                                      J Offline
                                      J Offline
                                      J4amieC
                                      wrote on last edited by
                                      #18

                                      nidhelp wrote: u r v detailed n i cld follow If only I could understand your "english" anywhere near as well as you could understand my explaination we'd both be happy.

                                      N 1 Reply Last reply
                                      0
                                      • J J4amieC

                                        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 set Cancel to true this can be done in the following two lines TextBox tb = sender as TextBox; 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.

                                        N Offline
                                        N Offline
                                        nidhelp
                                        wrote on last edited by
                                        #19

                                        I need to clarify this... My code WAS like this: private void button3_Click(object sender, System.EventArgs e) { if(Validate(textBox4.Text)) { if (tabControl1.TabPages.Contains(tabPage6)) { } else { tabControl1.TabPages.Add(tabPage6); } } else { // show invalid username dialogbox } } // Conditions to determine whether a tab page is to be displayed when Go button is clicked private bool Validate(string Val) { if(Val!="") { if(Val.Length==9||Val.Length==12) { return true; } else return false; } else return false; } I donno how to edit my codes with the codes u gave me to make my app run.. can u teach me what's going on? In fact, I also need to open a tab page if the text input is made up of 12 digits.. Therefore, a tab page should open when the text input is \d{12} OR [sSfF]\d{7}[a-zA-Z].. The user is allowed to key in anything he wishes but the tab page will not open if what he entered doesnt meet the conditions set.. Thank you!

                                        J 1 Reply Last reply
                                        0
                                        • J J4amieC

                                          nidhelp wrote: u r v detailed n i cld follow If only I could understand your "english" anywhere near as well as you could understand my explaination we'd both be happy.

                                          N Offline
                                          N Offline
                                          nidhelp
                                          wrote on last edited by
                                          #20

                                          u r v detailed n i cld follow >> you are very detailed and i could follow =) do u understand my "english" or do i have to type properly? sorry if you didnt.

                                          M 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups