Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Can i hide the Groupbox in windowsform on radio button selections

Here i take Two Groupbox in one groupbox i take product
another Category
and i taken 2 Radiobutton
one for Category and another for product
default i seleted radiobutton for Category RadioButton
if i select Product radioButton i want to hide Category Groupbox and display Product Groupbox
plz its urgent
Posted
Comments
[no name] 14-Apr-14 9:10am    
No it's not urgent at all. What have you tried?
Wessel Beulink 14-Apr-14 9:35am    
i think you can use the visible properties for this?

when radiobutton is checked or selected set the groupbox.visible to false?
[no name] 14-Apr-14 9:52am    
Why are you asking me? Of course you can use the Visible property for this. Elementary programming.

GroupBox has a Visible property - Do you have any problems setting it according to your required logic?
 
Share this answer
 
C#
//try below one 
1:gbProduct is groupbox for product
2:gbCategory is groupbox for category
3:rbCategory is radio for category
4:rbProduct is category for Product


private void Form1_Load(object sender, EventArgs e)
        {
            gbProduct.Visible = false;
            gbCategory.Visible = true;
        }

        private void rbProduct_CheckedChanged(object sender, EventArgs e)
        {
            if (rbProduct.Checked)
            {
                rbCatagory.Checked = false;
                gbProduct.Visible = true;
                gbCategory.Visible = false;
            }
        }

        private void rbCatagory_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCatagory.Checked)
            {
                rbProduct.Checked = false;
                gbProduct.Visible = false;
                gbCategory.Visible = true;
                
            }
        }

//i think it will help you
//let me know. i am waiting your reply
 
Share this answer
 


GroupBoxes are like any other control: they have a Visible property.



First some comments on your design. You have two GroupBoxes, only one of which should be displayed at a time. You need to have a control that can have only one of two states active at a time. The control that provides exclusivity is the RadioButton, not the Button. I would place two RadioButtons, named, say product_RB with a tag of product and category_RB with a tag of category, into a third GroupBox, named, say, controls_GB. I will describe the use of the tags below. Set the CheckChanged event handler for both RadioButtons to, say, RB_CheckChanged.



I would declare two boolean variables, named, say, product_active and category_active to track which GroupBox is active. Initialize product_active to true and category_active to false.



The tags product and category are used in the event handler RB_CheckChanged to detect which RadioButton was clicked. Actually, when one of the RadioButtons is clicked, the event handler is invoked twice: once for the RadioButton being "unclicked" and once for the RadioButton being clicked.



Lastly, I do not know if you are overlapping your GroupBoxes but that is a useful way to keep from using valuable real estate.


C#
using System;
using System.Windows.Forms;

namespace GroupBoxVisible
    {
    public partial class GroupBoxVisible : Form
        {

        bool  category_active = false;
        bool  product_active = true;

        // ******************************************** initialize_GUI

        void initialize_GUI ( )
            {

            control_GB.Visible = true;

            category_GB.Visible = category_active;
            product_GB.Visible = product_active;
            }

        // ******************************************* GroupBoxVisible

        public GroupBoxVisible ( )
            {

            InitializeComponent ( );

            initialize_GUI ( );
            }

        // ***************************************** RB_CheckedChanged

        void RB_CheckedChanged ( object    sender, 
                                 EventArgs e )
            {
            bool        is_checked = false;
            RadioButton radio_button = null;
            string      tag = String.Empty;

            radio_button = ( RadioButton ) sender;
            is_checked = radio_button.Checked;
            if ( radio_button.Tag == null )
                {
                throw new ApplicationException ( 
                    @"A tag is not assigned to the RadioButton" );
                }
            tag = radio_button.Tag.ToString ( ).ToLower ( ).Trim ( );
            if ( tag.Length <= 0 )
                {
                throw new ApplicationException ( 
                    @"A zero length tag is assigned to the " +
                    @"RadioButton" );
                }

            switch ( tag )
                {
                case @"product":
                    product_active = is_checked;
                    break;

                case @"category":
                    category_active = is_checked;
                    break;

                default:
                    throw new ApplicationException ( 
                        String.Format (
                            @"{0} is an unrecognized tag", tag ) );
                }

            initialize_GUI ( );
            }

        } // class GroupBoxVisible

    } // namespace GroupBoxVisible


Hope that helps.

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900