|
Intellisense also states:
"(except when used from within the type 'yournamespace.BaseForm')"
Compiler Error CS0070[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Actually, I didn't try compiling it because Intellisense insisted it was screwed up. I just want to why it doesn't treat it like a normal class member.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: I just want to why it doesn't treat it like a normal class member.
By design, I suppose.
"...events are a special type of delegate that can only be invoked
from within the class that declared them. Derived classes cannot directly
invoke events that are declared within the base class"
How to: Raise Base Class Events in Derived Classes (C# Programming Guide)[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
You can't raise an event (directly) from a derived class. (WTF?)
Write a method to wrap it.
protected void RaiseMyEventFunction
(
blah blah
)
{
if ( MyEventFunction != null )
{
MyEventFunction(this, new MyEventArgsArgs());
}
}
And call that instead, it's good practice anyway.
|
|
|
|
|
PIEBALDconsult wrote: You can't raise an event (directly) from a derived class. (WTF?)
I already know that (now), and I mentioned writing a function to do it in the base class. I just want to know why I can't raise an event in a derived class, and perhaps where the reason is explained.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Security, I guess, but I don't see it either.
|
|
|
|
|
I believe this is by design if you use the event keyword. I never use it, I just define a delegate and call it.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
You cannot call directly events of a base class from a child class
You must declare a protected (or other scope) method on the base class to handle that
If you dont want that quit the "event" keyword
public class MyEventArgs : EventArgs
{
public MyEventArgs()
{
}
}
public partial class BaseForm : Form
{
public delegate void MyEventHandler(object sender, MyEventArgs e);
public event MyEventHandler MyEventFunction;
public BaseForm()
{
InitializeComponent();
}
protected virtual void OnMyEventFunction(object sender, MyEventArgs e){
MyEventFunction(sender, e);
}
}
public partial class JohnsForm : BaseForm
{
private void button1_Click(object sender, EventArgs e)
{
OnMyEventFunction(this, new MyEventArgsArgs());
}
}
Saludos!!
____Juan
|
|
|
|
|
It's designed that way, but I'm not sure why! Maybe they figured that people wouldn't be careful about null checking etc if they could just call an event directly but if they called the OnEvent it should be handled there? Just guessing!
I suppose the normal way (as you said) is the protected virtual void OnMyEvent(MyEventArgs e) method in the base and call that directly or override in the derived class.
Instead, you can mark the event as virtual in the base and override that in the derived class if you want.
BTW, it doesn't matter of course, but I normally use this if using my own event args, saves having to declare those delegates myself!:
public event EventHandler<MyEventArgs> MyEvent;
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
DaveyM69 wrote: EventHandler<myeventargs>
Provided the event warrants an EventArgs; mine usually don't.
|
|
|
|
|
In that case
public event EventHandler MyEvent; takes care of it, and still no need to create a delegate ourselves, and just pass this, EventArgs.Empty when raising the event
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Not if I want to pass an int or something, my events tend to be very simple.
|
|
|
|
|
Cool - but doing it that way doesn't leave you any scope for expansion later on.
Some point down the road you may need to pass more than one piece of data. If that happens, you'll need to rewrite the delegate to take the new class/struct or additional parameters and every method signature invoked by it - that could be a whole lot of work! Using a custom event args avoids this so your apps won't break if requirements change.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I'll burn that bridge after I've crossed it.
|
|
|
|
|
PIEBALDconsult wrote: I'll burn that bridge after I've crossed it
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Hello,
This is my first post to a site ever, please be patient with me. If I am not following a normal rule of conduct also please tell me.
My question/problem is:
I am trying to control the input to a form from another program. For example ( and this is just an example not being specific to my problem)
-- for example if you wanted a program to click -- using the mouse -- on the calculator that comes with windows 543 / 234 =
-- or you wished to enter "543" into the text field portion of the calculator.
The specific project is actually geared to control a piece of stock trading software automatically. But there is no COM or NET library to interface with.
I was wondering if this is even possible. Can you move a mouse and click on a form and not actually change focus... and enter data into a text box field when you have no clue about its handle or name.
-----
I am really looking for a starting point and feasability issues. If someone could point me in the right direction.
|
|
|
|
|
Yes, so long as it's not a WPF program, it's possible. Every control is a window, you can get the handle to a window and then send it messages. It's a lot of work tho, and it can be flaky, esp if the program changes.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
One good starting point is getting WinSpy++ and inspecting the target application with it. Having done that, you can actually quite easy enumerate the windows/controls of any application (lookup EnumWindows() on MSDN) from there you get the handles of child controls until you find the control you want to interact with, from here its a piece of cake - use SendMessage() to have the control do whatever you want it to.
Sure its not THAT easy to keep track of the interaction and the related windows/controls but I guess it must be worth it.
|
|
|
|
|
|
How do i make to "snap" togheter 2 dialog boxes from same process. like in WinAMP
|
|
|
|
|
By working out when they are close, and moving them so they are right on top of each other. Then, when they are 'snapped', when one is moved, you move the other one in unison. There's a moved event you can use for this.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
I'm adding some code to a project that requires the use of the app.config file. However I don't have access to this file. Can anyone think of a good way to start the application and have it "generate" an app.config file, or just ignore the file?
I'm trying to use Enterprise Library Caching without app.config, or perhaps add the config section to the app.config file.
Any ideas? Solutions?
Thanks,
Jason
|
|
|
|
|
What do you mean 'requires the use' and 'don't have access' ?
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
So we have an application which has an app.config file. However the code I'm working on is a secondary agent that runs within that application with the app.config file. I would like to run the Enterprise Library Caching without having to modify this app.config file.
Does that make any more sense?
Is there a way to use the EntLib.Caching without the use of the app.config. Or perhaps is there a way in code to handle the calls to the app.config file?
|
|
|
|
|
You can add it to the solution. Right click -> Add Item -> Application configuration file.
Insert funny quote here.
|
|
|
|