Click here to Skip to main content
15,894,460 members
Home / Discussions / C#
   

C#

 
GeneralRe: Object Null/Empty Check Pin
Luc Pattyn15-Jun-10 0:55
sitebuilderLuc Pattyn15-Jun-10 0:55 
GeneralRe: Object Null/Empty Check Pin
DaveyM6915-Jun-10 1:07
professionalDaveyM6915-Jun-10 1:07 
AnswerRe: Object Null/Empty Check Pin
Eddy Vluggen14-Jun-10 23:04
professionalEddy Vluggen14-Jun-10 23:04 
QuestionAuto Generate Row Number [solved] Pin
Elango N14-Jun-10 22:12
Elango N14-Jun-10 22:12 
AnswerRe: Auto Generate Row Number Pin
Peace ON14-Jun-10 22:29
Peace ON14-Jun-10 22:29 
Questionhow I could save apicture in sql and bring it to a form in C#? Pin
ronakT14-Jun-10 21:34
ronakT14-Jun-10 21:34 
AnswerRe: how I could save apicture in sql and bring it to a form in C#? Pin
Abhinav S14-Jun-10 22:12
Abhinav S14-Jun-10 22:12 
QuestionHaving A Spot Of Trouble With Events Pin
Roger Wright14-Jun-10 19:48
professionalRoger Wright14-Jun-10 19:48 
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)
           {
               txtDim2.Focus();
           }
       }


In my understanding, if the value of dim1 is unchanged (dim1 = prev), the if test should fail, and RaiseEvent(...) should not execute. Maybe I've been staring at this too many hours, but I can't see where the error lies. For what it's worth, the event handling code is:

//Events
       public event EventHandler ValueChanged;
       public class MyEventArgs : EventArgs
       {
           private string myControl;
           public string MyControl
           {
               get { return myControl; }
               set { myControl = value; }
           }
       }

       protected virtual void OnValueChanged(object sender, MyEventArgs e)
       {
           EventHandler eh = ValueChanged;
           if (eh != null)
               eh(this, e);
       }

       public void RaiseEvent(object sender, MyEventArgs e)
       {
           OnValueChanged(sender, e);
       }


I adapted this from an article posted by DaveyM69 - an excellent article, by the way - and I admit that my understanding of event handling is shaky at best. But it does work nicely, albeit in weird ways.

Any assistance would be welcome...
"A Journey of a Thousand Rest Stops Begins with a Single Movement"

AnswerRe: Having A Spot Of Trouble With Events Pin
TheFoZ14-Jun-10 21:28
TheFoZ14-Jun-10 21:28 
GeneralRe: Having A Spot Of Trouble With Events Pin
Roger Wright15-Jun-10 7:06
professionalRoger Wright15-Jun-10 7:06 
AnswerRe: Having A Spot Of Trouble With Events Pin
DaveyM6914-Jun-10 22:21
professionalDaveyM6914-Jun-10 22:21 
QuestionTrackBar Pin
Nicolás Marzoni14-Jun-10 17:32
Nicolás Marzoni14-Jun-10 17:32 
AnswerRe: TrackBar Pin
Calla14-Jun-10 19:32
Calla14-Jun-10 19:32 
GeneralRe: TrackBar Pin
Nicolás Marzoni15-Jun-10 1:28
Nicolás Marzoni15-Jun-10 1:28 
GeneralRe: TrackBar Pin
DaveyM6915-Jun-10 1:31
professionalDaveyM6915-Jun-10 1:31 
GeneralRe: TrackBar Pin
Nicolás Marzoni15-Jun-10 1:36
Nicolás Marzoni15-Jun-10 1:36 
GeneralRe: TrackBar Pin
DaveyM6915-Jun-10 2:39
professionalDaveyM6915-Jun-10 2:39 
GeneralRe: TrackBar Pin
DaveyM6915-Jun-10 2:40
professionalDaveyM6915-Jun-10 2:40 
GeneralRe: TrackBar - Long post alert! Pin
DaveyM6915-Jun-10 10:17
professionalDaveyM6915-Jun-10 10:17 
QuestionAsp.Net MVC2 in VS 2010 Pin
Darrall14-Jun-10 12:03
Darrall14-Jun-10 12:03 
AnswerRe: Asp.Net MVC2 in VS 2010 Pin
Al Beback14-Jun-10 17:15
Al Beback14-Jun-10 17:15 
GeneralRe: Asp.Net MVC2 in VS 2010 Pin
Darrall15-Jun-10 3:37
Darrall15-Jun-10 3:37 
Questioncreating an unsafe array in C# Pin
Keith Vitali14-Jun-10 10:45
Keith Vitali14-Jun-10 10:45 
AnswerRe: creating an unsafe array in C# Pin
Luc Pattyn14-Jun-10 10:56
sitebuilderLuc Pattyn14-Jun-10 10:56 
GeneralRe: creating an unsafe array in C# Pin
Keith Vitali15-Jun-10 3:40
Keith Vitali15-Jun-10 3:40 

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.