Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic radio buttons (Read 2202 times)
jgarrett
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 5
Joined: Oct 19th, 2011
radio buttons
Oct 19th, 2011 at 7:14am
Print Post  
Hi,

I am currently evaluating your Windows control. The stuff we can do with composite nodes is fantastic! For our project we need the user to select only one of several fixed outputs attached to the node and we'd like to present something like radio buttons for that. Do you plan to add radio button components, or is there an easy way to implement them using check-boxes - by overriding their appearance and allowing for only a single box to be checked?

Thanks,
- Jacob
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: radio buttons
Reply #1 - Oct 19th, 2011 at 11:28am
Print Post  
Hi,

There are no immediate plans to add radio button components to MindFusion.Diagramming but you can implement your own with relative ease. Simply derive from CheckBoxComponent, define custom images for the checked and unchecked states of your component through the ImageList and CheckedImageList properties, then override the OnClicked event and ensure that only one radio button can be selected at a time. For the later, you can use the notion of radio button groups: when one of the buttons in the group gets checked, the other components are automatically unchecked. Here is the code that implements this scenario:

Code
Select All
public class RadioButtonComponent : CheckBoxComponent
{
      public RadioButtonComponent()
      {
            this.ImageList = new List<Image>();
            this.ImageList.AddRange(new Image[]
                  {
                        Resources.Unchecked_Normal,
                        Resources.Unchecked_Down,
                        Resources.Unchecked_Disabled,
                        Resources.Unchecked_Over
                  });
            this.CheckedImageList = new List<Image>();
            this.CheckedImageList.AddRange(new Image[]
                  {
                        Resources.Checked_Normal,
                        Resources.Checked_Down,
                        Resources.Checked_Disabled,
                        Resources.Checked_Over
                  });
      }

      protected override void OnClicked(EventArgs e)
      {
            base.OnClicked(e);

            IsChecked = true;
            if (group != null)
                  group.Toggle(this);
      }

      public RadioButtonGroup Group
      {
            get { return group; }
            set
            {
                  if (group != null)
                        group.Remove(this);
                  group = value;
                  if (group != null)
                        group.Add(this);
            }
      }


      private RadioButtonGroup group;
}

public class RadioButtonGroup
{
      public RadioButtonGroup()
      {
            buttons = new List<RadioButtonComponent>();
      }

      internal void Add(RadioButtonComponent radioButton)
      {
            buttons.Add(radioButton);
      }

      internal void Remove(RadioButtonComponent radioButton)
      {
            buttons.Remove(radioButton);
      }

      internal void Toggle(RadioButtonComponent radioButton)
      {
            foreach (RadioButtonComponent button in buttons)
            {
                  if (button == radioButton)
                        continue;

                  button.IsChecked = false;
            }
      }


      private List<RadioButtonComponent> buttons;
} 


Below is a code snippet, which creates a CompositeNode with several radio buttons from an XML string. Note that the new RadioButtonComponent type will be automatically resolved by the XmlLoader as long as the RadioButtonComponent class is public.

Code
Select All
CompositeNode n = new CompositeNode();

n.Components.Add(XmlLoader.Load(@"
      <Border Background='Silver' Padding='2'>
            <StackPanel Orientation='Vertical' Spacing='1'>
                  <StackPanel Orientation='Horizontal' Spacing='1'>
                        <RadioButtonComponent Name='rb1' HorizontalAlignment='Left' VerticalAlignment='Center' Focusable='False' />
                        <Text Text='Option #1' Font='Arial, 8pt' />
                  </StackPanel>
                  <StackPanel Orientation='Horizontal' Spacing='1'>
                        <RadioButtonComponent Name='rb2' HorizontalAlignment='Left' VerticalAlignment='Center' Focusable='False' />
                        <Text Text='Option #2' Font='Arial, 8pt' />
                  </StackPanel>
                  <StackPanel Orientation='Horizontal' Spacing='1'>
                        <RadioButtonComponent Name='rb3' HorizontalAlignment='Left' VerticalAlignment='Center' Focusable='False' />
                        <Text Text='Option #3' Font='Arial, 8pt' />
                  </StackPanel>
            </StackPanel>
      </Border>"));

RadioButtonGroup group = new RadioButtonGroup();
(n.FindComponent("rb1") as RadioButtonComponent).Group = group;
(n.FindComponent("rb2") as RadioButtonComponent).Group = group;
(n.FindComponent("rb3") as RadioButtonComponent).Group = group;

n.Bounds = new RectangleF(10, 10, 50, 50);

diagram1.Nodes.Add(n); 


And here is an image illustrating the end result:



Regards,
Meppy
  
Back to top
 
IP Logged
 
jgarrett
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 5
Joined: Oct 19th, 2011
Re: radio buttons
Reply #2 - Oct 19th, 2011 at 12:16pm
Print Post  
Hi Meppy,

That works great, thanks! Is there any way to define the radio groups in the XML template, or should I do that from my SurveyNode constructor (it's a custom class that derives CompositeNode)?

Thanks again,
- Jacob
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: radio buttons
Reply #3 - Oct 19th, 2011 at 2:03pm
Print Post  
You can extend the RadioButtonComponent implementation to support this. Add the following members to the RadioButtonGroup class:

Code
Select All
...
	private static Dictionary<string, RadioButtonGroup> groups;

	static RadioButtonGroup()
	{
		groups = new Dictionary<string, RadioButtonGroup>();
	}

	public static RadioButtonGroup Get(string groupId)
	{
		RadioButtonGroup group = null;
		if (!groups.TryGetValue(groupId, out group))
		{
			group = new RadioButtonGroup();
			groups.Add(groupId, group);
		}
		return group;
	}
... 


Then add a new string property to the RadioButtonComponent class which allows setting a radio button group through a unique string identifier:

Code
Select All
...
	public string GroupId
	{
		get { return ""; }
		set { Group = RadioButtonGroup.Get(value); }
	}
... 


Now you can assign groups to the radio buttons defined in XML by setting the GroupId property to any string value. Radio buttons with the same GroupId will be associated with the same group:

Code
Select All
...
<RadioButtonComponent Name='rb1' ... GroupId='group1' />
...
<RadioButtonComponent Name='rb2' ... GroupId='group1' />
...
<RadioButtonComponent Name='rb3' ... GroupId='group1' /> 


Regards,
Meppy
  
Back to top
 
IP Logged
 
jgarrett
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 5
Joined: Oct 19th, 2011
Re: radio buttons
Reply #4 - Oct 19th, 2011 at 2:52pm
Print Post  
Thanks Meppy, that was very kind of you!

- Jacob
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint