Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
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:-
C#
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);            
        }       
    }


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...
Posted

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;
                }
            }
        }
    }
}
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.
 
Share this answer
 
v2
Comments
JayantaChatterjee 30-Nov-13 10:24am    
Sir,
Its Works fine,This is I need...
Hats Off to You Sir(You did Just In 15-Mins).. WOW
Thank You Very muchhhhhhhhhhhhhhhhhhhhhhhh Sir........ :-)
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:
C#
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?

[EDIT]

With multiple radio buttons in one parent control (but how else?), you should not have this property. You can have, say:
C#
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
   }
}


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
 
Share this answer
 
v2
Comments
JayantaChatterjee 30-Nov-13 0:59am    
Sir, Its works fine ...
But only one problem is there: when I set IsChecked Property of user control is false then the last control's IsChecked is true(among four control the last is MyCustRadio4)...
My form code is :-
private void Form2_Load(object sender, EventArgs e)
{
myCustRadio1.Text = "1";
myCustRadio2.Text = "2";
myCustRadio3.Text = "3";
myCustRadio4.Text = "4";
unCheck();
}
private void unCheck()
{
myCustRadio1.IsChecked = false;
myCustRadio2.IsChecked = false;
myCustRadio3.IsChecked = false;
myCustRadio4.IsChecked = false;
}
How to resolved this ???
BillWoodruff 30-Nov-13 1:30am    
If each of your custom UserControls has one RadioButton, then each UserControl will have its own unique state of whether the RadioButton is checked or not.

Your code above ... if you call unCheck() ... sets all of the four Custom UserControls' RadioButtons to unchecked.

Is your goal here to have one-and-only-one custom UserControl that has the RadioButton checked ?

I think we are struggling here to understand exactly what you wish to do. Please try to clarify your original question.
JayantaChatterjee 30-Nov-13 1:33am    
Yes I have only one radio button in my User control...
Sergey Alexandrovich Kryukov 30-Nov-13 1:40am    
I answered, please see my update to the answer, after [EDIT].

But I did not assume you have only one radio button in your custom control. You cannot do how you want. Instead, you should have another control to be used as a group of your controls, or you should have a user control with several child radio buttons; that would be a better approach. Or (#3), you should have a utility binding your instances of your control together.

This is not a problem, but correct behavior. You should understand that all check buttons in the same parent behave in a coordinated manner: you check up, others become unchecked. So, you need to operate them through selecting just one.
I don't know what properties do you want to have in your control and why. If you explain, I'll explain how to do that.
—SA
JayantaChatterjee 30-Nov-13 1:55am    
Sir,
I want to create a option button which have the scroll bar to display its overflow text..
So first I create a custom textbox(which doesn't show the cursor) and a ratio button, and combine them in a user control.
I need two property: 1) Text property, 2) Checked Property and both are set and get type,also this user control behave like an system radio button...

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.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 29-Nov-13 23:24pm    
I think the problem is quite simple: OP ignores the evens of radioButton1. (And OP does use this control.)
Properties and events of the child control and parent controls are not properly bound, it is not even attempted...
Please see my solution...
—SA

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