C# - 从另一种形式调用函数
我需要来自另一种形式的托盘通知。 ControlPanel.cs(默认表单,此处为通知图标):
...
public partial class ControlPanel : Form
{
public string TrayP
{
get { return ""; }
set { TrayPopup(value, "test");}
}
public void TrayPopup(string message, string title)
{
TrayIcon.BalloonTipText = message;
TrayIcon.BalloonTipTitle = title;
TrayIcon.ShowBalloonTip(1);
}
Form1.cs(另一种表单):
...
public partial class Form1 : Form
{
public ControlPanel cp;
....
private void mouse_Up(object sender, MouseEventArgs e) {
cp.TrayP = "TRAY POPUP THIS";
}
On line cp.TrayP = "TRAY POPUP THIS";
我收到 NullException。 如果我将其更改为 cp.TrayPopup("TRAY POPUT THIS", "test"); 则会抛出异常。
如果我这样做:
private void mouse_Up(object sender, MouseEventArgs e) {
var CP = new ControlPanel();
CP.TrayPopup("TRAY POPUP THIS", "test");
}
,则会显示托盘弹出窗口,但它会创建第二个托盘图标,然后显示新图标的气球提示。我能做些什么? PS:抱歉英语不好。
I need a tray notify from another form.
ControlPanel.cs (default form, notifyicon here):
...
public partial class ControlPanel : Form
{
public string TrayP
{
get { return ""; }
set { TrayPopup(value, "test");}
}
public void TrayPopup(string message, string title)
{
TrayIcon.BalloonTipText = message;
TrayIcon.BalloonTipTitle = title;
TrayIcon.ShowBalloonTip(1);
}
Form1.cs (another form):
...
public partial class Form1 : Form
{
public ControlPanel cp;
....
private void mouse_Up(object sender, MouseEventArgs e) {
cp.TrayP = "TRAY POPUP THIS";
}
On line cp.TrayP = "TRAY POPUP THIS";
I am getting a NullException.
If i change it to cp.TrayPopup("TRAY POPUT THIS", "test");
an exception throws whatever.
If i make this:
private void mouse_Up(object sender, MouseEventArgs e) {
var CP = new ControlPanel();
CP.TrayPopup("TRAY POPUP THIS", "test");
}
, tray popup shows, but it`s creates the second tray icon and then show balloon hint from new icon. What can I do?
P.S.: Sorry for bad English.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您从 ControlPanel 打开第二个表单“Form1”,则应该将 CP 的实例传递给 Form1,例如
If you are opening second form "Form1" from ControlPanel, you should pass the instance of CP to Form1, like
不需要每次都分配内存,试试这个
don't need to allocate memory each time , try this
您的
public ControlPanel cp;
变量具有 null 引用,因为它从未初始化。为了访问控制面板,您需要设置对其的有效引用。如果您的 ControlPanel.cs 在另一种窗体上,您需要从那里获取该引用。通过公共属性或接口。Your
public ControlPanel cp;
variable has a null reference since its never initialized. In order to access ControlPanel, you need to set a valid reference to it. If your ControlPanel.cs is on another form, you need to get that reference from there. Either through a public property or interface.