C# 在GUI C中显示AVL树数据

C# 在GUI C中显示AVL树数据,c#,user-interface,avl-tree,C#,User Interface,Avl Tree,基本上我有一个AVL树,它存储Country类的实例。当我按顺序遍历树时,我能够正确地看到国家的详细信息,但是我希望在GUI中查看和修改country类的实例。我遇到的问题是,我不知道如何访问类数据并将其显示在类似列表框的东西中。这是我的国家班: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace

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

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));
                }

            }

这是我创建country类实例的地方:

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));
                }

            }
以下是我的索引遍历代码:

 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树中

我不确定您正在使用什么作为国家/地区收集的数据结构,但假设它现在是一个列表,您可以做以下注意事项:此示例仅用于演示在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 textBox1_TextChanged(object sender, EventArgs e)
    {
        Country country = (Country)listBox1.SelectedValue;
        if (country != null)
        {
            country.f = textBox1.Text;
        }
    }
这将为您提供以下显示:

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

您是在文本编辑器中编写的吗?您需要一个带有窗体或窗口的项目来在屏幕上显示某些内容。这是WPF还是WinForms?我在visual studio中有一个带有表单的项目。表格上有一个列表框。我的问题是我不知道如何从avl树中检索国家名称并将其显示在列表框中。你必须给我们更多。你有构建树的代码吗?你有乡村班的例子吗?您是否试图从树中获取任何信息?我创建了country类的实例,并在读取csv文件中的行时将它们添加到teee中。我已经修改了我的帖子,以显示我从avl树输出的内容。