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. TextBox validating e.cancel=true does not cancel button event

TextBox validating e.cancel=true does not cancel button event

Scheduled Pinned Locked Moved C#
csharpquestion
5 Posts 3 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.
  • F Offline
    F Offline
    fracalifa
    wrote on last edited by
    #1

    Hi all, In my .NET 1.1 Windows application I am using the validating event to check the correct field contents of a TextBox. When the contents was wrong I used e.cancel=true; and set the Focus on the TextBox again. I am leaving the TextBox by a button click event. I would expect that the validating event comes before a button click event occurs, but that's wrong. The button click will not be canceled. What must be done to cancel the button click when the e.cancel event of the TextBox becomes true ? Or can I check in my button click function if a cancel has happend anywhere ? Thanks in advance Frank

    T N 2 Replies Last reply
    0
    • F fracalifa

      Hi all, In my .NET 1.1 Windows application I am using the validating event to check the correct field contents of a TextBox. When the contents was wrong I used e.cancel=true; and set the Focus on the TextBox again. I am leaving the TextBox by a button click event. I would expect that the validating event comes before a button click event occurs, but that's wrong. The button click will not be canceled. What must be done to cancel the button click when the e.cancel event of the TextBox becomes true ? Or can I check in my button click function if a cancel has happend anywhere ? Thanks in advance Frank

      T Offline
      T Offline
      telha
      wrote on last edited by
      #2

      The event order is Validating, Validated and lost focus. So before you click on the button, the text box events are fired. You must use the validating event of the text box and use a flag on behalf of that to execute the button click.

      Muhammad Talha

      1 Reply Last reply
      0
      • F fracalifa

        Hi all, In my .NET 1.1 Windows application I am using the validating event to check the correct field contents of a TextBox. When the contents was wrong I used e.cancel=true; and set the Focus on the TextBox again. I am leaving the TextBox by a button click event. I would expect that the validating event comes before a button click event occurs, but that's wrong. The button click will not be canceled. What must be done to cancel the button click when the e.cancel event of the TextBox becomes true ? Or can I check in my button click function if a cancel has happend anywhere ? Thanks in advance Frank

        N Offline
        N Offline
        Nagy Vilmos
        wrote on last edited by
        #3

        Just tried this and it works okay, add a textbox and button to a form: private void textBox1_Validating(object sender, CancelEventArgs e) { if (string.Compare(textBox1.Text, "foo") != 0) { e.Cancel = true; } } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("bar!"); } If text box hasn't got foo then there is no bar. Doesn't matter if you tab or click away, the focus stays with the unvalidated control. Hope this helps,

        F 1 Reply Last reply
        0
        • N Nagy Vilmos

          Just tried this and it works okay, add a textbox and button to a form: private void textBox1_Validating(object sender, CancelEventArgs e) { if (string.Compare(textBox1.Text, "foo") != 0) { e.Cancel = true; } } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("bar!"); } If text box hasn't got foo then there is no bar. Doesn't matter if you tab or click away, the focus stays with the unvalidated control. Hope this helps,

          F Offline
          F Offline
          fracalifa
          wrote on last edited by
          #4

          Thank you for your answer, this is what I would appreciate to have. But in my Application the MessageBox was shown !!! This is what i don't understand. the Button is on a Tab of the tabcontrol and the TextBox is on a panel of the same tab, don't know if this metters ? An idea ? Thanks frank

          N 1 Reply Last reply
          0
          • F fracalifa

            Thank you for your answer, this is what I would appreciate to have. But in my Application the MessageBox was shown !!! This is what i don't understand. the Button is on a Tab of the tabcontrol and the TextBox is on a panel of the same tab, don't know if this metters ? An idea ? Thanks frank

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #5

            I can only think that your event is not bound to the text control. If the validating event is cancelled then the focus will stay on the text control. In the textbox's properties check the events and at the bottom look at Validating it should be (in the example) textBbox1_Validating

            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