sumair_coolboy Posted September 24, 2011 Posted September 24, 2011 I have 4 forms.. MAINFORM.. FORM1 FORM2 FORM3 In my mainForm .. I have 3 button and 1 panel... I want to create application in which.. If i click button1.. PANEL will display form1.. if i click button2 .. panel will display form2.. if i click button3 .. panel will display form3... Quote
Administrators PlausiblyDamp Posted September 25, 2011 Administrators Posted September 25, 2011 Forms aren't normally contained inside of another form in that way, if you were to make form1, form2 and form3 into user controls instead then things would be a lot easier. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Napivo1972 Posted September 26, 2011 Posted September 26, 2011 Maybe try this Probably being the Laziest programmer in the known universe... When I need to do something like this in VS10 I design a from in the designer I change the inherited item from form to panel I modify InitializeComponent() in the designer code, to start as a panel instead of a form Now just add the new panels to your form and make them visible and invisible as you like. This works quite well, Quick and dirty :) Quote
Administrators PlausiblyDamp Posted September 26, 2011 Administrators Posted September 26, 2011 Re: Maybe try this Not sure about lazy as it seems to be effort than just using a UserControl to get the same result but without having to hack at designer generated code. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Napivo1972 Posted September 26, 2011 Posted September 26, 2011 Of course, you are right... as usual :D Can't remember why I started doing something stupid like this in the first place. I only recently started programming again, after a break of a year or so... Must still be rusty Quote
sumair_coolboy Posted October 5, 2011 Author Posted October 5, 2011 (edited) I found the solution.. . private void MainForm_Load(object sender, EventArgs e) { this.btn_show1.Click += new EventHandler(btn_show_Click); this.btn_show2.Click += new EventHandler(btn_show_Click); this.btn_show3.Click += new EventHandler(btn_show_Click); } void btn_show_Click(object sender, EventArgs e) { this.pnl_ShowForms.Controls.Clear(); int tag = Convert.ToInt32( (sender as Button).Tag); switch (tag) { case 1: Form1 frm1 = new Form1(); frm1.FormBorderStyle = FormBorderStyle.None; frm1.Dock = DockStyle.Fill; frm1.WindowState = FormWindowState.Maximized; frm1.TopLevel = false; this.pnl_ShowForms.Controls.Add(frm1); frm1.Show(); break; case 2: Form2 frm2 = new Form2(); frm2.FormBorderStyle = FormBorderStyle.None; frm2.Dock = DockStyle.Fill; frm2.WindowState = FormWindowState.Maximized; frm2.TopLevel = false; this.pnl_ShowForms.Controls.Add(frm2); frm2.Show(); break; case 3: Form3 frm3 = new Form3(); frm3.FormBorderStyle = FormBorderStyle.None; frm3.Dock = DockStyle.Fill; frm3.WindowState = FormWindowState.Maximized; frm3.TopLevel = false; this.pnl_ShowForms.Controls.Add(frm3); frm3.Show(); break; } } Edited October 6, 2011 by snarfblam Quote
Leaders snarfblam Posted October 6, 2011 Leaders Posted October 6, 2011 If you're going to be using these exactly like controls, why would you create them as forms? It's no extra effort to use a UserControl instead. All you are doing is opening yourself up to the possibility of quirky or problematic unexpected behavior. Quote [sIGPIC]e[/sIGPIC]
EFileTahi-A Posted November 7, 2011 Posted November 7, 2011 An alternative: you could set your application's main form to be a MDI container, where your main form houses all other MDI child forms. Pretty much like a traditional image processor (Photo Shop) if you don't know what I'm talking about. By using this you could easily open / close and set the position of all windows through the buttons you described. http://msdn.microsoft.com/en-us/library/d4dabts7%28v=vs.80%29.aspx Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.