Scrolling event in Controls.

Ixeus
Ixeus used Ask the Experts™
on
I've been spending the best part of my offtime trying to find the seemingly non-existant onscroll event...  Is there another event I've not checked that perhaps contains this event? (esp. ListView)

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
what you can do is using the derived scrollbarclasses, VScrollbar or HScrollBar.
there is a scroll event.
this example is from msdn and shows, how to add a scrollbar to a form:
private void InitializeMyScrollBar()
 {
    // Create and initialize a VScrollBar.
    VScrollBar vScrollBar1 = new VScrollBar();
   
    // Dock the scroll bar to the right side of the form.
    vScrollBar1.Dock = DockStyle.Right;
   
    // Add the scroll bar to the form.
    Controls.Add(vScrollBar1);
 }

it will be similar to use it with a treeview.
remember to disable the scrollbars in your treeview.

Author

Commented:
Alright, that works, but only partially, it's not pretty.  I'll have to code in all the "mousewheel -> scroll up etc" events to it... all I really need right now, is just to know that the scrolling is happening.  so that I can "repaint" the custom controls/icons on it.

 The closest to what I was looking for lies within the WndProc.  But I can't for some reason track the Child controls of the form's events.

In my form class I got a function

protected override void WndProc(ref Message m)
        {
            // Listen for operating system messages.
//            m.HWnd is never the child...  how///
            base.WndProc(ref m);
        }
i somehow need to put this into the listView's class, not form

any ideas?



you will have to make a derived class of the treeview class.
then you can put the wnd proc in that derived class.
i have tested it and made a debug print of the message.
if i scroll, the m.msg will be WM_VSCROLL.
you derive a class of treeview like this example:


public class MyTreeView : System.Windows.Forms.TreeView
{
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ComponentModel.Container components = null;
   
   #region Construction
   /// <summary>
   /// Constructs MyTreeView.
   /// </summary>
   public MyTreeView()
   {
     // This call is required by the Windows.Forms Form Designer.
     InitializeComponent();            
   }

   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   protected override void Dispose( bool disposing )
  {
     if( disposing )
     {
        if(components != null)
       {
         components.Dispose();
       }
     }
      base.Dispose( disposing );
  }
        protected override void WndProc(ref Message m)
        {
            // Listen for operating system messages.
            //            m.HWnd is never the child...  how///
            base.WndProc(ref m);
            System.Diagnostics.Debug.WriteLine(m.ToString());
        }
.....
your code
.....
}

you have to compile that derive class as classlibrary so you will get your own treeview component.
if you compiled it you just have to add the dll of the compiled treeview as a reference of your form, and drag that dll to your toolset of components, so that you can use that new treeview.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

to make your own treeview control from the .net ide, you will only have to go to file->new->project
then select in section c#-project the windows control library(dunno the exact english name of the project cause i have the german version).
the wizard will make a base class for your own control.
then you will only have to change the class declaration from which you wanna derive your class:
the wizard will make :
public class UserControl1 : System.Windows.Forms.UserControl
and you will have to change it to
public class UserControl1 : System.Windows.Forms.TreeView

compile it and drag it on your form.

Author

Commented:
Ok you've scored 100pts till I can test this, phase 2, I'm sure I'll be able to create my own functions, based on events?  Such as onmousedown ()  and subsequently create a textbox/combo box in the appropiate places, and position it in the correct places (I've done this already, but not as an extended class)

I have to finish the project plan since that's due now, so I'll get back to this hopefully in a few hours time
Any additional information, would be nice, such as deploying etc.  (btw I'm sure I just need to replace TreeView with ListView)

Author

Commented:
wow I can't believe I wrote so incoherently, I apologize, I was listening to someone talk to me :p

I meant to say, I'd like to make this class, blackbox certain functions when user clicks on the Listview, to put a Combo/Text box in the subitem (I can do that already in code) position, but I would like to do this with 1 function e.g.

MyNewListView.ItemSelected(...)
{
    //I want to override this event and DO something here like putting the text box in the right place.
}
yes, you can derive your control class from any control you want.
why do you want to override that itemselect event?
you can connect every function you want to that event.
or you can fire your own event, if the itemselect event is fired.

Author

Commented:
Sweet.  That's what I hoped for, I come from C++ and VB background using API... so I'm very used to doing things the hard way when windows never provided me with enough functionality...

 Could I ask you to entertain me as to how to go about creating my very own control from scratch?
(using MSDE *usercontrol functionality)  e.g. a listview that would put a textbox whereever the mouse is, the Textbox.Text = scrollbar's value. That's all =)
If you're successful with a good talk through on creating this and implementing it on a form, I'll up this to 500 pts =)  ((I'm on a time-crunch my research time has expired :(((  ))

You seem like you know what you're doing in .NET, me... talk to me in C++ style if you know it or VB


-Ixeus
i come from visual c++ too :)
do you mean something like this?
http://www.csharphelp.com/archives2/archive341.html
i havent take a deeper look at that example but looking at the screenshots seems to me that there are some codesnipes which could be usefull for you.
but what i dont understand is why you need the scrollbars value?
if you wanna put a textbox right at your mousecursor, why dont you just get the mousepointer position?

Author

Commented:
The textbox at the cursor is NOT what I want to do, but since I've already got a formula to put the textbox where I want it, why ask you to do that?

 I usually don't like code snippets from other sites since they rarely answer the question I want, either too much or too little.  This is why I go to EE, to get custom answers.  (I don't ask for what I "ACTUALLY" need directly) Since I can do that myself from the information of the "only what I need to see code".  Like, I need to know at which point the scrolling is complete, so I can re-paint the combobox/textbox at the new item's coordinates.  ETC.  

