Call functions between different classes

I use to write nested c and poorly qualified in C #.

My problem is that I want to be able to run the function openAnotherForm()from Welcome_Form, and now the code is not working. I patiently tried different things, but only managed to push my disappointment.

I simplified my relevant code to illustrate the problem.

File 1 - this will start and file 2 will open.

class UIcode
{
    private Welcome_Form Welcome;
    private AnotherForm_Form AnotherForm;

    public UIcode()
    {
        Welcome = new Welcome_Form();
        Application.Run(Welcome);
    }

    public void openAnotherForm()
    {
        Welcome.Hide();
        AnotherForm = new AnotherForm_Form();
        AnotherForm.ShowDialog();
    }
}

File 2 - When I click TheButton, the program should run the function openAnotherFromfrom file 1.

public partial class Welcome_Form : Form
{
    public Welcome_Form()
    {
        InitializeComponent();
    }

    private void TheButton_Click(object sender, EventArgs e)
    {
        // Function from file 1
        UIcode.openAnotherForm();
    }
}

I understand that the problem can be quite trivial, but I will still be grateful for an explanation of how to do this.

Preferably: functions from the UI code should only be recognized by the classes specified in the UI code.

+3
source share
3

, UIcode, :

    private static UIcode myParent;

    public Welcome_Form(UIcode parent)
    {
        myParent = parent;
        InitializeComponent();
    }

UIcode:

   public UIcode()
   {
        Welcome = new Welcome_Form(this);
        Application.Run(Welcome);
   }

, , Welcome_Form:

    private void TheButton_Click(object sender, EventArgs e)
    {
        // Function from file 1
        myParent.openAnotherForm();
    }
+3

openAnotherForm() static, . UICode, static.

0

File1 . UICode, public UserInterface() public UICode().

class UIcode
{
    private Welcome_Form Welcome;
    private AnotherForm_Form AnotherForm;

    public UIcode() // Renamed Constructor
    {
        Welcome = new Welcome_Form();
        Application.Run(Welcome);
    }

    public void openAnotherForm()
    {
        Welcome.Hide();
        AnotherForm = new AnotherForm_Form();
        AnotherForm.ShowDialog();
    }
}

public partial class Welcome_Form : Form
{
    public Welcome_Form()
    {
        InitializeComponent();
    }

    private void TheButton_Click(object sender, EventArgs e)
    {
        // Create an instance UICode
        UICode instance = new UICode();

        // Call the method from the instance, not from the class.
        instance.openAnotherForm();
    }
}

openAnotherForm() a static, (Welcome AnotherForm) static. , , static.

class UIcode
{
    private static Welcome_Form Welcome;
    private static AnotherForm_Form AnotherForm;

    public static UIcode() // Renamed Constructor
    {
        Welcome = new Welcome_Form();
        Application.Run(Welcome);
    }

    public static void openAnotherForm()
    {
        Welcome.Hide();
        AnotherForm = new AnotherForm_Form();
        AnotherForm.ShowDialog();
    }
}
0

All Articles