C# - 从另一种形式调用函数

发布于 2024-12-17 05:05:01 字数 1067 浏览 4 评论 0原文

我需要来自另一种形式的托盘通知。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埖埖迣鎅 2024-12-24 05:05:01

如果您从 ControlPanel 打开第二个表单“Form1”,则应该将 CP 的实例传递给 Form1,例如

public partial class ControlPanel : Form
{

    public void ShowForm1(){
        FOrm1 f1 = new Form1();
        f1.SetCp(this);
        f1.show();
    }

    public void TrayPopup(string message, string title)
    {
        TrayIcon.BalloonTipText = message;
        TrayIcon.BalloonTipTitle = title;
        TrayIcon.ShowBalloonTip(1);
    }
}

public partial class Form1 : Form
{

    public ControlPanel _cp;
    public void SetCP(controlPanel cp){
            _cp = cp;
    }

    private void mouse_Up(object sender, MouseEventArgs e) {
            if(_cp != null)
            _cp.TrayPopup("TRAY POPUP THIS", "test");
    }
}

If you are opening second form "Form1" from ControlPanel, you should pass the instance of CP to Form1, like

public partial class ControlPanel : Form
{

    public void ShowForm1(){
        FOrm1 f1 = new Form1();
        f1.SetCp(this);
        f1.show();
    }

    public void TrayPopup(string message, string title)
    {
        TrayIcon.BalloonTipText = message;
        TrayIcon.BalloonTipTitle = title;
        TrayIcon.ShowBalloonTip(1);
    }
}

public partial class Form1 : Form
{

    public ControlPanel _cp;
    public void SetCP(controlPanel cp){
            _cp = cp;
    }

    private void mouse_Up(object sender, MouseEventArgs e) {
            if(_cp != null)
            _cp.TrayPopup("TRAY POPUP THIS", "test");
    }
}
与酒说心事 2024-12-24 05:05:01

不需要每次都分配内存,试试这个

public partial class Form1 : Form
{

    public ControlPanel cp = new ControlPanel();
    ....

    private void mouse_Up(object sender, MouseEventArgs e) {   
    CP.TrayPopup("TRAY POPUP THIS", "test");
    }
}

don't need to allocate memory each time , try this

public partial class Form1 : Form
{

    public ControlPanel cp = new ControlPanel();
    ....

    private void mouse_Up(object sender, MouseEventArgs e) {   
    CP.TrayPopup("TRAY POPUP THIS", "test");
    }
}
狠疯拽 2024-12-24 05:05:01

您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文