|
Hi Nick,
ok, so what would be the code for my example?
cheers
SM
|
|
|
|
|
stevemasters22 wrote:
ok, so what would be the code for my example?
I am not going to write the code for this example, that is for you to do. You need to look into Int32.Parse[^] and TextBox.Text[^]. The MSDN[^] is a great source for information, if you have a specific question please feel free to post it. Good luck.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Forgive me but I am very new to C# and VS.NET.
I have a number of submenu items that are created at run-time. When any of these items are clicked I want it to call the same function but with some sort of argument.
I have extended the System.EventArgs class to include a private member. Do I need to declare a delegate or can I use the System.EventHandler?
When I try declaring a delegate like this:
public delegate void OpenPortEventHandler(object sender, OpenPortEventArgs e);
And then add this eventhandler to a menu item like this:
this.OpenPort.MenuItems[index].Click += new OpenPortEventHandler(this.COM_Click);
I get this error: Cannot implicitly convert type 'Serial_Communicator.Terminal.OpenPortEventHandler' to 'System.EventHandler'
|
|
|
|
|
What you can do is subclass the MenuItem and add your event and delegate there.
class OpenPortMenuItem : MenuItem
{
public delegate void OpenPortEventHandler(object o, OpenPortEventArgs e);
public event OpenPortEventHandler PortClick;
public OpenPortMenuItem()
{
this.PortClick += new OpenPortEventHandler(OnPortClick);
}
protected override void OnClick(EventArgs e)
{
if(PortClick != null)
{
OpenPortEventArgs args = new OpenPortEventArgs();
args.Port = 80;
PortClick(this, args);
}
}
protected void OnPortClick(object sender, OpenPortEventArgs e)
{
base.OnClick(e);
}
}
class OpenPortEventArgs : EventArgs
{
private int _port;
public int Port
{
get{return _port;}
set{_port = value;}
}
public OpenPortEventArgs(){}
}
OpenPortMenuItem m = new OpenPortMenuItem();
OpenPortMenuItem m2 = new OpenPortMenuItem();
m.Click += new EventHandler(this.COM_Click);
m2.Click += new EventHandler(this.COM_Click);
this.mainMenu1.MenuItems.AddRange(new OpenPortMenuItem[] {m, m2});
private void COM_Click(object sender, OpenPortEventArgs e)
{
MenuItem m = sender as MenuItem;
if(m != null)
{
}
OpenPortEventArgs a = e as OpenPortEventArgs;
if(a != null)
{
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
Nick:
I can't thank you enough. Works like a charm. I'll have to examine a little closer to see exactly what you did.
Thanks again.
-Jae
|
|
|
|
|
Nick:
I was actually able to get it working with less code that you gave me there. Any idea why the code still works when I change the class to the following:
public class OpenPortMenuItem : System.Windows.Forms.MenuItem
{
//public delegate void OpenPortEventHandler(object o, OpenPortEventArgs e);
//public event OpenPortEventHandler PortClick;
private int index;
public OpenPortMenuItem(int index)
{
this.index = index;
//this.PortClick += new OpenPortEventHandler(OnPortClick);
}
protected override void OnClick(EventArgs e)
{
//if(PortClick != null)
//{
OpenPortEventArgs args = new OpenPortEventArgs(this.index);
OnPortClick(this, args);
//}
}
protected void OnPortClick(object sender, OpenPortEventArgs e)
{
base.OnClick(e);
}
}
And I use the menuitem like this:
this.OpenPort.MenuItems.Add(index,m);
this.OpenPort.MenuItems[index].Click += new System.EventHandler(this.COM_Click);
Let me know if that check for PortClick != null is there for some sort of error catching.
-Jae
|
|
|
|
|
Jae,
The check of if(PortClick != null) checks to see if the events' internal linked-list of handlers has any registered handlers or not, otherwise there is no reason to call anything if no one is listening. Others could use your OpenPortMenuItem class and attach their own event handlers to the PortClick event.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Nick:
Does that mean that the following:
protected override void OnClick(EventArgs e)
{
if(PortClick != null)
{
OpenPortEventArgs args = new OpenPortEventArgs(this.index);
OnPortClick(this, args); //Function call
}
}
Would be better written as this:
protected override void OnClick(EventArgs e)
{
if(PortClick != null)
{
OpenPortEventArgs args = new OpenPortEventArgs(this.index);
PortClick(this, args); //Raise the event
}
}
That way anyone else who has attached delegates to the PortClick event will be triggered when the menu item is clicked?
-Jae
my blog
|
|
|
|
|
Yes, sorry, the correct way would be to fire the event after you have checked if it was null. I will correct my initial posting.
- Nick Parker My Blog | My Articles
|
|
|
|
|
There are number of good articles deaing with how to raise custom events. You can refer to the following link (Events and Delegates Simplified)in CP.Hope this helps you
http://www.codeproject.com/csharp/events.asp
Regards,
Jay.
|
|
|
|
|
Hey
I have downloaded a project tat are using System.Management and i can compile an run it. But when i make a project meself I can't include System.Managemet.
It isn't in the drop down list (I use VS). And I get the following error : D:\Visual Studio Projects\ipset\WMIHelper.cs(3): The type or namespace name 'Management' does not exist in the class or namespace 'System' (are you missing an assembly reference?)
Does anybody know how to include System.Management ?!?
Thanks in advance
Jacob
|
|
|
|
|
You have to add a refrence to the System.Managment namespace. In the solution explorer, right click "Refrences" then click add refrence, then select the System.Managment one... and your in action.
If it's already in there with a yellow icon or something just delete it and add it again.
i think...
/\ |_ E X E GG
|
|
|
|
|
You need to include a reference to the System.Management.dll before you can include it within your project. Select Project --> Add Reference... and scroll down to the System.Management entry and hit the Select button. Now you should be able to include that namespace within your code.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi
I want my .DLLs to be in a subdir of the .exe, but so far have been unable to figure out how to configure things in order to make it work.
thanks
|
|
|
|
|
Why? Anyway, there really isn't any configuration you can do that will accomplish this, that is reliable. Keeping them in the same dir as the .EXE is the best practice.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Have you tried to add the sub directory to the system search path? I would have to assume you have a good reason to do that though. It is more reliable to keep them in the same directory.
|
|
|
|
|
|
|
Hi
I want to have a easy way to control my mdi documents. (like it's done on mirc, for example) but all i've been able to get is a list in the menu items.
|
|
|
|
|
Read[^] about the Form.MdiParent property in the .NET Framework SDK, which includes an example.
On a particular MenuItem (for example, "&Windows"), you can set MenuItem.MdiList to true in order to display all MDI Form s in the container application.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I have a simple text file on the network. My application will read and write to this file, can someone tell me how to lock this file while in exclusive mode so that another app or a user won't access this file while I'm reading/writing to the file? Also, can someone tell me how to check if this file is locked before I try to lock it?
Thanks
Sharon
|
|
|
|
|
Here is what I know
The IO namespace has a FileInfo class and some enumerations that control the Info, Access and Shareing of the file.
it looks kind of like this.
FileInfo fl = new FileInfo(@"C:\whatever.fil");
FileStream s = fl.Open(FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None);
This should get you an open file with RW and be exclusive to you.
|
|
|
|
|
Use the managed DirectX 9 SDK.
using Microsoft.DirectX.AudioVideoPlayback;
Audio theAudioFile = Audio.FromFile("name of the file.mp3");
theAudioFile.Play();
It works with .wav, .mp3, and .wma files. Also note that in the .NET framework 2.0 beta, there's a built-in .wav player control called SoundPlayer, found in System.Windows.Forms.SoundPlayer.
#include "witty_sig.h"
|
|
|
|
|
Ive tryed using AudioVideoPlayback before and it cant find the reference i have directx sdk 9 and have updated to the new 9.0b runtime, but is the managed direct x somthing extra? ive been looking and have seen it mentioned in my search but not understood its meaning
|
|
|
|
|
The Managed DirectX 9 SDK is seperate from the DirectX 9 SDK. See here[^]. If installed correctly, the MDX9 assemblies will be added to the GAC.
#include "witty_sig.h"
|
|
|
|