CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Mar 2007
    Posts
    274

    [RESOLVED] How to raise event trapping mouse wheel scroll?

    I cant seem to figure out how to raise an event that will pass the delta value for mouse scroll from a user control to main form.

    I see that MouseEventArgs has delta as one of its properties. With that in mind I have defined

    publiceventMouseEventHandler OnMouseActivity;

    in my user control, and raise the event

    Code:
     
    privatevoid KeystrokesDetails_MouseMove(object sender, MouseEventArgs e)
    {
    OnMouseActivity(this, e);
    }
    as well.......


    In my main program, immediately after I create an instance of the user control(the user control is added to a split container panel2 controls collection), I set up the event like this ..........

    newDetails.OnMouseActivity += newMouseEventHandler(MouseMoved);

    and have a method programmed to handle the event....

    Code:
     
    privatevoid MouseMoved(object sender, MouseEventArgs e)
    {
    if (e.Delta == 120)
    {
    splitContainer2.Panel2.VerticalScroll.Value += splitContainer2.Panel2.VerticalScroll.SmallChange;
    }
    else if (e.Delta == -120)
    {
    splitContainer2.Panel2.VerticalScroll.Value += splitContainer2.Panel2.VerticalScroll.SmallChange;
    }
    }
    This doesnt work. Delta is always 0. How can I isolate the Delta value?
    Last edited by Traps; April 24th, 2007 at 11:18 PM.

  2. #2
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    Re: How to raise event trapping mouse wheel scroll?

    override OnMouseWheel method in your user control to catch the delta value, this delta value will not be filled in MouseMove event.

    Code:
        public partial class UserControl1 : UserControl
        {
            public event MouseEventHandler MouseWheel;
            public UserControl1()
            {
                InitializeComponent();
            }
    
            protected override void OnMouseWheel(MouseEventArgs e)
            {
                if (MouseWheel != null)
                    MouseWheel(this, e);//this e will contain delta value
            }
        }

  3. #3
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    Thank you I'll give this a shot.


    If you want, I have another question. Its a stupid one, and a brief response is all I'm looking for.

    What does protected and override do?

  4. #4
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    Re: How to raise event trapping mouse wheel scroll?

    protected means that the method is accessible by the current class and all class that inherit from it.

    while override means that you want to provide a new implementation to a predefined method that is mark as virtual in the base class (phew that was a mouthfull).

    Code:
    public class1{
         protected void SomeMethod(){...}
    
         protected virtual void SomeOverridableMethod(){...}
    }
    
    public class2 : class1 //<-- class2 is inherited from class1
    {
         private void Class2OwnMethod(){
            //call class1 method
             SomeMethod();
         }
    
    
        protected override void SomeOverridableMethod(){
             //I want to change the behaviour of this method
             //do some cool stuff here
        }
    }
    btw, you're proggressing nicely since the last time, I'll bet in the next couple of months you'll be answering people questions

  5. #5
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    LOL, thanks. My program is coming along great. Just adding some bells and whistles before I get back into the grind of more serious code.

    BTW....Not sure what I did wrong. I created the new event, and did the overriding of OnMouseWheel, in my user control, however when I try to scroll OnMouseWheel in user control is never touched.

  6. #6
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    My code in user control


    publiceventMouseEventHandler MouseWheel;



    Code:
     
    protectedoverridevoid OnMouseWheel(MouseEventArgs e)
    
    {
    
    if (MouseWheel != null)
    
    MouseWheel(this, e);//this e will contain delta value
    
    }
    
    

  7. #7
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    Re: How to raise event trapping mouse wheel scroll?

    The user control needs to have focus before the mousewheel message is processed. Try clicking in the usercontrol to set the focus to it, and set the break point on if (Mousewheel != null) line

  8. #8
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    My user control is nothing more then some labels and a text box. I tried clicking on the control itself(rather then the labels or textbox, and no event fired.

    Just so you know. My user control has no scrollbars. I create multiple instances of this control in my main program and add them to a panel which does have a scroll bar. Its the scroll bar of the panel i'm trying to control.
    Last edited by Traps; April 25th, 2007 at 12:04 AM.

  9. #9
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    Re: How to raise event trapping mouse wheel scroll?

    I assume that in you placed these labels and text in your user control right? If that's the case then the usercontrol will never gain focus since it will always give focus to it's child control. That's why the mouse wheel event never fired.

    I don't know any easy solution to this one though

  10. #10
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    Thats a bummer. Looks like i'd have to use a mouse hook. Because i'm lazy I probably wont do that.

    Side note though. I did pass a mouse move event from the user control........ So that when my mouse moved over the control itself, not the child controls, I could manipulate the scroll bar.

    What about this. Is it possible to pass wheel scroll from the text box on the user control up the chain of command to my main program?
    Last edited by Traps; April 25th, 2007 at 12:16 AM.

  11. #11
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    also, looking at the list of available events in the development environment (you know, in the properties window) I dont see any OnMouseWheel event.

    Why would the MouseMove event have a delta value in the arguments? If you cant access delta why is it there?


    http://msdn2.microsoft.com/en-us/lib...s_members.aspx
    Last edited by Traps; April 25th, 2007 at 12:22 AM.

  12. #12
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    Re: How to raise event trapping mouse wheel scroll?

    I've just remember one thing, you can use ScrollableControl class to support mousewheel, and it already has Mousewheel event built in

    Code:
         public partial class UserControl1 : ScrollableControl
        {
            //public event MouseEventHandler MouseWheel;
            public UserControl1()
            {
                InitializeComponent();
                this.TabStop = true;
                
            }
    
            protected override void OnMouseWheel(MouseEventArgs e)
            {
                Debug.WriteLine(e.Delta);
                base.OnMouseWheel(e);
            }
        }

  13. #13
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    Oh crap, I just did something bad.!!!!!!!

    I get this error when trying to go to the user control design



    One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.
    The service System.ComponentModel.Design.IInheritanceService already exists in the service container. Parameter name: serviceType
    Hide

    at System.ComponentModel.Design.ServiceContainer.AddService(Type serviceType, Object serviceInstance, Boolean promote)
    at System.ComponentModel.Design.ServiceContainer.AddService(Type serviceType, Object serviceInstance)
    at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IServiceContainer.AddService(Type serviceType, Object serviceInstance)
    at System.Windows.Forms.Design.ComponentDocumentDesigner.Initialize(IComponent component)
    at System.ComponentModel.Design.DesignerHost.AddToContainerPostProcess(IComponent component, String name, IContainer containerToAddTo)
    at System.ComponentModel.Design.DesignerHost.Add(IComponent component, String name)
    at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType, String name)
    at System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer)
    at System.ComponentModel.Design.Serialization.DesignerSerializationManager.System.ComponentModel.Design.Serialization.IDesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer)
    at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
    at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)

  14. #14
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    lmao!!!!!!!!! WHEW I think I fixed it.

  15. #15
    Join Date
    Mar 2007
    Posts
    274

    Re: How to raise event trapping mouse wheel scroll?

    This looks hard


    You do not typically use the ScrollableControl class directly. The ContainerControl and Panel classes inherit from this class.

    The ScrollableControl class acts as a base class for controls that require the ability to scroll. To enable a control to display scroll bars as needed, set the AutoScroll property to true and set the AutoScrollMinSize property to the desired size. When the control is sized smaller than the specified minimum size, or a child control is located outside the bounds of the control, the appropriate scroll bars are displayed.
    So instead of using the panel on my main form to scroll, I would drop a panel on the scrollable control, add my labels and text box, and then in code, position them, and have method for adding instances. I think thats how I understand it. Or do I even need a panel for this control? I dont know. I've made alot of progress tonight, in so that I'm happy with the visual layout of my program now. I'll work on the scrolling tomorrow.

    Thank you again for your help.

Page 1 of 2 12 LastLast

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

Featured