Вопрос

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.

Это было полезно?

Решение

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();

Другие советы

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.

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.

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top