|
The correct way is to define a delegate that extends EventHandler , or use an existing one (depending on whether or not a particular delegate declares a param with an EventArgs -derivative that suits your needs).
Raising Events[^] in the .NET Framework SDK explains the recommended way of firing events, which includes declaring a delegate and EventArgs derivative (like I said, if necessary), declaring the event on your class, and making a protected virtual member named OnEventName that actually fires the event (so that derivative classes can either override it to handle events without a delegate (faster, with more control) or raise the event themselves).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You've been faster again! This is the second time today that I write an answer to a new question, and when it is posted your answer is already there.
BTW: It's not necessary to derive from EventArgs . FxCop wants me to do so, but where is the real benefit?
|
|
|
|
|
It's just common for events. You're right, though, it's not necessary. Commonality, though, fosters better programming for those (few these days, it seems) that recognize the pattern.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok, I've got it... thanks
I start to understand the way it works.
it's even more simple than MFC... :P
There is no spoon.
|
|
|
|
|
Hi Heath,
I was looking that way... Delegates. it seems they look like callbacks.
I can declare my delegate, but I don't understand yet how to use it in my class.
I have done the following:
<br />
class PeerPlayer<br />
{<br />
...<br />
public delegate void HostFound(HostInfo node);<br />
<br />
public void FindHostResponseHandler(object sender, FindHostResponseEventArgs args)<br />
{<br />
HostInfo node=new HostInfo();<br />
<br />
node.GuidInstance=args.Message.ApplicationDescription.GuidInstance;<br />
node.HostAddress=(Address) args.Message.AddressSender.Clone();<br />
node.SessionName=args.Message.ApplicationDescription.SessionName;<br />
<br />
if (!m_sessions.Contains(node))<br />
{<br />
m_sessions.Add(node);<br />
}<br />
}<br />
...<br />
}:((<br />
There is no spoon.
|
|
|
|
|
Did you read the article I linked? It's really quite clear.
A delegate is a managed function pointer. By declaring an event on your class, you in effect declare a MulticastDelegate that maintains a list of delegates. When you invoke your event delegate, any handlers are called in any given order.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok, I can fire my event now
but I don't understand how I can use it outside of the class...
PeerPlayer pp=new PeerPlayer();
pp.HostFound+=new??? // new what???
I have implemented a HostFound handler in the form class.
There is no spoon.
|
|
|
|
|
bouli wrote:
// new what???
Use whatever delegate declared by the event. So if you declared public event EventHandler HostFound , then use new EventHandler(myhandler) .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok, I've got it
the delegate was inside the PeerPlayer class.
I have done the following now:
pp.HostFound+=new HostFoundHandler(OnHostFound);
and it works
I'm now working on how to write the calls of the DirectPlay connections in a thread.
I would like to execute the following code in a thread:
<br />
public bool Enumerate(String hostName)<br />
{<br />
m_sessions.RemoveRange(0, m_sessions.Count);<br />
<br />
Address hostAddress=new Address();<br />
<br />
hostAddress.ServiceProvider=Address.ServiceProviderTcpIp;<br />
<br />
if (hostName.Length > 0)<br />
hostAddress.AddComponent(Address.KeyHostname, hostName);<br />
<br />
hostAddress.AddComponent(Address.KeyPort, m_port);<br />
<br />
ApplicationDescription appDesc=new ApplicationDescription();<br />
<br />
appDesc.GuidApplication=m_appGuid;<br />
<br />
try<br />
{<br />
m_peer.FindHosts(appDesc, hostAddress, m_localAddress, null, 0, 0, 0, FindHostsFlags.Sync);<br />
}<br />
catch(Exception ex)<br />
{<br />
Debug.WriteLine(ex.Message);<br />
return false;<br />
}<br />
<br />
return true;<br />
}<br />
There is no spoon.
|
|
|
|
|
If the event needs parameters, you can define your own delegate:
<br />
public delegate void MyEventHandler (object sender, HostInfo details);<br />
public event MyEventHandler MyEvent;<br />
To fire your event, just call it as if it was a method:
<br />
if(MyEvent != null){<br />
MyEvent(this, Node);<br />
}<br />
|
|
|
|
|
Do you always need to declare an event with a delegate?
as I come from MFC, if I understand well, it seems that delegates are to C# what callbacks are to C/C++...
There is no spoon.
|
|
|
|
|
1) There are a couple pre-defined delegates (EventHandler, CancelEventHandler, and so on). If you need other parameters, you have to define a delegate.
2) Exactly. There are no "official" callback in C#, they have been replaced by delegates.
|
|
|
|
|
ok, I understood how it works... thanks
it's more simple than MFC...
There is no spoon.
|
|
|
|
|
oh, yes, one more thing,
how the caller of the event can use the fired event???
I have done this:
class PeerPlayer
{
public delegate void HostFoundHandler(HostInfo node);
public event HostFoundHandler HostFound;
...
}
...
PeerPlayer=pp=new PeerPlayer();
...
pp.HostFound+=????
There is no spoon.
|
|
|
|
|
I'm writing instalation program that creates web application. Before this could happen, IIS must be installed on the machine. If it is not, I would like to show warning message and gracefully end the installation process.
How can I check programatically if IIS is installed ?
Thank You
Michał
|
|
|
|
|
If you use the Windows Installer project in VS.NET, you can create a Web Project that does this already. If IIS is not installed, it will display an error and exit.
There are many ways of determining if IIS is installed, from checking for the inetmgr.exe in %WINDIR%\System32\inetsvr\ (use Environment.GetFolderPath(Environment.SpecialFolder.System) ) to get the %WINDIR%\System32 directory), or using System.DirectoryServices classes using the iis:// scheme (IIRC, if IIS isn't installed trying to use the IIS directory protocol should fail).
WMI (via System.Management ) should also give you information, although I don't remember what WMI classes you want to use off-hand.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
There are many ways of determining if IIS is installed, from checking for the inetmgr.exe in %WINDIR%\System32\inetsvr\ (use Environment.GetFolderPath(Environment.SpecialFolder.System)) to get the %WINDIR%\System32 directory),
Thanks. I'll give these a try.
Heath Stewart wrote:
or using System.DirectoryServices classes using the iis:// scheme (IIRC, if IIS isn't installed trying to use the IIS directory protocol should fail).
I tried this already, and on WinServer 2003 I was able to get webRoot object, event though IIS had not been installed.
private bool DetermineIfIISIsInstalled()<br />
{<br />
try<br />
{<br />
DirectoryEntry deRoot= new DirectoryEntry("IIS://LocalHost/W3SVC/1/Root");<br />
if(deRoot==null)<br />
return false;<br />
}<br />
catch(Exception ex)<br />
{<br />
return false;<br />
}<br />
return true;<br />
}
Thanks
Michał
|
|
|
|
|
Hi!
I have made an application with a panel showing 2d graphics. Now I have found out that the grapichs are inverted, the x axis is pointing in the right direction but y is downwards when it should be upwards on the screen. I can´t really find any good way to invert the axis.
I could just change code doing the drawing, but the panel is accessed from a lot of places and it would take a lot of time. What I would like to do is invert the axix at startup and that all the 2d graphics in the panel is drawn in the right way every time a graphics object is created via panel.CreateGraphics().
Help would be much appreciated!
Regards! / Mikke
|
|
|
|
|
GDI and GDI+ all use an axis with the origin in the upper-left corner. This has always been the case. DirectX and I assume OpenGL (never worked with it) use the lower-left corner unless you transform the point of origin.
The painting code will just have to be changed. You should encapsulate this in a derivative class of the Panel you should make, rather than doing the drawing outside the Panel that is responsible for showing the graphics. Encapsulation is a major feature of OOP.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok, thank you! I thought it might be like that, but wanted to be sure.
I have used a OOD doing the class hierachy and have encapsulated all the important features. Altough some of the functionallity has been placed outside the class because some of components in the application are drawing some graphics om the panel, mainly debug information.
Looks like I will have to do some changes in the painting routines then..
/ Mikke
( by the way, I think that openGL uses upper right corner too. If I remember correctly, I have not used it in a while.. )
|
|
|
|
|
Hi,
I searched the web. I can find how to export data for Excel 2002 or later.
But how to do for Excel 2000 ?
Thanks
Wilson
|
|
|
|
|
Instead of trying to export to Excel, just export to a CSV (comma-separated values, or comma-delimited text file). Excel and other applications can open these with no problem. Just be sure to quote your strings that contain commas in them, which isn't hard and there are articles to assist you.
To note, though, the typelib for Excel hasn't changed much since Excel 97. The base functionality is the same - as all good COM libs should. Chances are whatever works for Excel 2002 will work for Excel 2000, but you should use the Excel 2000 typelib for Excel 2000 and later since the class IDs and interface IDs (GUID s) should be the same throughout new versions (new interfaces are added and the old classes implement those, but the old interfaces are typically still implemented). Please note that this is usually the case, but things do get deprecated, though the functionality you require is pretty basic so you should be fine.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi ,,,,
I would like know what is the equivalent of SetItemData and GetItemData of MFC, listCtrl in C# listview ?
Any help would be greatly apprecaited.
Thanks in advance
saleem
|
|
|
|
|
Setting:
listview.Items.Add("aaaa");
listview.Items[index].SubItems.Add("bbbb");
Geting:
listview.Items[index]
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
Hi...
Thanx for the response.
But I think my question is not very clear.
In MFC what happens is that when we are adding data in the list, we usually call 'InsertItem'. And to associate a unique number to the added row we call 'SetItemData', so that we can get the correct row number when a row is selected by calling 'GetItemData'.
I would like to know what is the equivalent of this in C# .NET ?
Thanks in advance
saleem
|
|
|
|