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. Having A Spot Of Trouble With Events

Having A Spot Of Trouble With Events

Scheduled Pinned Locked Moved C#
mobilehostinghelp
4 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.
  • R Offline
    R Offline
    Roger Wright
    wrote on last edited by
    #1

    As everyone here is tired of hearing, I'm working on a simple User Control that's misbehaving. If you help me solve this, I promise to contribute an article about it. The control displays various user-selectable shapes for water channels, then asks for the dimensions of the conduit. I have an event handler working nicely to notify the hosting Form when a value changes, but it's too responsive; too many events fire it. Specifically, when a user enters values into the textboxes for the dimensions of the channel, if for some ungodly reason the user decides to select another shape, the event fires for a change of value in the previous (unchanged) textbox. I added some code tonight to check to see that the value actually changed before the focus moves to another control, but it doesn't seem to be working, and I don't see why. The relevant code is as follows:

    private void rbTrap_CheckedChanged(object sender, EventArgs e)
    {
    if (rbTrap.Checked == true)
    {
    Shape = myShape.Trap;
    MyArgs.MyControl = "rbTrap";
    RaiseEvent(rbTrap, MyArgs);
    lblDim1.Text = "Depth, d";
    lblDim2.Text = "Bottom Width, W1";
    lblDim3.Text = "Top Width, W2";
    lblDim3.Visible = true;
    txtDim3.Visible = true;
    Invalidate();
    }
    }

        private double ValidateEntry(TextBox MyTextBox)
        {
            try
            {
                return Double.Parse(MyTextBox.Text);
    
            }
            catch (FormatException ex)
            {
                MessageBox.Show("Enter a valid numeric value" + ex.Message);
                MyTextBox.Focus();
                return 0.0;
            }
    
    
        }
    
        //txtDim1
        private void txtDim1\_Enter(object sender, EventArgs e)
        {
            txtDim1.SelectAll();
        }
    
        private void txtDim1\_Leave(object sender, EventArgs e)
        {
            prev = dim1;                    //Save the current value
            dim1 = ValidateEntry(txtDim1);  //Get the new value
            if (dim1 != 0.0 & prev != dim1) //Raise an event if Validation failed AND value changed
            {
                MyArgs.MyControl = "txtDim1";
                RaiseEvent(txtDim1, MyArgs);
            }
        }
    
        private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
    
    T D 2 Replies Last reply
    0
    • R Roger Wright

      As everyone here is tired of hearing, I'm working on a simple User Control that's misbehaving. If you help me solve this, I promise to contribute an article about it. The control displays various user-selectable shapes for water channels, then asks for the dimensions of the conduit. I have an event handler working nicely to notify the hosting Form when a value changes, but it's too responsive; too many events fire it. Specifically, when a user enters values into the textboxes for the dimensions of the channel, if for some ungodly reason the user decides to select another shape, the event fires for a change of value in the previous (unchanged) textbox. I added some code tonight to check to see that the value actually changed before the focus moves to another control, but it doesn't seem to be working, and I don't see why. The relevant code is as follows:

      private void rbTrap_CheckedChanged(object sender, EventArgs e)
      {
      if (rbTrap.Checked == true)
      {
      Shape = myShape.Trap;
      MyArgs.MyControl = "rbTrap";
      RaiseEvent(rbTrap, MyArgs);
      lblDim1.Text = "Depth, d";
      lblDim2.Text = "Bottom Width, W1";
      lblDim3.Text = "Top Width, W2";
      lblDim3.Visible = true;
      txtDim3.Visible = true;
      Invalidate();
      }
      }

          private double ValidateEntry(TextBox MyTextBox)
          {
              try
              {
                  return Double.Parse(MyTextBox.Text);
      
              }
              catch (FormatException ex)
              {
                  MessageBox.Show("Enter a valid numeric value" + ex.Message);
                  MyTextBox.Focus();
                  return 0.0;
              }
      
      
          }
      
          //txtDim1
          private void txtDim1\_Enter(object sender, EventArgs e)
          {
              txtDim1.SelectAll();
          }
      
          private void txtDim1\_Leave(object sender, EventArgs e)
          {
              prev = dim1;                    //Save the current value
              dim1 = ValidateEntry(txtDim1);  //Get the new value
              if (dim1 != 0.0 & prev != dim1) //Raise an event if Validation failed AND value changed
              {
                  MyArgs.MyControl = "txtDim1";
                  RaiseEvent(txtDim1, MyArgs);
              }
          }
      
          private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e)
          {
              if (e.KeyChar == 13)
              {
      
      T Offline
      T Offline
      TheFoZ
      wrote on last edited by
      #2

      Hi Roger. I'm having trouble recreating your problem. I shoved two text boxes on a form and used the following code:-

          double prev;
          double dim1;
      
          private void txtDim1\_Leave(object sender, EventArgs e)
          {
              prev = dim1;
              dim1 = ValidateEntry(txtDim1);
      
              if (dim1 != 0.0 & prev != dim1)
              {
                  MessageBox.Show("Event Fired");
              }
      
          }
      
          private double ValidateEntry(TextBox MyTextBox)
          {
              try
              { 
                  return Double.Parse(MyTextBox.Text); 
              }
              catch (FormatException ex) 
              { 
                  MessageBox.Show("Enter a valid numeric value" + ex.Message); 
                  MyTextBox.Focus(); return 0.0; 
              }
          }
      
          private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e)
          {
              if (e.KeyChar == 13) { txtDim2.Focus(); }
          }
      

      and it worked as expected. Is there anywhere else in your code that you could be changing the value of txtDim1.

      The FoZ

      R 1 Reply Last reply
      0
      • R Roger Wright

        As everyone here is tired of hearing, I'm working on a simple User Control that's misbehaving. If you help me solve this, I promise to contribute an article about it. The control displays various user-selectable shapes for water channels, then asks for the dimensions of the conduit. I have an event handler working nicely to notify the hosting Form when a value changes, but it's too responsive; too many events fire it. Specifically, when a user enters values into the textboxes for the dimensions of the channel, if for some ungodly reason the user decides to select another shape, the event fires for a change of value in the previous (unchanged) textbox. I added some code tonight to check to see that the value actually changed before the focus moves to another control, but it doesn't seem to be working, and I don't see why. The relevant code is as follows:

        private void rbTrap_CheckedChanged(object sender, EventArgs e)
        {
        if (rbTrap.Checked == true)
        {
        Shape = myShape.Trap;
        MyArgs.MyControl = "rbTrap";
        RaiseEvent(rbTrap, MyArgs);
        lblDim1.Text = "Depth, d";
        lblDim2.Text = "Bottom Width, W1";
        lblDim3.Text = "Top Width, W2";
        lblDim3.Visible = true;
        txtDim3.Visible = true;
        Invalidate();
        }
        }

            private double ValidateEntry(TextBox MyTextBox)
            {
                try
                {
                    return Double.Parse(MyTextBox.Text);
        
                }
                catch (FormatException ex)
                {
                    MessageBox.Show("Enter a valid numeric value" + ex.Message);
                    MyTextBox.Focus();
                    return 0.0;
                }
        
        
            }
        
            //txtDim1
            private void txtDim1\_Enter(object sender, EventArgs e)
            {
                txtDim1.SelectAll();
            }
        
            private void txtDim1\_Leave(object sender, EventArgs e)
            {
                prev = dim1;                    //Save the current value
                dim1 = ValidateEntry(txtDim1);  //Get the new value
                if (dim1 != 0.0 & prev != dim1) //Raise an event if Validation failed AND value changed
                {
                    MyArgs.MyControl = "txtDim1";
                    RaiseEvent(txtDim1, MyArgs);
                }
            }
        
            private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == 13)
                {
        
        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Hi Roger, Thanks for the kind words :rose: My only initial thought is to set a breakpoint inside if (rbTrap.Checked == true). I have had wierd behaviour before with CheckBoxes/RadioButtons where CheckedChanged has raised more often than expected. If that doesn't help isolate the cause then feel free to drop me an email through the link at the bottom of this post and I'll happily take a look at the project for you and root the b**ger out!

        Dave

        If this helped, please vote & accept answer!

        Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        1 Reply Last reply
        0
        • T TheFoZ

          Hi Roger. I'm having trouble recreating your problem. I shoved two text boxes on a form and used the following code:-

              double prev;
              double dim1;
          
              private void txtDim1\_Leave(object sender, EventArgs e)
              {
                  prev = dim1;
                  dim1 = ValidateEntry(txtDim1);
          
                  if (dim1 != 0.0 & prev != dim1)
                  {
                      MessageBox.Show("Event Fired");
                  }
          
              }
          
              private double ValidateEntry(TextBox MyTextBox)
              {
                  try
                  { 
                      return Double.Parse(MyTextBox.Text); 
                  }
                  catch (FormatException ex) 
                  { 
                      MessageBox.Show("Enter a valid numeric value" + ex.Message); 
                      MyTextBox.Focus(); return 0.0; 
                  }
              }
          
              private void txtDim1\_KeyPress(object sender, KeyPressEventArgs e)
              {
                  if (e.KeyChar == 13) { txtDim2.Focus(); }
              }
          

          and it worked as expected. Is there anywhere else in your code that you could be changing the value of txtDim1.

          The FoZ

          R Offline
          R Offline
          Roger Wright
          wrote on last edited by
          #4

          I think Dave may be on to something; it has to do with the radio button somehow. The extra event does not occur if I use the mouse to move to another text box, only when I move to a radio button. The messages displayed also show that the text did not change, but it does happen that the text changes from Selected to unselected when the focus is moved to the radio button. I wonder if that's being picked up as a text change and firing the event?

          "A Journey of a Thousand Rest Stops Begins with a Single Movement"

          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