如何在整个程序中显示一个Winform的单个实例?

发布于 2024-11-30 02:53:16 字数 2075 浏览 1 评论 0原文

如果我的问题和描述看起来太简单或者已经被其他人问过,请接受我的道歉。

我以前可能问过类似的问题。不过,我对winform还是有点困惑。假设您有一个包含 Mainform、Form1、Form2、Form3 的 Delphi prism .net 程序。另外,您希望能够显示 Mainform、Form2 和 Form3 中 Form1 的单个瞬间。你是怎么做到的?我必须有一个 winform,需要在整个程序中根据需要显示,以显示 RichTextBox 中的应用程序错误。这意味着我的程序中可以随时随地调用SysErrorDlg winform来显示程序错误。对于我来说,如果在整个程序中只能访问 SysErrorDlg winform 的单个实例,我就能够做到这一点。

请遵循下面非常简单的代码。这就是我正在尝试做的事情。

=========================================
Mainform

using
Form1;
Form2;
Form3;   

Mainform1 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
F1:Form1;
end;

constructor MainForm1;
begin
   F1 := new Form1;
end;

method Mainform1.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   F1.Show; // or ShowDialog;
end;

=====================================================
Form1

Form1 = class(System.Windows.Form)
private
public
constructor;
end;

constructor Form1;
begin

end;

=====================================================
Form2

using
Mainform;

Form2 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
end;

constructor Form2;
begin

end;

method Form2.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   MainForm1.F1.Show; // or ShowDialog;
end;

====================================================
Form3

Using
MainForm;

Form3 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
end;

constructor Form3;
begin

end;

method Form3.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   MainForm1.F1.Show; // or ShowDialog;
end;

============================================

有可能这样做吗?我的编译器不允许我声明全局 winform 变量,但抱怨它需要被识别为公共变量。即使有一个选项可以启用此功能,我也不想这样做,因为我喜欢将变量保留为类的私有或本地变量的想法。

如果已经在 Mainform 中声明并实例化了 form1 的实例,如何将 form1 的实例传递给 form2 或 form3?我了解 show 和 showdialog 的工作原理。如果 Form1 实例位于 MainForm 中,您将使用哪行代码从 Form2 访问 Form1 实例?

您可以提供一些代码以及解释。

Accept my apology if my question and the description seems too simple or already been asked by others.

I may have asked a similar question before. However, I am still confused a little about winform. Say you have a Delphi prism .net program with Mainform, Form1, Form2, Form3. Plus, you want to be able to display a single instant of a Form1 from within Mainform, Form2 and Form3. How do you do that? I have to have a winform that needs to be displayed as needed throughout the whole program to show application errors within RichTextBox. This means SysErrorDlg winform can be called anytime from anywhere in my program to display program errors. For me to be able to do this is if only single instance of SysErrorDlg winform is accessible throughout my whole program.

follow the very simple code below. That's pretty much what I am trying to do.

=========================================
Mainform

using
Form1;
Form2;
Form3;   

Mainform1 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
F1:Form1;
end;

constructor MainForm1;
begin
   F1 := new Form1;
end;

method Mainform1.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   F1.Show; // or ShowDialog;
end;

=====================================================
Form1

Form1 = class(System.Windows.Form)
private
public
constructor;
end;

constructor Form1;
begin

end;

=====================================================
Form2

using
Mainform;

Form2 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
end;

constructor Form2;
begin

end;

method Form2.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   MainForm1.F1.Show; // or ShowDialog;
end;

====================================================
Form3

Using
MainForm;

Form3 = class(System.Windows.Form)
private
method ShowBtn_Click(sender: System.Object; e: System.EventArgs)
public
constructor;
end;

constructor Form3;
begin

end;

method Form3.ShowBtn_Click(sender: System.Object; e: System.EventArgs);
begin
   MainForm1.F1.Show; // or ShowDialog;
end;

============================================

Is it even possible to do that? My compiler won't let me declare a global winform variable but complains that it needs to be identified to be a public. Even if there is an option to enable this feature, I don't want to for I like the idea of keeping variables private or local to classes.

