A new form in same form window

show/hide form controls

Page 1 of 1

10 Replies - 5008 Views - Last Post: 09 March 2010 - 02:21 PM Rate Topic: -----

#1 Guest_xikky*


Reputation:

A new form in same form window

Posted 09 March 2010 - 09:30 AM

hi there!

I want to know if it is possible to change the controles of a form. For example if I have a button in a form, and when clicked, other controles show up in the same form and the previous controls are removed.

I've tried to search for some help and i found something about panels ... but i can't manage to get them to work.

hope u have understood my problem ^^

thanks a lot!

Is This A Good Question/Topic? 0

Replies To: A new form in same form window

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: A new form in same form window

Posted 09 March 2010 - 09:38 AM

It is possible. Every control has a .Show() and .Hide() method that shows or hides control. The other option is to remove the control using this.Controls.Remove method, where 'this' is a form.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_xikky*


Reputation:

Re: A new form in same form window

Posted 09 March 2010 - 09:48 AM

thanks for the fast reply FlashM :)

the .show() and .hide() methods would be a good idea but not practical when having a good number of controls on the form i guess.

this.Controls.Remove methods seems fine.. (i will check it out soon) but will i be able to re-show the previous controls again?

what i have is like a menu and i want to bring up the specific controls in the same form when an item is selected from the menu. It would be easier for me to create an new form and it's instance and use the 'instance'.ShowDialog() method. But i wish to learn the possibility off manipulating controls on the same form xD
Was This Post Helpful? 0

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: A new form in same form window

Posted 09 March 2010 - 09:54 AM

In this case I believe .Show() and .Hide() is a better option. If you are not in a hurry, I can post you some sample code how I would do that, but it will take me at least 2-3 hours, cuz I'm busy at the moment.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_xikky*


Reputation:

Re: A new form in same form window

Posted 09 March 2010 - 10:14 AM

no problems m8 xD

i can work on something else at the moment. i would be very pleased 2 c some good code 2 learn from. .. i'm still a beginner in c# and in programming!

much thanks!
Was This Post Helpful? 0

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: A new form in same form window

Posted 09 March 2010 - 12:16 PM

This is one of the ways you could do it... If you need any explanation, feel free to ask.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

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

        //Initialize the controls that I want to be able to show or hide
        private void InitializeMyControls()
        {
            TextBox tb_1 = new TextBox();
            tb_1.Location = new Point(10, 10);
            tb_1.Size = new Size(200, 15);
            tb_1.Name = "txtName";
            this.Controls.Add(tb_1); //add textbox to a form

            TextBox tb_2 = new TextBox();
            tb_2.Location = new Point(10, 35);
            tb_2.Size = new Size(200, 15);
            tb_2.Name = "txtAddress";
            this.Controls.Add(tb_2); //add textbox to a form

            Button bt_1 = new Button();
            bt_1.Location = new Point(220, 10);
            bt_1.Size = new Size(75, 23);
            bt_1.Name = "btnSave";
            bt_1.Text = "My button";
            this.Controls.Add(bt_1); //add button to a form


            my_controls = new List<Control>();

            //add controls to my controls collection
            my_controls.Add(tb_1);
            my_controls.Add(tb_2);
            my_controls.Add(bt_1);
        }


        private void btnShowControls_Click(object sender, EventArgs e)
        {
            //Show all controls in my controls collection
            ShowOrHideControlsMethod(true);
        }
        

        private void btnHideControls_Click(object sender, EventArgs e)
        {
            //Hide all controls in my controls collection
            ShowOrHideControlsMethod(false);
        }


        private void ShowOrHideControlsMethod(bool show)
        {
            if (my_controls != null && my_controls.Count > 0)
            {
                foreach (Control c in my_controls)
                {
                    if (c != null)
                    {
                        if (show)
                            c.Show();
                        else
                            c.Hide();
                    }
                }
            }
        }


        //this list will hold all my controls that I want to hide or show
        private IList<Control> my_controls = null;
    }
}



You could also use Panels or UserControls to achieve something very similar, but this really depends on what you are trying to do... If you provide us with some of your code, it would be easier to help you and provide you with best possible solution :-)
Was This Post Helpful? 1
  • +
  • -

#7 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: A new form in same form window

Posted 09 March 2010 - 12:20 PM

I think a better solution might be to use a tabbed view. Don't get me wrong the answer from FlashM is great, I just think that tabs might be a little more user friendly for a beginner. I don't have the time right now to mash out some code for you but below is a link to the MSDN article about it.

MSDN Tabcontrol class article
Was This Post Helpful? 0
  • +
  • -

#8 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: A new form in same form window

Posted 09 March 2010 - 12:49 PM

Tab controls are great but I have to admit that data sharing between different tab pages could be quite a challenge if you want to write a nice and clean approach. And there is one more thing about tab control: TabPage does provide a .Hide() and .Show() method, but those two methods are only inherited from a Control class and not really implemented...
Was This Post Helpful? 0
  • +
  • -

#9 Guest_xikky*


Reputation:

Re: A new form in same form window

Posted 09 March 2010 - 01:04 PM

worked just fine FlashM xD thanks a lot once again for your help! ... now it looks so simple ^^

thanks 4 ur tip as well adkins :) will check that out later on.

problem solved!!
Was This Post Helpful? 0

#10 Guest_xikky*


Reputation:

Re: A new form in same form window

Posted 09 March 2010 - 02:06 PM

something else that can be done ...

i'm now using panels that acts like a controls container and i can show/hide the panel only. unfortunately it seems very confusing to design multiple panels :/

anyway! happy programming!
Was This Post Helpful? 0

#11 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: A new form in same form window

Posted 09 March 2010 - 02:21 PM

Try using UserControl instead of panels. You can design each individual user control just like you would design your form in a designer. Then create an instance of your user control and dock it into your container form.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1