Event handling

This is a discussion on Event handling within the C# Programming forums, part of the General Programming Boards category; Hello everyone, I am going to try to explain this problem the best way I can without confusing anyone. In ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    184

    Event handling

    Hello everyone,

    I am going to try to explain this problem the best way I can without confusing anyone. In a windows app, I have a menu with a list of 10 menu items. They represent settings for a increment variable, 1 - 10. What I am trying to do, instead of having 10 different event handlers, I want 1 event handler to handle all 10 menu items. The problem comes in in trying to decipher which menu item called the event and setting a property of that specific menu item. I will include a code snippet that does NOT work, but it might help clear up what I am ideally trying to do.

    Code:
    		private void mi1sec_Click(object sender, System.EventArgs e)
    		{                                                 
    			uncheckIncrement();
    			sender.checked = true;
                                                    switch(sender)
    			{
    				case mi1sec:
    					setIncrement(1);
    					break;
    				case mi2sec:
    					setIncrement(2);
    					break;
    				case mi3sec:
    					setIncrement(3);
    					break;
    				case mi4sec:
    					setIncrement(4);
    					break;
    				case mi5sec:
    					setIncrement(5);
    					break;
    				case mi6sec:
    					setIncrement(6);
    					break;
    				case mi7sec:
    					setIncrement(7);
    					break;
    				case mi8sec:
    					setIncrement(8);
    					break;
    				case mi9sec:
    					setIncrement(9);
    					break;
    				case mi10sec:
    					setIncrement(10);
    					break;
    			}			
    			
    		}
    
    		private void uncheckIncrement()
    		{
    			mi1sec.Checked = false;
    			mi2sec.Checked = false;
    			mi3sec.Checked = false;
    			mi4sec.Checked = false;
    			mi5sec.Checked = false;
    			mi6sec.Checked = false;
    			mi7sec.Checked = false;
    			mi8sec.Checked = false;
    			mi9sec.Checked = false;
    			mi10sec.Checked = false;
    		}
    
    		private void setIncrement(int i)
    		{
    			FRIncrement = i;
    			barProgress.LargeChange = i;
    		}
    Any suggestions would be greatly appreciated.

    Thanks,
    Kendal

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Why don't you use a properties instead of that setIncrement function?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    What do you mean by using a properties????? The increment variable will be used by an audio player to specify how far to fast forward or rewind an audio file while in playback mode.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    One of the shortcommings of C# I needed in almost any program I wrote was that menuitems have no tag. So you have to do this yourself.

    PHP Code:
    public class TagMenuItem MenuItem
    {
        private 
    object m_Tag;
        
        public 
    object Tag
        
    {
            
    get { return m_Tag; }
            
    set m_Tag value; }
        }

    So now, you have a menuitem you can save things in.

    Create your menuitems and put the value in each tag ( 1-10 ).
    Give all of them the same eventhandler.

    PHP Code:
    private void miANYsec_Click(object senderSystem.EventArgs e)
    {                                                 
        
    TagMenuItem item = ( sender as TagMenuItem );

        
    uncheckIncrement(); 

        
    setIncrementitem.Tag as int ); // or as any other datatype you need

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    private void setIncrement(int i)
    {
    FRIncrement = i;
    barProgress.LargeChange = i;
    }

    This should be written as a property of your class:

    PHP Code:
    private int Increment
    {
        
    get { return FRIncrement; }
        
    set FRIncrement valuebarProgress.LargeChange value; }

    Usage: instead of "setIncrement( i );" you write "Increment = i;"
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 05:47 AM
  2. Event handling between forms
    By ejohns85 in forum C# Programming
    Replies: 1
    Last Post: 12-11-2006, 08:14 PM
  3. Standard Win Forms Event Handling? (& #develop)
    By Zeusbwr in forum C# Programming
    Replies: 1
    Last Post: 04-22-2005, 03:59 PM
  4. Using TObject class in Borland C++ Builder event handling
    By siegelp in forum Windows Programming
    Replies: 4
    Last Post: 07-19-2004, 01:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21