在C#中从用户Control获取属性值的问题 [英] Problem to get property value from user Control in C#

查看:90
本文介绍了在C#中从用户Control获取属性值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个自定义控件(用户控件),所以它结合了文本框和选项按钮。

我的问题是当我试图获得控件时checked(Property)值返回默认值(为false)...



我的用户控制代码是: -

Hi,
I created a Custom control(User control),so its combined with textbox and option button.
My problem is when I try to get the control checked(Property) value its return default value(which is false)...

My User Control code is:-

public partial class MyCustRadio : UserControl
    {
        string _text=string.Empty;
        bool _checked = false;
        public MyCustRadio()
        {
            InitializeComponent();
        }
        public override string Text{
            get { return _text; }
            set { _text = value; textAsLabel1.Text = _text; Invalidate(); }
    }
        
        public bool Checked{
            get { return _checked; }
            set { _checked = value; radioButton1.Checked = _checked; }
        }
       
        private void UserControl1_Click(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
            _checked = radioButton1.Checked;
            if (this.Checked)
                foreach (Control myBut in Parent.Controls)
                {
                    if (myBut is MyCustRadio && !myBut.Equals(this))
                    {
                        MyCustRadio ctrl = (MyCustRadio)myBut;
                        ctrl.Checked=false;
                    }
                }            
        }

        private void textAsLabel1_Click(object sender, EventArgs e)
        {
            UserControl1_Click(sender,e);            
        }       
    }





这是一些问题:

有时候它没有作为一个选项按钮工作(如果我选中了一个按钮,所有其他按钮都取消选择)我在groupBox控件中使用它。

和checked属性不返回当前值..

请告诉我如何解决这个问题....



感谢高级..



Regard

Jayanta ...



Here is some problems:
some times its doesn't work as a option button(like if i checked a button all the other button are deselect)I'm using it in a groupBox control.
and the checked property doesn't return the current value..
Please tell me how to Solve this problems....

Thanks in Advanced..

Regard
Jayanta...

推荐答案

我已经回过头来解决你原来的问题了我不明白你的目标是什么。这是一个可以用来试验的工作模板...希望可以学习。



在这种情况下,挑战很有趣:你有点弯曲 RadioButton是一个它不是为它设计的任务。在这种情况下......一个单一答案是正确的问卷...你想要一组UserControls行为好像他们各自的RadioButtons是同一容器中一组RadioButtons的一部分。



我创建了一个名为'ucQuestion的UserControl,它有一个TextBox和一个RadioButton。



I've gone back over your original question and realized I didn't understand what your goal is here. Here's a working template you can use to experiment with ... hopefully learn from.

The challenge, in this case, is interesting: you are kind of "bending" the RadioButton to a task it wasn't designed for. In this case ... a single-answer-is-correct questionnaire ... you want a group of UserControls to behave as if their individual RadioButtons are part of a group of RadioButtons in the same container.

I created a UserControl named 'ucQuestion that has one TextBox, and one RadioButton.

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

namespace Nov30_QuestionnaireProtoType
{
    public partial class ucQuestion : UserControl
    {
        public ucQuestion()
        {
            InitializeComponent();
        }

        // expose the TextBox
        public TextBox theUCTextBox { get; private set; }

        // expose the CheckState of the RadioButton
        public CheckState theUCCheckState { get; private set; }

        // keep a List of all ucQuestions created
        private static List<ucQuestion> ucQuestionList; 

        // use of a boolean variable here is unfortunately
        // required to deal with the quirks of having a single
        // RadioButton in a Control
        private bool isRadioButtonChecked = false;

        private void ucQuestion_Load(object sender, EventArgs e)
        {
            // initialize the static List<ucQuestion> only once
            if(ucQuestionList == null) ucQuestionList = new List<ucQuestion>();

            // add the current instance of this UserControl to the List of ucQuestion
            ucQuestionList.Add(this);

            // set the Public Property that exposes the internal TextBox
            theUCTextBox = textBox1;
            
            // initialize the Public Property that exposes the internal RadioButton
            // CheckState: note that's an Enumeration, not a boolean value !
            theUCCheckState = CheckState.Unchecked;
            
            // for "safety" ... probably unnecessary
            radioButton1.Checked = false;
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            isRadioButtonChecked = ! isRadioButtonChecked;
            radioButton1.Checked = isRadioButtonChecked;
            theUCCheckState = isRadioButtonChecked ? CheckState.Checked : CheckState.Unchecked;
            
            // if the RadioButton is Checked, make sure all other
            // ucQuestion Controls in the List<ucQuestion>
            // have their RadioButtons Unchecked
            if (isRadioButtonChecked)
            {
                foreach (ucQuestion theUCQuestion in ucQuestionList)
                {
                    if (theUCQuestion == this) continue;

                    theUCQuestion.isRadioButtonChecked = false;
                    theUCQuestion.radioButton1.Checked = false;
                    theUCQuestion.theUCCheckState = CheckState.Unchecked;
                }
            }
        }
    }
}

