0

I have the following two pieces of code, please take a look at it, I pointed where it is going wrong. I removed the functions where I call the second window, they do not make sense here.

First, main form, this form calls the second form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace STP_Design
{
    public partial class STP2Main : Form
    {
        public STP2Main()
        {
            InitializeComponent();
            tabControl1.SelectedTab = tabPageDeviceManager;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MenuForm MDIMF = new MenuForm();
            MDIMF.MdiParent = this;
            MDIMF.StartPosition = FormStartPosition.Manual;
            MDIMF.Location = new Point(3, 50);
            MDIMF.Show();
            tabControl1.Visible = false;
        }

        public void set()
        {
            tabControl1.Visible = true; // This statement executes, but does not result in anything I'd expect. The debugger tells me the visibility is false.
            tabControl1.BringToFront();
        }
   }
}

and second form, which I close and should update the first form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace STP_Design
{
    public partial class MenuForm : Form
    {
        public MenuForm()
        {
            InitializeComponent();

            this.BringToFront();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            STP2Main stp = new STP2Main();
            stp.set();
            this.Close();
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
2pietjuh2
  • 879
  • 2
  • 13
  • 29

1 Answers1

1

You're calling the set method on a new version of the main form, rather than on the version you presumably already have displayed to the user.

What you need to do is get the current main form from the MdiParent property of the menu form, and call the method on that instead.

// In menu form
private void button1_Click(object sender, EventArgs e)
{
    var mainForm = this.MdiParent as STP2Main;
    if (mainForm != null)
        mainForm.set();
    this.Close();
}
Rawling
  • 49,248
  • 7
  • 89
  • 127
  • OK, so i should change "STP2Main stp = new STP2Main();" to " public STP2Main MainForm { get; set; }". When I do that, i get a null reference error on "MainForm.Set();" – 2pietjuh2 Oct 19 '12 at 12:01
  • 1
    @2pietjuh2 I've changed my code to match the code you posted, hopefully this should work for you. – Rawling Oct 19 '12 at 12:02