【问题标题】:Diplaying AVL Tree data in a GUI C#在 GUI C# 中显示 AVL 树数据
【发布时间】:2015-04-07 18:19:35
【问题描述】:

基本上我有一个存储 Country 类实例的 AVL 树。当我对树进行中序遍历时,我能够正确查看国家/地区详细信息,但是我希望在 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));
                }

            }

这是我的中序遍历的代码:

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

【问题讨论】:

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

标签: c# user-interface avl-tree


【解决方案1】:

我不确定您使用什么作为 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;
    }

然后,您可以使用 selected index changed 事件来填充您的字段:

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

这将为您提供以下显示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多