在GUI C#中显示AVL树数据

时间:2015-04-07 18:19:36

标签: c# user-interface avl-tree

基本上我有一个存储Country类实例的AVL树。当我对树进行顺序遍历时,我能够正确地看到国家/地区详细信息,但是我希望在GUI中查看和修改国家/地区类的实例。我遇到的问题是我不知道如何访问类数据并将其显示在列表框之类的内容中。这是我的国家班:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace International_Trading_Data
{
    class Country : IComparable
    {
        public string countryName { get; set; }
        public double gdp { get; set; }
        public double inflation { get; set; }
        public double tradeBalance { get; set; }
        public int hdiRanking { get; set; }
        public LinkedList<string> tradePartners { get; set; }
        public string f;

        public Country (){
    }
        public Country(string cname, double g, double i, double t, int h, LinkedList<string> tp)
        {
            this.countryName = cname;
            this.gdp = g;
            this.inflation = i;
            this.tradeBalance = t;
            this.hdiRanking = h;
            this.tradePartners = tp;
        }

        public int CompareTo(object obj)
        {
            Country temp = (Country)obj;
            return countryName.CompareTo(temp.countryName);
        }

        public override string ToString()
        {
            foreach (string i in tradePartners)
                f += i+",";
            return countryName+" "+gdp+" "+" "+inflation+" "+tradeBalance+" "+ hdiRanking+ " "+f;

        }
    }
}
`

这是我创建国家/地区类实例的地方:

 public void loadFile()
    {
        OpenFileDialog open = new OpenFileDialog();
        open.Filter = "CSV Files (*.csv)|*.csv";
        open.FilterIndex = 1;
        open.Multiselect = true;
        if (open.ShowDialog() == DialogResult.OK)
        {
            string selectedFilePath = open.FileName;
            const int MAX_SIZE = 5000;
            string[] allLines = new string[MAX_SIZE];

            allLines = File.ReadAllLines(selectedFilePath);
            foreach (string line in allLines)
            {
                if (line.StartsWith("Country"))
                {
                    headers = line.Split(',');
                }
                else
                {
                    string[] columns = line.Split(',');

                    LinkedList<string> tradePartners = new LinkedList<string>();
                    string[] partners = columns[5].Split('[', ']', ';');
                    foreach (string i in partners)
                    {
                        if (i != "")
                        {
                            tradePartners.AddLast(i);

                        }
                    }

                   countries.InsertItem(new Country(columns[0], Double.Parse(columns[1]),Double.Parse(columns[2]), Double.Parse(columns[3]) ,int.Parse(columns[4]),tradePartners));
                }

            }

以下是我的inorder遍历的代码:

 public void InOrder()
    {
        inOrder(root);

    }

    private void inOrder(Node<T> tree)
    {
        if (tree != null)
        {
            inOrder(tree.Left);

            System.Diagnostics.Debug.WriteLine(tree.Data.ToString());

            inOrder(tree.Right);

        }

此代码为少数测试国家/地区生成以下输出:

阿根廷3 22.7 0.6 45巴西,智利,

澳大利亚3.3 2.2 -5 2中国,日本,新西兰,

巴西3 5.2 -2.2 84智利,阿根廷,美国,

所以我知道我的类已经在avl树中正确存储了。

1 个答案:

答案 0 :(得分:0)

我不确定您作为countries集合的数据结构使用了什么,但假设它现在是一个List,您可以执行以下操作(注意:此示例仅用于演示显示有关的信息)用于操纵的UI):

    public Form1()
    {
        InitializeComponent();

        List<Country> countries = new List<Country>() { 
            new Country() { countryName = "Mattopia" , gdp = 1500, inflation = 1.5, f="hi"}, 
            new Country { countryName = "coffeebandit", gdp = 2000, inflation = 1.2, f="hey" }};
        listBox1.DisplayMember = "countryName";
        listBox1.DataSource = countries;
    }

    public class Country
    {
        public string countryName { get; set; }
        public double gdp { get; set; }
        public double inflation { get; set; }
        public double tradeBalance { get; set; }
        public int hdiRanking { get; set; }
        public LinkedList<string> tradePartners { get; set; }
        public string f;
    }

然后,您可以使用所选索引更改事件来填充您的字段:

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Country country = (Country)listBox1.SelectedValue;
        //fill up all other GUI controls
        textBox1.Text = country.f;
        textBox2.Text = country.inflation.ToString();
    }

如果您想处理文字更改:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        Country country = (Country)listBox1.SelectedValue;
        if (country != null)
        {
            country.f = textBox1.Text;
        }
    }

这将显示以下内容:

enter image description here

enter image description here

这应该展示如何在WinForms UI中编辑类的基础知识 对于更高级的示例,我建议使用其他事件来根据您的需求变化来捕获信息。