0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Dictionary<Prime_Broker, Margin2> myDictionary = new Dictionary<Prime_Broker, Margin2>();
        public Form1()
        {
            InitializeComponent();

            Prime_Broker pr1 = new Prime_Broker("Deutsche Bank", "z", "india");
            Prime_Broker pr2 = new Prime_Broker("Deutsche Bank1", "z", "india");
            Prime_Broker pr3 = new Prime_Broker("Deutsche Bank2", "z", "india");
            Prime_Broker pr4 = new Prime_Broker("Deutsche Bank3", "z", "india");
            Prime_Broker pr5 = new Prime_Broker("Deutsche Bank4", "z", "india");

            Margin2 mr1 = new Margin2(30, 40, 50);
            Margin2 mr2 = new Margin2(301, 40, 50);
            Margin2 mr3 = new Margin2(302, 40, 50);
            Margin2 mr4 = new Margin2(303, 40, 50);
            Margin2 mr5 = new Margin2(304, 40, 50);





            //myDictionary.Add(new Prime_Broker("Deutsche Bank", "z", "india"), new Margin2(30, 40, 50));
            //myDictionary.Add(new Prime_Broker("Deutsche1 Bank", "z", "india"), new Margin2(301, 40, 50));
            //myDictionary.Add(new Prime_Broker("Deutsche2 Bank", "z", "india"), new Margin2(302, 40, 50));
            //myDictionary.Add(new Prime_Broker("Deutsche3 Bank", "z", "india"), new Margin2(303, 40, 50));

            myDictionary.Add(pr1, mr1);
            myDictionary.Add(pr2, mr2);
            myDictionary.Add(pr3, mr3);
            myDictionary.Add(pr4, mr4);
            myDictionary.Add(pr5, mr5);
        }

        public class Prime_Broker
        {
            public string Name { set; get; }
            public string Symbol { set; get; }
            public string Address { set; get; }

            public Prime_Broker(string name, string symbol, string address)
            {
                this.Name = name;
                this.Symbol = symbol;
                this.Address = address;
            }

        }

        public void Form1_Load(object sender, EventArgs e)
        {
            List<Prime_Broker> lst_broker = new List<Prime_Broker>();

            lst_broker.Add(new Prime_Broker("Deutsche Bank", "z", "india"));
            lst_broker.Add(new Prime_Broker("Deutsche1 Bank", "z", "india"));
            lst_broker.Add(new Prime_Broker("Deutsche2 Bank", "z", "india"));
            lst_broker.Add(new Prime_Broker("Deutsche3 Bank", "z", "india"));


            listBox1.DataSource = lst_broker;
            listBox1.DisplayMember = "Name";
            listBox1.ValueMember = "name";
            //listBox1.Items.AddRange(lst_broker.ToArray());
            dataGridView1.DataSource = lst_broker;

        }

        public class Margin2
        {
            public int Maintainence_margin { set; get; }
            public int Initial_margin { set; get; }
            public int Commission_fees_percentage { set; get; }

            public Margin2(int M_margin, int I_margin, int CF_Percentage)
            {
                this.Maintainence_margin = M_margin;
                this.Initial_margin = I_margin;
                this.Commission_fees_percentage = CF_Percentage;
            }

        }

       public void listBox1_SelectedValueChanged(object sender, EventArgs e)
       {
           Form1 fr = new Form1();


           //List<fr.myDictionary> lst_broker = new List<Prime_Broker>();

        }



    }
}

你好

这是我正在尝试将数据添加到列表框的代码,现在我想将列表框的选定索引的字典数据显示到文本框但无法执行。如何将字典数据绑定到列表框和新的 texbox

像这里如果我选择“deutchBank1”,那么现在文本框将包含(301、40、50)。

4

1 回答 1

1

您只需要将其ListBox.SelectedItem转换为正确的类型。在 VS 2010 C# Express 中测试:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Dictionary<Prime_Broker, Margin2> myDictionary = new Dictionary<Prime_Broker, Margin2>();
        List<Prime_Broker> brokers = new List<Prime_Broker>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          for(Int32 i = 0; i < 5; i++)
          { 
             Prime_Broker br = new Prime_Broker("Broker " + i.ToString(),
                                                "Symbol " + i.ToString(),
                                                "Address " + i.ToString());
             Margin2 mrg = new Margin2( i, i, i);
             myDictionary.Add(br, mrg);
             brokers.Add(br);
          }
          listBox1.DataSource = brokers;
          listBox1.DisplayMember = "Name";
          listBox1.ValueMember = "Name";
        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                Prime_Broker selectedBroker = (Prime_Broker)listBox1.SelectedItem;
                Margin2 margin;
                if (myDictionary.TryGetValue(selectedBroker, out margin))
                {
                    textBox1.Text = String.Format("{0}, {1}, {2}",
                                                  margin.Commission,
                                                  margin.Maint,
                                                  margin.Fees);
                }
            }
        }
    }

    public class Prime_Broker
    {
        public string Name { set; get; }
        public string Symbol { set; get; }
        public string Address { set; get; }

        public Prime_Broker(string name, string symbol, string address)
        {
            this.Name = name;
            this.Symbol = symbol;
            this.Address = address;
        }
    }

    public class Margin2
    {
        public Int32 Maint { set; get; }
        public Int32 Commission { set; get; }
        public Int32 Fees { set; get; }

        public Margin2(Int32 maint, Int32 commission, Int32 fees)
        {
            this.Maint = maint;
            this.Commission = commission;
            this.Fees = fees;
        }
    }
}
于 2013-07-04T13:32:49.210 回答