Bound ListBox Values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SenileOwl
    New Member
    • Feb 2008
    • 27

    #16
    I've never actually programmed in C#, I received that code from another person, altered it as best I could to VB and ran with it. So I'm not sure I'm quite the person to ask. I will try and answer your questions, but I have also just barely started programming 3 months ago, and I'm still mostly confused by the whole thing. I would recommend creating a new post in the .NET forum. There are a lot of people who program in C# there.


    First Question. the infinity error. The listbox that I used this program for was bound to a table. Therefore, most the of the normal ways of looking at my listbox did not work, because instead of my items being just text, they were whole rows in a table. I think your listbox is just text and not bound. As a result, the whole datarowview thing might not work, because your items are not datarowview things, they are just strings. (maybe) Earlier in this Thread we talked about some ways to get values out. Most of the ideas did not work for me, because my listbox was bound. They might work for you, if you can convert them to C#.


    Second Question, I think the column and row thing is really just how a person looks at it. the Rows came, because my listbox was bound to a table, and each item in my listbox came from a different row in my table. The column part, was just what SammyB called his variable, I named mine blahblah. However, looking at the values as part of a column makes a lot of sense.


    Third Question, my guess is that when you are setting the value member, you are not setting it individually for each listbox item. When I set the value member for my listbox, I set it as a certain column in my table. Therefore the program went through and matched up the the values in two different columns in my program. It made one value, the display member that users could see, and made the other value, the hidden value that only I saw. The point was, I only set my valuemember once for my whole listbox, and it applied to the whole thing. If you want to apply the value to only one item, you are going to need to specify that one item somehow.

    These are my best guesses to your questions, but I can't really give you any code to help you follow. Try posting in the .NET forum, they might be able to give you the code you are looking for.




    Originally posted by OliveOyl3471
    Yes...that is what I want to do! Finally...someo ne gets it right. :D

    I tried your suggestion, and changed the name of the listbox to listBox1. The program runs, but gives me an error at

    DataRowView dr = (DataRowView)li stBox1.Items[0];
    It says "when casting from a number, the value must be a number less than infinity"...wha t does that mean?

    I added a try/catch block here and another place. The program runs, but not like it's supposed to. It still has errors, but I don't know how to fix them.

    I have another question. Why would I want a row and
    column if I have a listbox? I'm new to C#, have only had a few
    lessons so I really don't know what I'm doing with it yet.
    (Is the answer simply..."that is how you do it"?)

    To get the value assigned to each list item:
    Might this possibly be correct? It doesn't give me an error, but
    it also doesn't display correctly.
    When I mouse over "ValueMembe r" it says "Gets or sets the
    property to use as the actual value for the items
    in the System.Windows. Forms.ListContr ol."


    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                //populate listbox
                listBox1.Items.Add("Mary"); 
                listBox1.ValueMember = "Ma".ToString();
                listBox1.Items.Add("Joe");
                listBox1.ValueMember = "J".ToString();
                listBox1.Items.Add("Mom");
                listBox1.ValueMember = "M".ToString();
                listBox1.Items.Add("Dad");
                listBox1.ValueMember = "D".ToString();
                listBox1.Items.Add("Sandy");
                listBox1.ValueMember = "S".ToString();
                
            }
    I think this is what I want...right?

    Comment

    • OliveOyl3471
      New Member
      • Jul 2007
      • 8

      #17
      Thanks for the advice. I think you are right.

      I've just started using C#. The class is supposed to be advanced C++ but the instructor prefers C#, so that is what we're using. I've taken classes in VB, C++, HTML, and have done a little bit of QBasic, a little SQL, and a little JavaScript and DHTML. All these languages do get kind of mixed up when you try to learn them all.

      I think C# is kind of a combination of VB and C++.
      It would be easier if they just had ONE language and stuck with it. According to my instructor, that is the purpose of making the languages so similar. Eventually they want to have just one language.

      Thanks again for trying to help me, and clearing up the row/column question. I'll post the answer when I figure it out.

      :)

      Comment

      • OliveOyl3471
        New Member
        • Jul 2007
        • 8

        #18
        This works, although I am sure it is not the only way to do this.

        Code:
                public class names
                {
                    private string realName;
                    public string nickName;
        
                    public names(string strnickName, string strrealName)
                    {
        
                        this.realName = strrealName;
                        this.nickName = strnickName;
                    }
        
                    public string realName1
                    {
                        get
                        {
                            return realName;
                        }
                    }
        
                    public string nickName1
                    {
        
                        get
                        {
                            return nickName;
                        }
                    }
        
                    public override string ToString()
                    {
                        return this.realName;
                    }
                }
                public void Form1_Load(object sender, EventArgs e)
                {
                    //populate listbox 
        
                    ArrayList names1 = new ArrayList();
                    names1.Add(new names("Mary", "Sis"));
                    names1.Add(new names("Joe", "Bubba"));
                    names1.Add(new names("Lynn", "Mom"));
                    names1.Add(new names("Fred", "Dad"));
                    names1.Add(new names("Sandy", "dog"));
        
                    listBox1.DataSource = names1;
                    listBox1.DisplayMember = "nickName1";
                }
        
                private void button1_Click(object sender, EventArgs e)
                    //display values of  selected items
        
                        foreach (int index in listBox1.SelectedIndices)
                        {
                            MessageBox.Show(listBox1.Items[index].ToString());
                            label1.Text += listBox1.Items[index].ToString() + Environment.NewLine;

        Comment

        Working...