|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Data binding an array to a listboxof objects. I think using data binding is supposed to be the easiest way to do this. I've never used data binding before and am having trouble getting it to do anything. The relevant code is below, followed by a better explanation of what I'm trying to do. // pseudocode start public delegate string StepMethod(); public class SubStep { public SubStep(string stepName, StepMethod stepMethod) { this.method = stepMethod; this.stepName = stepName; } public string StepName { get { return StepName; } } private readonly string stepName; public StepMethod Method { get { return Method; } } private readonly StepMethod method; } public sealed class Test1 : IMyInterface { public Test1() { } private static readonly SubStep[] subSteps = new SubStep[] { new SubStep("Substep 1", new StepMethod(SubStep1)), new SubStep("Substep 2", new StepMethod(SubStep2)), new SubStep("Substep 3", new StepMethod(SubStep3)), etc... }; private static string SubStep1() {...} private static string SubStep2() {...} private static string SubStep3() {...} #region IAcceptanceTest Members // property is part of IMyInterface implementation public SubStep[] SubSteps { get { return subSteps; } } } // end pseudocode I have a form with a listBox. What I'd like to do is bind the data in the subSteps static array (using the non-static SubSteps property to access it) to the listBox. I want line N in the list box to be subSteps[N].StepName and want listBox.SelectedItem to return subSteps[N].Method when line N in the list box is selected. I've created a BindingSource called test1BindingSource and set its DataSource property to Test1 and the DataMember property to SubSteps. In my listBox, I set the DataSource property to test1BindingSource and the DisplayMember property to StepName (using the visual designer) and the ValueMember property to Method. It also gives me the option of setting SelectedValue to either StepName or Method, or nothing. Not sure what do do with this. When I build this and run, the listbox shows up with nothing in it. What am I doing wrong (lots I'm sure)? And do I need to fool with the Advanced data binding properties? I also noticed that if I change the SubSteps property in class Test1 to return Test1.subSteps instead of just subSteps, the visual designer doesn't seem to understand it and won't show the StepName or Method members for setting the DisplayMember and ValueMember properties. A. Spiehler wrote:
Show quoteHide quote > I'm trying to fill a listBox control with string members from an array Seems to me that you could follow the tutorial examples in MSDN. There is a> of objects. I think using data binding is supposed to be the easiest > way to do this. I've never used data binding before and am having > trouble getting it to do anything. The relevant code is below, followed > by a better explanation of what I'm trying to do. > > // pseudocode start > > public delegate string StepMethod(); > > public class SubStep > { > public SubStep(string stepName, StepMethod stepMethod) > { > this.method = stepMethod; > this.stepName = stepName; > } > > public string StepName > { get { return StepName; } } > > private readonly string stepName; > > public StepMethod Method > { get { return Method; } } > > private readonly StepMethod method; > } > > public sealed class Test1 : IMyInterface > { > public Test1() > { } > > private static readonly SubStep[] subSteps = new SubStep[] > { > new SubStep("Substep 1", new StepMethod(SubStep1)), > new SubStep("Substep 2", new StepMethod(SubStep2)), > new SubStep("Substep 3", new StepMethod(SubStep3)), > etc... > }; > > private static string SubStep1() > {...} > > private static string SubStep2() > {...} > > private static string SubStep3() > {...} > > #region IAcceptanceTest Members > > // property is part of IMyInterface implementation > public SubStep[] SubSteps > { > get { return subSteps; } > } > } > > // end pseudocode > > I have a form with a listBox. What I'd like to do is bind the data in > the subSteps static array (using the non-static SubSteps property to > access it) to the listBox. I want line N in the list box to be > subSteps[N].StepName and want listBox.SelectedItem to return > subSteps[N].Method when line N in the list box is selected. > > I've created a BindingSource called test1BindingSource and set its > DataSource property to Test1 and the DataMember property to SubSteps. > In my listBox, I set the DataSource property to test1BindingSource and > the DisplayMember property to StepName (using the visual designer) and > the ValueMember property to Method. It also gives me the option of > setting SelectedValue to either StepName or Method, or nothing. Not > sure what do do with this. When I build this and run, the listbox shows > up with nothing in it. What am I doing wrong (lots I'm sure)? And do I > need to fool with the Advanced data binding properties? > > I also noticed that if I change the SubSteps property in class Test1 to > return Test1.subSteps instead of just subSteps, the visual designer > doesn't seem to understand it and won't show the StepName or Method > members for setting the DisplayMember and ValueMember properties. very simple example of listbox filling from arrays. Perhaps if you need additional help, please post. Good luck
Other interesting topics
Property accessors and Optimization
How to measure performance by milliseconds? static controls Return month from dropdown as integer Some construction that I don't understand why it works as it does. Adding auditory feedback to button clicks Interesting flickering problem if application's user control is invalidated by external application "The page cannot be displayed" when running web service Need help with KeyDown REGEX vs Stack Tokenizer? |
|||||||||||||||||||||||