这是匆忙(十五分钟)完成的,我没时间了对于今天,它没有经过全面测试。一个明显的限制是使用静态的ucQuestions列表意味着你不能创建多组ucQuestions:它们都引用相同的List。这可能会,也可能不会与你想要完成的事情一起玩得很好。



如果你需要创建多个独立的ucQuestions组,那么下一步可能是创建一个复合用户控件,用于托管一组ucQuestions。

This was done in a hurry (fifteen minutes), and I'm out of time for today, so it's not fully tested. An obvious limitation is that using a static List of ucQuestions means you can't create multiple groups of ucQuestions: they'd all reference the same List. That may, or may not, "play well" with what you are trying to accomplish.

If you needed to create multiple "independent" groups of ucQuestions, a logical next step might be to create a compound-UserControl designed to host a group of ucQuestions.


那么,你会期待什么?您完全忽略 radioButton1 上的已检查事件(并且永远不会离开自动生成的名称,它们违反(良好)Microsoft命名条件,始终重命名为某些语义敏感的名称)。 />


以下是解决方法:
Well, what would you expect? You totally ignore the checked event on radioButton1 (and never leave auto-generated names, they violate (good) Microsoft naming condition, always rename to some semantically sensitive names).

Here is how it can be resolved:
public partial class MyCustRadio : UserControl {

    public MyCustRadio {
        radioButton1.CheckedChanged += (sender, eventArgs) => {
            if (this.CheckedChanged != null)
                this.CheckedChanged.Invoke(this, new eventArgs);
        }
    }

    public event System.EventHandler CheckedChanged;

    public bool IsChecked {
       get { return radioButton1.IsChecked; }
       set { radioButton1.IsChecked = value; }
    }

}





你得到的照片吗?







在一个父控件中有多个单选按钮(但是怎么回事?),你不应该拥有这个属性。您可以说:



Are you getting the picture?



With multiple radio buttons in one parent control (but how else?), you should not have this property. You can have, say:

byte CheckedRadioButton {
   get { return this.chechedRadioButton; } // the value should be written by the event handler
   set {
       System.Diagnostics.Debug.Assert(value <= checkBoxes.Length);
       checkBoxes[value].Checked = true;
       // all other will automatically uncheck
       // no need to set this.chechedRadioButton value:
       // it  will be done by the event handler
   }
}





在事件处理程序中(为其组中的每个单选按钮添加事件 CheckedChanged ,添加代码以设置<$ c $的值) c> this.chechedRadioButton 到刚刚检查过的单选按钮的索引。



-SA



In the event handler (added for the event CheckedChanged for every radio button in its group, add the code to set the value of this.chechedRadioButton to the index of the radio button which has been just checked.

—SA


这里的问题是RadioButton Control在WinForms中的工作方式的结果:它不是你在这种情况下需要的控件。



按照设计,RadioButton最初处于未检查状态......如果你没有明确设置Checked状态您的代码作为表单加载,或者在属性网格浏览器中为RadioButton在设计时显式设置Checked状态。



一旦你点击RadioButton ...当只有其中一个...你将无法在运行时将Checked状态设置为在用户界面中通过直接操作取消检查...当然,你可以在代码中设置它。



解决方案很简单:使用CheckBox控件。



更大图片:RadioButton意为与其他RadioButtons一起使用,其目标是一次性检查共享同一容器控件或表单的所有RadioButton的唯一选项。
The problem here is a result of the way the RadioButton Control works in WinForms: it is not the Control you need in this scenario.

By design, a RadioButton initially comes up in an un-checked state ... if you have not explicitly set the Checked state in your code as the Form Loads, or explicitly set the Checked state at design-time in the Property Grid Browser for the RadioButton.

Once you've clicked on a RadioButton ... when there's only one of them ... you will be unable at run-time to set the Checked state to un-checked by direct action in the user-interface ... you, of course, could set it in code.

The solution is simple: use a CheckBox Control.

Bigger picture: a RadioButton is meant to be used along with other RadioButtons where your goal is to have one-and-only option checked at a time for all RadioButtons that share the same Container Control or Form.


这篇关于在C#中从用户Control获取属性值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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