Results 1 to 19 of 19

Thread: Phone numer Method that only allows digits and "." and "-", works great.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Phone numer Method that only allows digits and "." and "-", works great.

    I'm working on a project that has a textbox where a user can type in their phone number, but I needed a way to allow only digits and two other items like "." and "-"

    This code does it perfectly. And it's tied into the keyPress event.
    Here's the code.

    Code:
        Private Sub CheckPhoneForLetters(sender As Object, e As KeyPressEventArgs) Handles txtPhoneNumber.KeyPress
            If Not (Asc(e.KeyChar) = 8) Then
                Dim AllowedChars As String = "12.-34567890"
                If Not AllowedChars.Contains(e.KeyChar) Then
                    e.KeyChar = ChrW(0)
                    e.Handled = True
                End If
            End If
        End Sub
    Hope you can find a use for this. It can be modified to fit almost any type of input such as a First Name textbox. If you only want characters, then you would change the "AllowedChars to to the alphabet.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    Moderator Action: Moved to the Visual Basic .NET code bank which is where useful code snippets or practical code examples go.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    Thanks dday. I'll remember to put these things there next time

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    Paste the string "I am not a phone number" into the text box.

    This is why I mentioned I hate trying to restrict key presses.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    I like your "Paste" idea. That would certainly be an alternative method to try. And yes, I know you don't like validating key presses. But I'm sure there are others that might be interested. Validating UI input is a very new thing for me. I've had interesting conversations with people about it.

    Some say: Let them put whatever they want in there, and when they realize that they have shot themselves in the foot, they will change it to the correct information. That leans more to your side.

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    What I'm saying is this isn't good UI validation, as a very common use case for the user lets them input bad data.

    If the developer also adopts the stance, "My UI validation is there, I don't need to validate in code", they'll get a very big surprise when someone uses a basic feature of Windows to "circumvent" the UI validation.

    There's at least two other ways to get a non-phone-number into the box, but those involve scheming programmer types and I'm less concerned about them. If you want to cover the paste use case, you have no real good choices because there's not an event raised in that case. You can handle TextChanged, but that's an after-the-fact event so you can't do much but clear the text box in response, which stinks.

    I've never taken a crack at it, but I think to get something actually reliable, you've got to either use the MaskedTextBox (which stinks a different stink) or latch on to some API-level messages. I'm not sure why they didn't provide an OnPaste() to let you validate incoming text. TextBox is not built for that kind of validation, I guess.

    But I figure if my way is "Implement model-level validation and tell the user when they mess up", I'm arguing against, "Implement UI-level validation that catches some cases and tells the user when they messed up, and implement model-level validation that tells the user when they mess up." Which looks like it has an extra layer doing nothing.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    What I personally have taken the stance on is to let the users input their data and then I validate it on the backend, just like how HTML5 validates form data. For example, in your case for HTML it would look something like this:
    Code:
    <form>
      <label for="txtPhone">Phone Number</label>
      <input id="txtPhone" pattern="\d{3}(\.|\-)\d{3}(\.|\-)\d{4}" type="tel" />
      <input id="btnSubmit" type="submit" value="Submit" />
    </form>
    When the user would click on the submit button it would either inform the user that the value entered does not match the pattern or it would continue. In Visual Basic .NET I would do the same thing where I would store the RegEx pattern in the Tag property of the TextBox and then in the submit or next button it would validate the RegEx:
    Code:
    Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        'Get all input's who have a pattern and check if their value matches the pattern
        Dim validatedControls = (From c As Control In Me.Controls Where c.Tag IsNot Nothing)
        If validatedControls.Count(Function(c) System.Text.RegularExpressions.Regex.IsMatch(c.Text, c.Tag.ToString())) <> validatedControls.Count Then
            MessageBox.Show("Please enter a correct value for each required control.", "Invalid Input", MessageBoxButtons.OK)
        Else
            'Continue
        End If
    End Sub
    Optionally I will change the BackColor of the invalid inputs too.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    But I figure if my way is "Implement model-level validation and tell the user when they mess up", I'm arguing against, "Implement UI-level validation that catches some cases and tells the user when they messed up, and implement model-level validation that tells the user when they mess up." Which looks like it has an extra layer doing nothing.
    I apologize if I'm not understanding you correctly but.... I would like to think of this as if it were a real life situation, what if this were some software you had been tasked to write for work. And this was one of the requirements of that software, validating user input in a textbox where there are some things you don't want the user to enter.

    How would you handle this situation if it were your task (other that try and talk the boss out of it) <smile>.

  9. #9
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    Quote Originally Posted by dday9 View Post
    What I personally have taken the stance on is to let the users input their data and then I validate it on the backend, just like how HTML5 validates form data. For example, in your case for HTML it would look something like this:
    Code:
    <form>
      <label for="txtPhone">Phone Number</label>
      <input id="txtPhone" pattern="\d{3}(\.|\-)\d{3}(\.|\-)\d{4}" type="tel" />
      <input id="btnSubmit" type="submit" value="Submit" />
    </form>
    What happens here if I use my browser's developer tools to remove the 'pattern' attribute? 😇

    Quote Originally Posted by jumper77 View Post
    I apologize if I'm not understanding you correctly but.... I would like to think of this as if it were a real life situation, what if this were some software you had been tasked to write for work. And this was one of the requirements of that software, validating user input in a textbox where there are some things you don't want the user to enter.

    How would you handle this situation if it were your task (other that try and talk the boss out of it) <smile>.
    I feel like this is a straw man. Of course I would do my best to write this feature if I had a user requesting it. But philosophically, it's more trouble then it's worth. If they insist, I'd handle it like I already handle every feature request:

    First, I might implement an alternative that works the way several other users of the forum and I have agreed we prefer, and let the user test-drive that. Maybe they pitch a fit and say they HAVE to have the text box that only lets the right formats go in. That's the point where they have to make a hard decision.

    That's when we'd have a meeting to draw up the requirements so I can write a Requirements Change Request. Part of this is estimating the engineering, QA, build/installer, and testing effort involved. I could easily expect this new feature to require 2-3 days of engineering effort, a day of 2-3 QA testers' attention, and another day's impact on overall test efforts and build/installer work. So I'd draw up the new budget and schedule: we release 1 week later, and it costs 1 more week of my team's rate. Plus the two days that have already been sunk on the requirements change.

    Then the customer can decide: do they want the validation I've already implemented and the budget/schedule we already agreed upon, or do they want to commission me to write a fancy validating text box before starting on what they paid me for? Effort costs money, and I don't enter contracts without the explicit agreement that requirements changes represent renegotiation of the project schedule and budget.

    I've been in jobs where they don't do this. They were chronically behind schedule and over-budget. I sometimes had an 8AM meeting that told me to throw away yesterday's code for new requirements, then a 3PM meeting that told me to throw away the morning's code for new requirements. They paid about twice what I'm making now. But taking that offer felt like digging a grave. One of the developers didn't see his daughter for a week. A week.

    Don't think it can take that much time? I'm not convinced. This is a project I've tried before, and if internationalization's involved I might even be on the conservative side. The time to add this kind of polish is when you're finished ahead of schedule. When you're behind, and the customer remembers they need some European phone number that uses a weird unicode character as a separator, you tend to curse every minute you spent on fancy textbox character rejection.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    I'm sorry you feel like this is a "straw man" situation. I didn't mean it that way at all. I look up to you and take your ideas into serious consideration. And for anyone who think this is... well, you know. It's not true. I was trying to learn something...... And you have more knowledge than I and I respect you opinions and advice.

    So don't think I was trying to throw you under the bus <smile>.
    I had no thoughts like that whatsoever.

  11. #11
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    I tend to say, validate everything as early as possible but that doesn't replace the need to do a final "model" validation. The model validation is my "true" validation. Checking the key press is more to do with providing a better UI experience by catching mistakes as early as possible - it's about guiding the user rather than truly "validating".

    It falls under the same category as using drop downs rather than text boxes or putting in the effort to get the .Net drop downs to support proper auto-complete as opposed to the bloody weird "keep hammering the first letter repeatedly until you eventually get to the element you want" paradigm Microsoft offers out of the box (what is up with that MS? Seriously?).

    I don't wait for the customer to request this because 1. it's trivial to provide and 2. they probably won't request it but it will negatively colour their opinion of the product at a subliminal level. By providing it as part of the default offering I help them get that warm fuzzy feeling that's one of the key differentials between the users (nb, not management, who get to make the purchasing decision) accepting or rejecting the product. It may not make the difference between getting this order or not, but it sure makes a difference to getting the next one.

    The one criticism I'd offer is that I'd actually have the validation occur in a function rather than the event and that function would probably be sitting in some model class. Then I'd be using exactly the same functions under the key press as I would for the model validation. It's this that makes it so trivial to provide both and keeps them consistent.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    Hi dex, good morning. I'm glad you joined the conversation, because it's good to hear what people think about how to handle validation.

    I tend to say, validate everything as early as possible but that doesn't replace the need to do a final "model" validation. The model validation is my "true" validation. Checking the key press is more to do with providing a better UI experience by catching mistakes as early as possible - it's about guiding the user rather than truly "validating".
    I tried to look up "model" validation but the only references I could find were ASP.net. I did see one thing though. It was about validating in the properties section. Is that what you're talking about?

    It falls under the same category as using drop downs rather than text boxes or putting in the effort to get the .Net drop downs to support proper auto-complete as opposed to the bloody weird "keep hammering the first letter repeatedly until you eventually get to the element you want" paradigm Microsoft offers out of the box (what is up with that MS? Seriously?).
    I have to laugh about this one. Microsoft seems to have some things in the IDE that I wish it wish it wouldn't do. Although I can't remember what causes it to happen, there will be a popup window when I'm trying to type. So you have to close that window before I can keep typing. Which in my case, it means hitting the escape key to close the little window, then start typing again. So yea, what's up with that too?

    The one criticism I'd offer is that I'd actually have the validation occur in a function rather than the event and that function would probably be sitting in some model class. Then I'd be using exactly the same functions under the key press as I would for the model validation. It's this that makes it so trivial to provide both and keeps them consistent.
    It's good to hear how other people feel about validation. It's something that I need to learn. I'm guilty of validating as early as possible. It's because I don't have any opinions of the many different ways there are to handle validation. It's something I need read about, plus getting other people's input about how they handle validation.

    So thanks for the response. If you have any links that might help, feel free to post them.

  13. #13
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    Is that what you're talking about?
    I don't think it's an "official" phrase, I just used it because Sitten was using it and it seemed to sum up the concept quite well.

    Basically, it's saying validate your model (ie the states of the properties in your classes) before you process it. I.e. while you may allow the user to enter an invalid value for a phone number, you would validate (and refuse) it before saving it to a database.

    I'm 100% with Sitten that you should include this step of validation - it's crucial. Where we differ is that I think you should prevent the user from even entering it whereas he's happy to catch it "after the event". There's no doubt that catching it early is extra work so the question comes down to whether that work is worth doing. I think we'd probably both say "it depends".
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    I don't think it's an "official" phrase, I just used it because Sitten was using it and it seemed to sum up the concept quite well.
    Ok <smile> I guess I was taking that one too seriously. It made me search on the phrase and made me feel like I missed something along the way.

    Basically, it's saying validate your model (ie the states of the properties in your classes) before you process it. I.e. while you may allow the user to enter an invalid value for a phone number, you would validate (and refuse) it before saving it to a database.
    I know that Sitten's idea on this is to handle it in the properties section. I say that because I've seen some of his code. And I have to admit, that I'm starting to agree with him about validating in the keypress event is not the way to go about things.

    From what you say, you believe we should do both. And I can't remember if it was public or not, but Sitten pointed out a number of ways that the user could cause harm in the keypress event. I was all for checking input at the point where it was happening, but the code becomes more difficult to maintain because it will be in constant flux.

    And we're just talking about textboxes. What about the many other controls you would have to write realtime validation for. ListViews and DataGrids are enough to drive most people crazy (in my book).

    I'm 100% with Sitten that you should include this step of validation - it's crucial. Where we differ is that I think you should prevent the user from even entering it whereas he's happy to catch it "after the event". There's no doubt that catching it early is extra work so the question comes down to whether that work is worth doing. I think we'd probably both say "it depends".
    I would have to agree on the part about "it depends". From a time to market standpoint, such an elevated process of validation for all the developers would take a toll on delivery time. I've worked in different kinds of shops. Those who want you to write code as fast as you can, and others who give you all the time you need to do it right. I started out with the "fast as you can" type.

  15. #15
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    Topical and timely. The custom TextBox that appeared in there is now safe against 1 of the 3 non-keyboard ways to change Text.

    "Model" is a fundamental concept in modern UI development. It's very old, MS was pushing it in MFC and COM as far back as the mid-90s and possibly even further. It's been part of Mac OS GUI from the start. But the VB community tries very hard to ignore it, and I'm functioning on so little sleep I don't think I can tackle that topic without being downright insulting. The most polite way to put it is, "Sometimes I feel like the VB community is very proud to have earned the 'class clown' award, and works hard to defend that status."

    In some thread a while back, I described an application as having three parts:

    • The actual code that does work, like calculating square roots or simulating a system. It's just a bunch of functions, and in VB .NET that morphs to "a bunch of classes". It does work.
    • The UI code. Maybe it's console. Maybe Windows Forms. Maybe WPF. This is the part that displays things to the user in some way.
    • The "glue". This is code that takes user input from the UI, converts it to what the "work" code wants, coordinates the "work" code's functionality, then converts the results of the "work" code into something the UI code can interpret and display.


    To shorten a tutorial that could go on for pages: "Model" code is part of the code that "does the work". In particular, "model" code works with what we call the "domain objects". In a contact list, that's classes like Contact. They're the types that don't have anything to do with the UI, and if you were to throw away your UI and build another you'd still keep your model types.

    This is part of a series of patterns from the late 80s that propose the idea of separating our UI and model code with layers, so we can change one without impacting the other. The grand-daddy of these patterns is called "Presentation Model", and the implementations are something I'd love to talk about, if I believed the VB community wanted to hear about them at all.

    Validation's a tough topic because it can go against a lot of Presentation Model concepts: model validation is a concern of the model, but if we want our UI to validate it we're making it a shared concern. That's why there's a host of validation patterns that try to abstract it as best as they can so both View and Model have a concept of validation but the implementation can be shared without losing the benefits of separating UI and presentation. Windows Forms has a fairly interesting validation framework. WPF has a couple of options. But there's plenty of roll-your-own solutions, as well.

    And I'm fine with "it depends". That's the not-straw-man form of "What if the customer asks for it?" To be clear, I like it better because instead of saying, "I don't have to think about this because my customer wants it," it says, "There are downsides with this approach, but I have consciously decided those downsides are paid for by the benefits." "The customer asked for it" only argues, "My groceries are paid for by the customer", and that's not a good way to make engineering choices. Ask Morton-Thiokol how it worked out in terms of approving shuttle launches.

    Choice of framework matters too. In WinForms, making a text box that only ever contains a finite set of characters is so hard I consider it a design flaw. In WPF, built-in features make it tedious but feasible. On iOS and several other platforms, it's a built-in behavior. So it's only in Windows Forms that I "Well, actually...", because it's mostly in Windows Forms where I believe I could implement more valuable features in less time.

    The reason I put WPF/etc. on a pedestal here is everyone else has a concept Windows Forms lacks: a form of "The textbox's value is about to be changed to this string, will you allow this?" Windows Forms only provides this in the form of, "A user just pressed this key, should I let them?" That doesn't cover the clipboard, programmatic changes, assistive technologies, or automation, like everyone else. It's not a good idea to have validation that only breaks if a blind user's inputting text.
    Last edited by Sitten Spynne; Apr 26th, 2016 at 11:03 AM.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  16. #16
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    And I'm fine with "it depends".
    I like that paragraph, it highlights that our difference on this are really in emphasis and priority rather than fundamental.

    I want to clarify a couple of things. I don't consider handling the key press event "validation", I consider it part of the user experience. Specifically it's the part that's concerned with what to do when a user enters a single character into the text box. It's not catering to "special" key combinations etc and I wouldn't claim otherwise. If you want that stuff covered you have to code for it. That may be in the key press event, it may be in the mouse events, the lost focus event - there's all sorts of places you may or may not choose to spend effort to make things just a little bit slicker (and it usually is a "little" bit at best).

    The logic behind that event is validation and belongs in the model - the question really is just when to apply that logic. That's really important and is why I said it should be broken out to a function and sit in some model class - the rules that govern a phone number should be in the phone number, not the UI. If I make these rules granular enough I can call them for small changes (in this case a single letter) instead of waiting for a big change (a whole customer or just a whole phone number). Note that this doesn't mean I can ignore the need to validate the big events though - never assume your little steps add up to a one big stride, that way lies pulled hamstrings.

    There certainly are downsides to this, though. One is that the validation rules have to be more granular to support it - that's extra work. Another is that the increased interaction between the UI and the model tends to bring closer coupling (sure, I can break that coupling with interfaces, etc. but that add complexity to the interfaces - it's not free). And it's also easy to fall into the trap of thinking "I've already validated the keystrokes so everything must be OK" - that's a big mistake.

    I'm pretty confident in saying that both Sitten and I would consider validation to be a responsibility of the model. Where we differ is that I'm willing to force a little more complexity onto the model in order to support a slightly friendlier user experience. I'm willing to bet Sitten would allow the same level of complexity in if the customer demanded it and I would certainly leave that complexity out if the customer demanded a faster turnaround than I could otherwise manage. Our difference on this isn't really where we'd end up but rather where we'd start negotiating from.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    Hi dex, well, you just blew me out of the water with this post. I was busy writing away when I saw your post come in. And it stated so much of what I was going to post that is just threw it away <sigh>.

    The logic behind that event is validation and belongs in the model - the question really is just when to apply that logic. That's really important and is why I said it should be broken out to a function and sit in some model class - the rules that govern a phone number should be in the phone number, not the UI.
    I have to agree about the logic being part of a model class. That's exactly what it should be. I say that now because I did not do that. Being new to OOP, I didn't think about it at the time, but this conversation will make things stick in my head better. And the next time this situation comes along, I believe I will think about what's been said here.

    Something I said in the document I now wish I hadn't thrown away was, If the code guys got together with the UI people <not that they don't code>, they could come up with a model where both sides agrees on a validation strategy. It would be more work in the beginning, but worth it in the long run. The end result of the collaboration would be documentation laying out the guidelines for validation. And as I said before, even if some of the guidelines are fuzzy. (as in fuzzy logic <smile>)


    There certainly are downsides to this, though. One is that the validation rules have to be more granular to support it - that's extra work. Another is that the increased interaction between the UI and the model tends to bring closer coupling (sure, I can break that coupling with interfaces, etc. but that add complexity to the interfaces - it's not free).
    oops... think I just talked about this one from what you said before this.


    And it's also easy to fall into the trap of thinking "I've already validated the keystrokes so everything must be OK" - that's a big mistake.
    IMHO, When it comes to testing/validation, never take anything for granted. Some companies are better at it than others depending on how important they think it is. And having a robust method for validation usually comes to companies that have been in business for years. And in fact, I've "never" worked at a company who put validation high on the priority list.

  18. #18
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    I'm arguing that the KeyPress event handler is part of a larger requirement: "I want my textbox to only allow some set of characters as input."

    I'm being picky because it's hard to guarantee that with Windows Forms. It's easier if you decide "I don't care about handling that" for some cases. I think most customers, upon finding they can't type "asdf" in a number field but can paste it, will immediately report it as a bug. And someone who's automating the form and manages to send some bad input through won't be very happy either. It's up to you to decide if those users exist in your universe.

    I can't make that decision for you, though I think the paste use case is important. I do want to make sure it's understood that there are cases to consider. I also think it's important to know this really obvious hole in WinForms' validation capabilities is a non-issue in 10-year-old WPF, in case you're looking for a reason to start looking at that framework.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Phone numer Method that only allows digits and "." and "-", works great.

    I'm being picky because it's hard to guarantee that with Windows Forms. It's easier if you decide "I don't care about handling that" for some cases. I think most customers, upon finding they can't type "asdf" in a number field but can paste it, will immediately report it as a bug. And someone who's automating the form and manages to send some bad input through won't be very happy either. It's up to you to decide if those users exist in your universe.
    I have to laugh about "asdf". Have you been standing over my shoulders? It's the one thing that requires the least amount of thinking that you can type with one hand while keeping the mouse in the other hand

    validation capabilities is a non-issue in 10-year-old WPF, in case you're looking for a reason to start looking at that framework.
    Since I don't know anything about WinForms anyway. I might as well go for WPF. I mean I know the basic events but that's about it. I still don't how about "RaiseEVent" and "WithEvents". So yea, I would like to go that way, but at my age, I wouldn't want to go it alone. There's no payoff.

    Oh, and in reference to copy and paste, this was the first thing thing I came across and there seems to be plenty of them out there. Just wanted you to see for grins.

    Code:
    	Imports System.Windows.Forms
    	Public Class MyTextBoxClass
    
    	    Inherits TextBox
    	    Private Const WM_COPY As Integer = &H301
    	    Private Const WM_PASTE As Integer = &H302
     
    	    Public Event TextBeingCopied As EventHandler 'Happens before copy event
    	    Public Event TextWasCopied As EventHandler 'Happens after the copy event
    	    Public Event TextBeingPasted As EventHandler 'Happens before paste event
    	    Public Event TextWasPasted As EventHandler 'Happens after the paste event
    	 
    	    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    
    	        If m.Msg = WM_COPY Then
    	            RaiseEvent TextBeingCopied(Me, New EventArgs)
    	        ElseIf m.Msg = WM_PASTE Then
    	            RaiseEvent TextBeingPasted(Me, New EventArgs)
    	        End If
    	 
    	        MyBase.WndProc(m) 'Process the message
    	 
    	        If m.Msg = WM_COPY Then
    	            RaiseEvent TextWasCopied(Me, New EventArgs)
    	        ElseIf m.Msg = WM_PASTE Then
    	            RaiseEvent TextWasPasted(Me, New EventArgs)
    	        End If
    
    	    End Sub
    	End Class
    Code:
    Public Class Form1
     
    	    Private Sub MyTextBoxClass1_TextBeingCopied(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextBoxClass1.TextBeingCopied
    	        'This happens before the text is copied to the clipboard from the textbox
    	        MessageBox.Show("Text is being copied from the textbox")
    	    End Sub
    
    	    Private Sub MyTextBoxClass1_TextBeingPasted(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextBoxClass1.TextBeingPasted
    
    	        'This happens before the clipboard text is pasted in the textbox
    	        MessageBox.Show("Text is being pasted into the textbox")
    	    End Sub
    
    
    	    Private Sub MyTextBoxClass1_TextWasCopied(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextBoxClass1.TextWasCopied
    
    	        'This happend after the text is copied to the clipboard from the textbox
    	        MessageBox.Show("Text was copied from the textbox")
    	    End Sub
    	 
    	    Private Sub MyTextBoxClass1_TextWasPasted(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextBoxClass1.TextWasPasted
    
    	        'This happens after the clipboard text is pasted into the textbox
    	        MessageBox.Show("Text was pasted into the textbox")
    	    End Sub
    
    	End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width