首页 新闻 赞助 找找看

RadioButton显示选中的问题

0
悬赏园豆:10 [已关闭问题] 关闭于 2013-09-10 16:02

 

想实现如下功能:

在tabControl标签中点击测试按钮,会向tabStudent标签中发送一个ID,然后进入tabStudent标签界面,会看到ID对应的RadioButton为选中状态

例如发送的ID为2,那么要显示RadioButton2为选中状态.

 

问题是现在ID值传过去,点击标签进入tabStudent界面,但是RadioButton没有显示为选中状态

 

 

问题补充:

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace cao
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.tabControl1.SelectedTabIndex = 2;
        }

        private void tabItem1_Click(object sender, EventArgs e)
        {
            frmSound frmsound = new frmSound();
            frmsound.Dock = DockStyle.Fill;
            this.tabControlPanel1.Controls.Add(frmsound);
            frmsound.Show();
        }

        private void tabItem2_Click(object sender, EventArgs e)
        {
            frmStudent frmstu = new frmStudent();
            frmstu.Dock = DockStyle.Fill;
            this.tabControlPanel2.Controls.Add(frmstu);
            frmstu.Show();
        }


    }
}

 

 

-----

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace cao
{
    public partial class frmSound : UserControl
    {
        public static int sid = 0;

        public int ID
        {
            set { sid = value; }
            get { return sid; }
        }
        public frmSound()
        {
            InitializeComponent();
            if (ID != 0)
            {
                changeview(ID);//有参数传递过来就改变选中的RadioButton
            }
            else
            {
                changeview(4);//默认的选中是RadioButton5
            }
        }

        private void changeview(int i)
        {
            try
            {
                foreach (Control c in this.groupBox1.Controls)
                {
                    if (c.GetType().Name == "RadioButton")
                    {
                        RadioButton rbt = c as RadioButton;
                        if (i == Convert.ToInt32(rbt.Name.Substring(11)) - 1)
                        {
                            rbt.Checked = true;
                        }

                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}

 

-----

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace cao
{
    public partial class frmStudent : UserControl
    {
        public frmStudent()
        {
            InitializeComponent();
            this.comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmSound.sid = Convert.ToInt32(this.comboBox1.Text.Trim());
        }

    }
}

 

nightfire的主页 nightfire | 初学一级 | 园豆:94
提问于:2012-01-12 11:30
< >
分享
所有回答(3)
0

在测试按钮的单击事件中做判断,根据设定值找到对应的单选按钮并赋值为选中状态

ERS | 园豆:728 (小虾三级) | 2012-01-12 11:36

这个方法我之前也试过,值是传过去了,状态同时也设置为CHECKED。但是我点标签到tabStudent界面后,对应的RadioButton并不是选中状态。

支持(0) 反对(0) nightfire | 园豆:94 (初学一级) | 2012-01-12 13:21

@nightfire: 

如果不行的话,可以怀疑是tab标签切换时,tab会进行重新加载,tab上面的标签都初始化了,你也许可以用一个static变量,当点击测试的时候对应的传入一个值,然后在加载tab标签的时候根据static变量进行单选按钮选中

支持(0) 反对(0) ERS | 园豆:728 (小虾三级) | 2012-01-12 13:34

@ERS: 多谢你的提醒,我再试试。

支持(0) 反对(0) nightfire | 园豆:94 (初学一级) | 2012-01-12 15:55
0

你单击测试按钮时,设置rado2的checked=true; 不就行了;

画方软件 | 园豆:778 (小虾三级) | 2012-01-12 11:50

要是控件都在一个界面就可以按你说的这个方法。关键是在不同的界面,我测试过不行。

        changeview(ID);

        private void changeview(int i)
        {
            try
            {
                foreach (Control c in this.panel1.Controls)
                {
                    if (c.GetType().Name == "RadioButton")
                    {
                        RadioButton rbt = c as RadioButton;
                        if (i == Convert.ToInt32(rbt.Name.Substring(11)) - 1)
                        {
                            rbt.Checked = true;
                        }

                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

支持(0) 反对(0) nightfire | 园豆:94 (初学一级) | 2012-01-12 13:18

@nightfire: 你是用的是winform吗,不行,笑话;

支持(0) 反对(0) 画方软件 | 园豆:778 (小虾三级) | 2012-01-12 13:20

@HuaFang: winform下,窗体上的任何一个控件都能直接使用;不像web嵌套控件使用那样麻烦;这种只是简单的容器

支持(0) 反对(0) 画方软件 | 园豆:778 (小虾三级) | 2012-01-12 13:23

@HuaFang: 是用了第三方的控件,但只是做为容器用的。

我这现在是不同的子窗体。

支持(0) 反对(0) nightfire | 园豆:94 (初学一级) | 2012-01-12 15:57

@nightfire: 不同的子窗体;没有看出来;

支持(0) 反对(0) 画方软件 | 园豆:778 (小虾三级) | 2012-01-12 15:59

@HuaFang: 不同的子窗体,tabcontrol空器中的两个灰色窗体,是程序运行时加的。

支持(0) 反对(0) nightfire | 园豆:94 (初学一级) | 2012-01-12 16:15

@nightfire: 你做事真是;哎,浪费时间呀;这样搞吧;定义一个基类,两都窗体都继承;单rado时设置基类变量值;你再操作另一个窗体时时获取这个值;

支持(0) 反对(0) 画方软件 | 园豆:778 (小虾三级) | 2012-01-12 16:18
0

同意@

kaleyroy | 园豆:360 (菜鸟二级) | 2012-01-12 11:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册