hi guys,

sorry to pester you all again.

im having a problem trying to show the hidden main form, any ideas? becuase only way i know hpw to do it (in C#) is to create a new form, but then that loses my data

cheers

Recommended Answers

All 4 Replies

As I understand it well, I think you could create your main form at startup of your program and then call the form.Hide() method, fetch your data and then call from.Show() method. If I'm wrong, please make your question a bit clearer.

hi,

for example in VB.net

you could use

//in form 1//
form2.show()
me.hide()

//then in form 2//
form1.show()
me.close()

but in C# this doesn't seem to work, i can hide the form fine (this.hide();) but when in the second form i cannot recall the form (form1.show();) as it doesn't seem to exsist, but i tested it by taking out the .hide(); and it just sits in the background (unpleasant) lol

Can this help?

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form Form2 = new Form(); //create second form

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Form2.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            Form2.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            Form2.ClientSize = new System.Drawing.Size(284, 262);
            Form2.Name = "Form2";
            Form2.Text = "Form2";
            Form2.Hide();         //Hide it 
        }

        //button on form1 to show form2
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Form2.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

for example in VB.net ....

but in C# this doesn't seem to work, i can hide the form fine (this.hide();) but when in the second form i cannot recall the form (form1.show();) as it doesn't seem to exsist, but i tested it by taking out the .hide(); and it just sits in the background (unpleasant) lol

That is because VB.Net with the Application Framework enabled automagically creates and maintains a globally accessible instance of all forms in your project. This is one feature that I wish that they never implemented; it does make some things easier (I guess that was the intent), but it really messes with the minds of programmers who are not used to having objects automagically created and made accessible.

I really dislike the idea globally accessible objects and feel that a class needs a reference to an external object, that reference should be purposely set. The reference can either be a property or passed in the constructor.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            this.Hide();
            f2.ShowDialog();
        }
    }
}

Now for Form2:

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private Form _FormThatShowedMe;
        public Form2(Form FormThatShowedMe) // pass reference 
        {
            if (FormThatShowedMe == null)
            {
                throw new ArgumentException("Hey! I need a valid form");
            }
            InitializeComponent();
            _FormThatShowedMe = FormThatShowedMe;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            _FormThatShowedMe.Show();
            this.Close();
        }
    }
}
commented: Totally agree! I like C# because it is so strict in many matters +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.