c# - How to create a checkbox for each possible enum value in Windows forms? -
say have enum identifier {name, id, number} , want provide user message dialog checkboxes each possible identifier value , ok button. on dialog confirmation list<identifier>(empty if none of checkboxes selected). how such simple thing winforms?
you can array of values in enum with:
var valuesarray = enum.getvalues(typeof (identifier)); to display checkboxes:
foreach (var val in valuesarray) { //create checkbox var cb = new checkbox(); cb.name = string.format("cb_{0}", val); cb.text = val; //set properties //add form controls this.controls.add(cb); } to list, checkboxes on form:
var checkedidentifiers = new list<identifier>(); foreach (var val in valuesarray) { //find checkbox var cb = this.controls[string.format("cb_{0}", val)]; if (cb != null && cb.checked) checkedidentifiers.add((identifier)enum.parse(typeof(identifier), val)); } you can additional error checking above, gist of it.
Comments
Post a Comment