c# - Why is only the first RadioButton being added to the GroupBox? -
i trying dynamically create windows controls , add them panel. button , checkbox has worked fine; i've run problem groupbox, though, radiobuttons inside it.
the first radiobutton element created , added groupbox in expected location, susbequent ones, although ostensibly created (stepping through code makes appear case), not visible.
i think if subsequent radiobuttons being plopped atop previous ones, last 1 1 seen. looks like:
each ~-delimited val should text value of radiobutton, yet 1 displays. need explicitly provide location vals subsequent radiobuttons, or why failing?
here code:
private groupbox getgroupbox(string currentsettings, int curleftval) { // "apple~orange~peach~true (must "enclose group in black box" last val (ignore quick-and-dirty mockup, though)) list<string> grpbxvals = new list<string>(currentsettings.split('~')); groupbox gb = new groupbox(); gb.height = 60; gb.location = new point(curleftval, panel_top_loc); radiobutton radbtn = null; // "-1" because we're ignoring final bool ("enclose in black box") (int = 0; < grpbxvals.count-1; i++) { radbtn = new radiobutton(); radbtn.text = grpbxvals[i]; gb.controls.add(radbtn); } return gb; }
update
the idea in answer below pierre seems sensible, it's still not quite doctor ordered:
update 2
this works pretty (modification of pierre's code):
ilist<string> grpbxvals = new list<string>(currentsettings.split('~')); groupbox gb = new groupbox { height = 60, location = new point(curleftval, 0) }; int radbuttonposition = 0; (int = 0; < grpbxvals.count() - 1; i++) { gb.controls.add(new radiobutton { text = grpbxvals[i], location = new point(curleftval, radbuttonposition) }); radbuttonposition += new radiobutton().height - 4; // "-4" kludge } return gb;
gives me:
if set breakpoint, you'll see groupbox contains radiobuttons. location indeed same, they're displayed 1 above other. problem not adding them groupbox, displaying them all.
to achieve that, increment location on each add operation display them :
private groupbox getgroupbox(string currentsettings, int curleftval) { // "apple~orange~peach~true (must "enclose group in black box" last val (ignore quick-and-dirty mockup, though)) list<string> grpbxvals = new list<string>(currentsettings.split('~')); groupbox gb = new groupbox(); gb.height = 60; gb.location = new point(curleftval, panel_top_loc); radiobutton radbtn = null; // "-1" because we're ignoring final bool ("enclose in black box") int radbuttonposition = panel_top_loc; (int = 0; < grpbxvals.count - 1; i++) { radbtn = new radiobutton(); radbtn.text = grpbxvals[i]; radbtn.location = new point(curleftval, radbuttonposition ); radbuttonposition += radbtn.height; gb.controls.add(radbtn); } return gb; }
i'm defining variable called radbuttonposition
, initializing groupbox's position. i'm setting radiobutton location according , incrementing radbuttonposition
height of radiobutton each time 1 added.
here's little refactored version :
private groupbox creategroupboxwithradiobuttons(string currentsettings, int curleftval) { ilist<string> grpbxvals = new list<string>(currentsettings.split('~')); groupbox gb = new groupbox { height = 60, location = new point(curleftval, panel_top_loc) }; int radbuttonposition = panel_top_loc; (int = 0; < grpbxvals.count() - 1; i++) { gb.controls.add(new radiobutton {text = grpbxvals[i], location = new point(curleftval, radbuttonposition)}); radbuttonposition += new radiobutton().height; } return gb; }
there's of course lot of method extraction respect srp, that's start.
Comments
Post a Comment