Home All Groups Group Topic Archive Search About

Data binding an array to a listbox

Author
26 Apr 2006 7:13 PM
A. Spiehler
I'm trying to fill a listBox control with string members from an array
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.

Author
26 Apr 2006 9:58 PM
intrader
A. Spiehler wrote:

Show quoteHide quote
> I'm trying to fill a listBox control with string members from an array
> 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.
Seems to me that you could follow the tutorial examples in MSDN. There is a
very simple example of listbox filling from arrays.
Perhaps if you need additional help, please post.
Good luck

Bookmark and Share