Results 1 to 8 of 8

Thread: [RESOLVED] Databinding radiobutton set

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Resolved [RESOLVED] Databinding radiobutton set

    Hi,

    I'm trying to create a databinding to a couple of radiobuttons.
    The dataobject contains a integer representation of the radiobutton to be set.
    So after doing some searching I found the suggestion to make a groupbox/panel, so I made one:
    Code:
    	public class RadioPanel : Panel, INotifyPropertyChanged
    	{
    		public event PropertyChangedEventHandler PropertyChanged;
    
    		private int _selectedIndex = -1;
    		private bool _cancelEvents = false;
    
    		public int SelectedIndex
    		{
    			get	{ return this._selectedIndex; }
    			set
    			{
    				this._cancelEvents = true;
    
    				foreach (Control control in this.Controls)
    				{
    					if (control is RadioButton && control.Tag != null)
    						if ((string)control.Tag == value.ToString())
    							((RadioButton)control).Checked = true;
    						else
    							((RadioButton)control).Checked = false;
    				}
    
    				this._selectedIndex = value;
    
    				this._cancelEvents = false;
    			}
    		}
    
    		protected override void OnControlAdded(ControlEventArgs e)
    		{
    			base.OnControlAdded(e);
    			if (e.Control is RadioButton)
    				((RadioButton)e.Control).CheckedChanged += new EventHandler(CheckedChanged);
    
    		}
    
    		private void CheckedChanged(object sender, EventArgs e)
    		{
    			if (!this._cancelEvents && ((RadioButton)sender).Checked == true)
    			{
    				this.SelectedIndex = Convert.ToInt32(((RadioButton)sender).Tag);
    				if (PropertyChanged != null)
    					PropertyChanged(this, new PropertyChangedEventArgs("SelectedIndex"));
    			}
    		}
    	}
    The radiobuttons in the panel are given a tag representing their 'index'.
    Upstream works great. I modify the value in the dataobject and it is represented by the according radiobutton.
    But when I check a radiobutton it doesn't update the dataobject.

    The databinding I'm using is the following:
    Code:
    this.optPriority.DataBindings.Add("SelectedIndex", this._myDataObject, "Priority", false, DataSourceUpdateMode.OnPropertyChanged);
    It's driving me mad, because it does fire the PropertyChanged event when I debug it.

    If anyone could shed some light on this, that would be awesome.
    Thanks.
    Delete it. They just clutter threads anyway.

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Databinding radiobutton set

    Is optPriority an actual radio button or is it the radio button group ? I think your issue might comme from the databinding. Here's a possible solution on StackOverflow. I think it might be worth the look. He's pretty much doing what you are trying to achieve, but it looks to me like he is binding on each radiobutton, see the bottom of the answer.

    Anyways, hope this helps.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Databinding radiobutton set

    Thanks for the reply.
    optPriority is the button group.
    That link is where I got the idea from, but it uses LINQ.
    And it mentions
    Code:
     radioGroup1.DataBindings.Add("Selected", Properties.Settings.Default, "selected1");
    So that's not it either.
    Delete it. They just clutter threads anyway.

  4. #4

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Databinding radiobutton set

    I haven't worked with databindings a lot, so I might be totally off, but this looks like pretty odd behavior to me:
    - Breakpoint at PropertyChanged firing
    - Breakpoint on setter and getter in data object
    I'd expect the PropertyChanged in the control to invoke the setter, but it seems to call the getter after firing PropertyChanged.

    I'm really confused..
    Delete it. They just clutter threads anyway.

  5. #5
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Databinding radiobutton set

    Yeah I don't have much experience with event handlers either.
    I guess you already checked but:
    Is your setter actually called by your CheckedChanged event handler ? and when the getter is called after firing PropertyChanged, is the returned value the good one, is it actually the value of the selected radio button ?
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  6. #6

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Databinding radiobutton set

    Which setter are you referring to? The one in the dataobject or the control?
    After the PropertyChanged event has fired, the getter of the control returns the correct new value.
    What I'm expecting because of the binding, is the getter of the dataobject also to return the new value. But it doesn't.
    Delete it. They just clutter threads anyway.

  7. #7

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Databinding radiobutton set

    Got the little bugger.
    I was building a sample project with the control to post here, but it turned out to work fine there.
    So I started looking more carefully what on earth could be the problem.

    Seems that I was using an enum for the dataobject property.
    As stated in one of the stackoverflow answers, the built-in databinding is very weakly typed.
    A little exception from it wouldn't have been inappropriate here.

    Thanks anyway
    Delete it. They just clutter threads anyway.

  8. #8
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: [RESOLVED] Databinding radiobutton set

    hehe that explains why it was so hard to find the bug
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width