-Ixeus

 
aah ok.
then you have to make it like i said, with your own control.
override the wndproc and check, if the message is wm_vscroll.
then fire ur own event.
you know how to make own events?

Author

Commented:
not in .NET :(

I wouldn't mind a little assistance there :)
k, gimme some time.
i try to make a little example of an own listview control with an onscroll event.

Author

Commented:
Excellent, take your time, I can only try this tomorrow morning anyway ( I'm close to GMT ) (GMT + 2)
Thanks for the help =)

Author

Commented:
I've upped it to 350 for your effort, I'll put it at 500, once I test the working solution =)
+2 hours? where r u from dude?
im from germany, so im +1 hour:)

Author

Commented:
South Africa :)
+2 hours? where r u from dude?
im from germany, so im +1 hour:)
nice!
think your weather is much greater than in germany:)

Author

Commented:
at least your trains are on time :)
k, i have made a little solution with 1 testcontrol(derived listview) and 1 testapp.
the testcontrol fires an onscroll event each time the scrollbar is moved.
my testapp just shows a messagebox if the event is fired.

code for the control:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace OwnControls
{      
    public class MyListView : System.Windows.Forms.ListView
    {            
        #region Own EventArgs
        /// <summary>
        /// Class that contains the data for
        /// own event args. derives from System.EventArgs.
        /// no args are needed in this example but you can pass every args you want.        
        /// </summary>
       
        public class MyEventArgs : EventArgs
        {  
            /// <summary>
            /// Constructor
            /// </summary>            
            public MyEventArgs()
            {
            }
        }
        #endregion

        /// <summary>
        /// Delegate the event handler.
        /// </summary>
        public delegate void MyEventHandler(object sender,MyEventArgs e);
       
        /// <summary>
        /// The Event
        /// </summary>
        public event MyEventHandler OnScroll;
       
        /// <summary>
        /// The protected OnVScroll method raises the event by invoking
        /// the delegates. The sender is always this, the current instance
        /// of the class.
        /// </summary>        
        protected virtual void OnVScroll(MyEventArgs e)
        {
            if(OnScroll != null)
            {                
                OnScroll(this, e);
            }
        }

        private System.ComponentModel.Container components = null;
            
        public MyListView()
        {      
      InitializeComponent();
        }

        protected override void Dispose( bool disposing )
        {
      if( disposing )
      {
             if( components != null )
          components.Dispose();
      }
      base.Dispose( disposing );
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if(m.Msg == 0x115)//fire event on wm_vscroll
            {
                MyEventArgs e = new MyEventArgs();
                this.OnVScroll(e);                
            }
        }

        #region Component Designer generated code
        private void InitializeComponent()
        {
             components = new System.ComponentModel.Container();
        }
        #endregion
    }
}

code for the testapp:

sing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using OwnControls;

namespace TestApp
{      
    public class frmMain : System.Windows.Forms.Form
    {
        private OwnControls.MyListView myListView1;
        private System.Windows.Forms.ColumnHeader columnHeader1;

        private System.ComponentModel.Container components = null;

        public frmMain()
        {                  
            InitializeComponent();
            this.myListView1.OnScroll += new OwnControls.MyListView.MyEventHandler(this.OnScroll);
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                 if (components != null)
      {
           components.Dispose();
      }
            }
            base.Dispose( disposing );
        }              
         #endregion

         [STAThread]
         static void Main()
         {
      Application.Run(new frmMain());
         }

        private void OnScroll(object sender, OwnControls.MyListView.MyEventArgs e)
        {
            MessageBox.Show("event fired");
        }        
    }
}

i deleted the code for InitializeComponent() function cause this is a designer made function of all objects.

the best way of testing it will be if you make a new solution, add 2 projects, one windows application and 1 customusercontrol.
change the code of the usercontrol with the posted code i made.
compile the control and add the reference of the dll to your test application.
then drag the dll to your toolbar so that you are able to use the costum control.

ixeus, have you tested it?

Author

Commented:
no I haven't had any time today at all :(((  I'm behind on schedule and found out I Had forgot to come back to the User stored procedures, so I've been writing them the whole day... I'll only be able to TEST this once I'm done with that :/

 BTW, I could accept your answer, on good faith that you'll still be there to assist me after I accept.

 I'm leaving for home in 30min, and I doubt I'll be finished by then :/  I apologize for that oversight.
no problem.
i just wanted to know how things going on.
hope you will get your store procedures finished in time.
greets
andre
hey ixeus, any results out there in south africa?

Author

Commented:
Geez dam sorry man, I've been SO busy with work, on a diff project I haven't had a chance to do anything (been coming to work at 5am till 5pm everyday for 2 weeks...)  I apologize for making you wait, I haven't got a chance to test it, but I'll take it on good faith, and my lack of coming back to you, I've upped it to 600.  Sorry again

-Ix

Author

Commented:
mmm it didn't let me up it to 600...

Author

Commented:
well the limit is 500, I see... I'll have to add another question (please read the C# questions for your other remaining points) or go here
<http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20776712.html>
you dont have to increase the points dude :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial