Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi in the following code:
C#
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BindingSourceSimpleTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class Color
        {
            public string Name { get; set; }
            public int Red { get; set; }
            public int Green { get; set; }
            public int Blue { get; set; }
        }

        public class Obj
        {
            public string Name { get; set; }
            public string Color { get; set; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var colors = new List<Color>();
            colors.Add(new Color { Name = "Black", Red = 0, Green = 0, Blue = 0 });
            colors.Add(new Color { Name = "Blue", Red = 0, Green = 0, Blue = 255 });
            colors.Add(new Color { Name = "Red", Red = 255, Green = 0, Blue = 0 });
            colors.Add(new Color { Name = "Magenta", Red = 255, Green = 0, Blue = 255 });
            colors.Add(new Color { Name = "Green", Red = 0, Green = 255, Blue = 0 });
            colors.Add(new Color { Name = "Cyan", Red = 0, Green = 255, Blue = 255 });
            colors.Add(new Color { Name = "Yellow", Red = 255, Green = 255, Blue = 0 });
            colors.Add(new Color { Name = "White", Red = 255, Green = 255, Blue = 255 });

            var objs = new List<Obj>();
            objs.Add(new Obj { Name = "Sun", Color = "Yellow" });
            objs.Add(new Obj { Name = "Grass", Color = "Green" });
            objs.Add(new Obj { Name = "Blood", Color = "Red" });
            objs.Add(new Obj { Name = "Sky", Color = "Blue" });
            objs.Add(new Obj { Name = "Hair", Color = "Black" });
            objs.Add(new Obj { Name = "Snow", Color = "White" });
            objs.Add(new Obj { Name = "Rose", Color = "Red" });

            listBoxObjs.DataSource = new BindingSource(objs, null);
            listBoxColors.DataSource = new BindingSource(colors, null);
            comboBoxObjColor.DataSource = new BindingSource(colors, null);

            textBoxObjName.DataBindings.Add("Text", listBoxObjs.DataSource, "Name");
            comboBoxObjColor.DataBindings.Add("SelectedItem", listBoxObjs.DataSource, "Color");
            textBoxColorName.DataBindings.Add("Text", listBoxColors.DataSource, "Name");
            textBoxColorRed.DataBindings.Add("Text", listBoxColors.DataSource, "Red");
            textBoxColorGreen.DataBindings.Add("Text", listBoxColors.DataSource, "Green");
            textBoxColorBlue.DataBindings.Add("Text", listBoxColors.DataSource, "Blue");

            listBoxObjs.DisplayMember = listBoxColors.DisplayMember = comboBoxObjColor.DisplayMember = "Name";
        }
    }
}

and the following form UI:

Form1[^]

1. i see that the sun is black! why?!

after a deeper look i found that the items in the list of colors are of type Color while the Color property of an object is of type string.

2. how can i make the Color property of an object be shown correctly in the combobox?
3. if i fix this, changing the combobox selection updates this property of an object correctly too?
4. if i fix this, does sun gets yellow? :)

thx
Posted

1 solution

You should work with data, never with strings. A string is only needed with you show something in the UI. In particular, here is a hint about list boxes: you can insert an object of any type in the list, but what is shown as the text of an item? The answer is simple: whatever the method ToStrings() returns. Conclusions: properly overridden System.Object.ToString() controls how the object is shown in the list box. As this is the root type for all object, it can be done to any object, event to a value-type object, it its boxed form.

Finally, you don't have to define those named colors; you can use ready-to use System.Drawing.Color static objects which give name to some colors:
http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx[^].

The wrong color of the sun and other artifacts of poor programming are easily cured by using a debugger.

—SA
 
Share this answer
 
Comments
ilostmyid2 25-Jun-12 4:03am    
thanx for the solution
u know, i include name of a color in an obj because it's enough. there's no need to include all properties of a color also in obj. what can i do if i want to do so?
Sergey Alexandrovich Kryukov 25-Jun-12 11:48am    
You are welcome. I see no reason for not using System.Drawing.Color. If you include only what you need, what would it be and what would you gain? Actually, System.Drawing.Color instance generally carries only the color, ARGB values, not name, but you also include name. Static values do not add to your instance memory, besides, repeated strings go to the intern pool of strings, are not repeated. That's why the strings are immutable. This type is maybe designed better than you though.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900