How do you pass the instance of the form1 to form2 or form3 if an instance of form1 is already declared and instantiated within Mainform? I understand how show and showdialog work. What line of code would you use to access Form1 instance from Form2 if the Form1 instance is within MainForm?

You could provide a little code along with your explanation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

淑女气质 2024-12-07 02:53:16

听起来您需要为此使用单例模式
例如,在 c# 中,我可以这样定义 Form1

public partial class Form1 : Form
{
    static Form1 _theform;

    /// <summary>
    /// Gets the one and only instance of Form1.
    /// </summary>
    static public Form1 TheForm
    {
        get
        {
            if ( null == _theform )
            {
                _theform = new Form1();
            }

            return _theform;
        }
    }

    protected Form1()
    {
        InitializeComponent();
    }
}

然后,在我的应用程序中的任何位置,我都可以通过执行以下操作来获取 Form1 的相同实例:

Form1.TheForm.Show();

Sounds like you need to use the singleton-pattern for this.
For example, in c# I could define Form1 like this:

public partial class Form1 : Form
{
    static Form1 _theform;

    /// <summary>
    /// Gets the one and only instance of Form1.
    /// </summary>
    static public Form1 TheForm
    {
        get
        {
            if ( null == _theform )
            {
                _theform = new Form1();
            }

            return _theform;
        }
    }

    protected Form1()
    {
        InitializeComponent();
    }
}

Then, anywhere in my application I can get the same instance of Form1 by doing this:

Form1.TheForm.Show();
单身情人 2024-12-07 02:53:16

您可以使用 ShowDialog() 的帮助来实现此目的。例如,如果 F1 是 Form1 的对象,那么第一次使用 F1.ShowDialog() 调用 form1 将打开 Form1,第二次将不允许调用,直到第一个实例窗口是关闭的。但是 F1.Show() 将为每次调用打开 Form1 的多个窗口。

You, can use the Help of ShowDialog() for this purpose. For Example, if F1 is the Object of Form1 then by calling form1 with F1.ShowDialog() for first time will open the Form1 and for the second time it will not allow to call until the first instance window is gets closed. But where as F1.Show() will opens multiple windows of Form1 for every call.

挥剑断情 2024-12-07 02:53:16

使用 Form1.show() 显示 form1(而不是 Form1.showdialog())。
然后使用 Hide 而不是 Close 来隐藏表单: Me.Hide() 。这会留下一个正在运行的表单实例,并且应用程序中的其他表单可以访问它。

然后,例如,如果Mainform 将文本放入Form1 的richtextbox 中,那么当Form2 和Form3 显示Form1 时,文本就会在那里。

Display form1 using Form1.show() (instead of Form1.showdialog()).
Then use Hide instead of Close to hide the form: Me.Hide() . This leaves an instance of the form running, and it can be accessed by other forms in the application.

Then, for example, if Mainform puts text in a richtextbox of Form1, it will be there when Form2 and Form3 show Form1.

凉世弥音 2024-12-07 02:53:16

我认为保持表单实例处于活动状态可能不是最好的方法。我建议将日志记录与日志显示分开。

这个想法是使用 .NET 中可用的技术进行日志记录和/或跟踪(即静态 Trace 类),并简单地附加一个特定的侦听器,该侦听器将跟踪整个应用程序中的所有记录条目,并提供对这个日志。

然后您可以在需要时显示您的日志窗口。此日志窗口仅访问侦听器并使用数据绑定在文本字段中显示侦听器内容。

通过使用数据绑定,您还可以确保一旦新条目写入日志,显示就会自动更新。

这样,您可以在需要时关闭并重新实例化表单,而无需“破解”您当前遇到的限制。

I think keeping a form's instance active is probably not the best approach. I'd suggest to separate the logging from the display of the log.

The idea would be to use the technologies available in .NET for logging and/or tracing (i.e. static Trace class) and simply append a specific listener that will keep track of all logged entries throughout the application, as well as provide read-access to this log.

Then you can show your Log-window whenever required. This log window simply accesses the listener and uses databinding to display the listeners content in the text field.

By using databinding you can also make sure that, as soon as new entries are written in the log, the display automatically updates.

This way you can close and re-instanciate the form whenever required without 'hacking' around the restrictions you currently encounter.

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