Sign in to follow this  

[.net] Creating an event in a derived class

This topic is 3571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I use Visual C++ and .Net 2.0. I need to create a derived class. The purpose is I want to create new events inside that class. How to create the event inside that class? Can you please give me examples? Thank you very much.

Share this post


Link to post
Share on other sites
Quote:
Original post by chrisliando
I use Visual C++ and .Net 2.0.

Why?!? Switch to C# and there will be a bunch of us that would be more than glad to help. [wink] [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by chrisliando
I cannot do that because it has been a rule that everyone must use Visual C++.

Thank you for your advice.


Translation; "It is homework and in the class we use Visual C++."

Look at this to get you started;
Events in Native C++

theTroll

Share this post


Link to post
Share on other sites
I think we should have a new rule on these forums that if we help out someone with their homework they have to post their results so we can see how well we did ;)

Share this post


Link to post
Share on other sites
I've tried this code below but it is error like this:

Results.cpp
c:\chris\sample test\diffengtest\mydiff\Results.h(262) : error C3254: 'MyDiff::Results' : class contains explicit override 'OnVScroll' but does not derive from an interface that contains the function declaration
c:\chris\sample test\diffengtest\mydiff\Results.h(262) : error C2838: 'OnVScroll' : illegal qualified name in member declaration
MyDiff.cpp
c:\chris\sample test\diffengtest\mydiff\Results.h(262) : error C3254: 'MyDiff::Results' : class contains explicit override 'OnVScroll' but does not derive from an interface that contains the function declaration
c:\chris\sample test\diffengtest\mydiff\Results.h(262) : error C2838: 'OnVScroll' : illegal qualified name in member declaration



What happen?

Thank you.



Codes:

ref class MyListView: ListView {
public:
MyListView()
{
WM_HSCROLL = 0x0114;
WM_VSCROLL = 0x0115;
WM_MOUSEWHEEL = 0x020A;
}
System::Void OnVScroll(Message %m);

private:
int WM_HSCROLL;
int WM_VSCROLL;
int WM_MOUSEWHEEL;

protected:
virtual void WndProc(Message %m) override
{
if( m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL )
MyListView::OnVScroll(m);
else
ListView::WndProc(m);
}
}; //end of class MyListView

private: void MyListView::OnVScroll(Message %m) {
SendMessage(lvSource->Handle, m.Msg, m.WParam, m.LParam);
} //end of OnVScroll

Share this post


Link to post
Share on other sites
I've got a problem here in invoking event in a class that was derived from a ListView. I added a new event called OnVScroll to check if the user slide the listView scrollbar.

The purpose is if user slide listView1 scrollbar then listView2 also synchronized.

Here is the code:
static int const SBS_VERT = 1;
static int const SB_THUMBPOSITION = 4;

[DllImport("user32.dll", CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)]
static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

[DllImport("user32.dll")]
static int GetScrollPos(IntPtr hWnd, int nBar);

[DllImport("user32.dll")]
static int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);


ref class MyListView: ListView {
public:
MyListView()
{
WM_HSCROLL = 0x0114;
WM_VSCROLL = 0x0115;
WM_MOUSEWHEEL = 0x020A;
}
Void MyListView::OnVScroll(System::Windows::Forms::Message %m) {

} //end of OnVScroll

private:
int WM_HSCROLL;
int WM_VSCROLL;
int WM_MOUSEWHEEL;

protected:
virtual void WndProc(System::Windows::Forms::Message %m) override {
if( m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL )
MyListView::OnVScroll(m);
else
ListView::WndProc(m);
}

}; //end of class MyListView*/

On the event OnVScroll, I want to call :

SendMessage(lvSource->Handle, m.Msg, m.WParam, m.LParam);


I've placed this code OUTSIDE MyListView class, along with other built-in events like Button1_Click, etc:

private: System::Void MyListView::OnVScroll( System::Windows::Forms::Message %m ) {
SendMessage( lvSource->Handle, m.Msg, m.WParam, m.LParam );
} //end of OnVScroll

But it generated error like this:

Results.cpp
c:\chris\sample test\diffengtest\mydiff\Results.h(487) : error C3254: 'MyDiff::Results' : class contains explicit override 'OnVScroll' but does not derive from an interface that contains the function declaration
c:\chris\sample test\diffengtest\mydiff\Results.h(487) : error C2838: 'OnVScroll' : illegal qualified name in member declaration
MyDiff.cpp
c:\chris\sample test\diffengtest\mydiff\Results.h(487) : error C3254: 'MyDiff::Results' : class contains explicit override 'OnVScroll' but does not derive from an interface that contains the function declaration
c:\chris\sample test\diffengtest\mydiff\Results.h(487) : error C2838: 'OnVScroll' : illegal qualified name in member declaration
AssemblyInfo.cpp

What is wrong and how to fix this?

Thank you very much.

Share this post


Link to post
Share on other sites

This topic is 3571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this