我可以在单选按钮选择中隐藏Windows窗体中的组框 [英] Can I Hide The Groupbox In Windowsform On Radio Button Selections

查看:74
本文介绍了我可以在单选按钮选择中隐藏Windows窗体中的组框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在单选按钮选项中隐藏windowsform中的Groupbox



这里我将Two Groupbox放在一个groupbox中我拿产品

另一个类别

和我拍摄2 Radiobutton

一个用于分类,另一个用于产品

默认我选择的无线电按钮用于分类RadioButton

如果我选择产品radioButton我想隐藏类别组框并显示产品组框

plz其紧急

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

推荐答案

GroupBox有一个可见属性 - 根据您所需的逻辑设置它是否有任何问题?
GroupBox has a Visible property - Do you have any problems setting it according to your required logic?


//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




GroupBoxes就像任何其他控件一样:它们有一个Visible属性。


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



首先评论一下你的设计。您有两个GroupBox,一次只能显示其中一个。您需要一个控件,一次只能激活两个状态中的一个。提供排他性的控件是RadioButton,而不是Button。我会放置两个RadioButtons,名为 product_RB ,标签为 product category_RB ,标签为 category ,进入第三个GroupBox,名为 controls_GB 。我将描述下面标签的使用。将两个RadioButtons的 CheckChanged 事件处理程序设置为,例如 RB_CheckChanged


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.



我会声明两个布尔变量,命名为 product_active category_active 来跟踪哪个GroupBox处于活动状态。将 product_active 初始化为 true 并将 category_active 初始化为 false


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.



标签产品类别在事件处理程序 RB_CheckChanged 中用于检测单击了哪个RadioButton。实际上,当单击其中一个RadioButton时,会调用事件处理程序两次:一次是RadioButton被未被点击,一次被RadioButton点击。


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.



最后,我不知道你是否重叠了你的GroupBoxes,但这是一种避免使用有价值的房地产的有用方法。


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

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.


这篇关于我可以在单选按钮选择中隐藏Windows窗体中的组框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