简体   繁体   English

从另一个类(或更好的东西)填充TreeView C#

[英]Populate TreeView from another class (or something better?) C#

I'm trying to make a level reader application for one of my favourite games, and simply put this is what I'm trying to achieve currently: 我正在尝试为我最喜欢的游戏之一制作一个关卡阅读器应用程序,简单地说,这就是我目前正在努力实现的目标:

First 8 bytes of level file: 级别文件的前8个字节:

0100000001000000

It states that the mesh version = 1 and vertex version = 1. 它指出网格版本= 1,顶点版本= 1。

I've made a GUI in which I have a treeview, and in the tree it has header, and in header it has vertex and mesh version. 我制作了一个GUI,其中有一个树视图,在树中有标头,在标头中有顶点和网格版本。 I would like to populate these with this data. 我想用这些数据填充这些。

Now this is where my first problem arises: 现在这是我的第一个问题出现的地方:
I want to have all my reading done in its own class (for example, HeaderData, MaterialData), etc, just to make the code cleaner. 我想将所有阅读工作都放在自己的类中(例如HeaderData,MaterialData),以使代码更简洁。 Because I'm doing this in another class, 因为我在另一堂课上
I have NO idea how I can access my treeview from said class (if I were in the Form class I could just do treeView1.whatever, but I don't know how to access it from another class. 我不知道如何从所说的类访问我的树视图(如果我在Form类中,我只能做treeView1.what,但是我不知道如何从另一个类访问它。

Note: I've tried Levelreader.Form1.treeView1, but it doesn't exist). 注意:我已经尝试过Levelreader.Form1.treeView1,但是它不存在。

public void button1_Click(object sender, EventArgs e)
{
    using (OpenFileDialog fileDialog = new OpenFileDialog())
    {
        if (fileDialog.ShowDialog() != DialogResult.Cancel)
        {
            textBox1.Text = fileDialog.FileName;
            using (BinaryReader fileBytes = new BinaryReader(new MemoryStream(File.ReadAllBytes(textBox1.Text))))
            {
                //Get the hex data in bytearray format
                //This won't be displayed
                int length = (int)fileBytes.BaseStream.Length;
                byte[] hex = fileBytes.ReadBytes(length);
                //File.WriteAllBytes(@"c:\temp_file.txt", hex);

                //This is what's displayed.
                //Remember to make changes to the byte array
                //and then update the view.
                string tempStr = BitConverter.ToString(hex).Replace("-", " ");
                richTextBox1.Text = tempStr;
                richTextBox1.ScrollBars = RichTextBoxScrollBars.ForcedVertical;

                //Instantiate the class
                Header temp = new Header();
                temp.HeaderData(hex);
                }
            }
        }

    }

This is the method, within the class Form1, in namespace LevelReader, that reads the file and then instantiates the class Header(). 这是名称空间LevelReader中类Form1中的方法,该方法读取文件,然后实例化Header()类。 I then call HeaderData, and within that class I get the mesh and vertex version. 然后,我调用HeaderData,并在该类中获取网格和顶点版本。

namespace SceneStuff
{
    public class Header
    {

        public void HeaderData(byte[] hex)
        {
            //First 4 Bytes = Mesh Version
            //Second 4 Bytes = Vertex Version
            byte[] meshVersion = hex.Take(4).ToArray();
            byte[] vertexVersion = hex.Skip(4).Take(4).ToArray();

        }

    }
}

in THIS method is where I want to use this data to populate my treeview. 我想在此方法中使用此数据填充树形视图。 And say for example I changed some information in the level, such as changing the mesh version to 2, I want, when I press 'Compile Level' (in my apps GUI) to read from the TreeView, as I believe it would be best that way. 并说,例如,我更改了级别中的一些信息,例如将网格物体版本更改为2,我想当我按“编译级别”(在我的应用程序GUI中)从TreeView中读取时,因为我认为这是最好的那样。 However, if you have suggestions, please state them, as I'm new to c#! 但是,如果您有建议,请说明一下,因为我是C#的新手!

So in summary, here are my two problems: 总而言之,这是我的两个问题:
1) How can I access my treeview from another namespace/class? 1)如何从另一个名称空间/类访问我的树形视图?
2) Do you have any suggestions to improve my code? 2)您对我的代码有什么建议吗? (and the way I plan to compile my levels?) (以及我打算编译关卡的方式?)

Thanks! 谢谢!

PS I apologize for the wall of text! 附言:我对文字墙表示歉意! >_< > _ <

Edit: 编辑:

I have another problem which is that I can't seem to populate my node. 我还有另一个问题,就是我似乎无法填充我的节点。

It is created like this: 它是这样创建的:

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Mesh Version");

and below: 及以下:

        treeNode2.Name = "meshVersion";
        treeNode2.Text = "Mesh Version";

I've tried adding treeNode2 as a reference to my method, but that doesn't work. 我尝试添加treeNode2作为对我的方法的引用,但这不起作用。
I've tried to select it with treeView1.SelectedNode = treeView1.Nodes[1].Nodes[1]; 我试图用treeView1.SelectedNode = treeView1.Nodes[1].Nodes[1];选择它 but that doesn't seem to work either. 但这似乎也不起作用。
(here is an image of the GUI, which shows that Nodes[1].Nodes[1] is, I think, what I'm trying to access (I'm trying to access Mesh Version) http://i.imgur.com/hahhG.png (这是GUI的图像,它显示Nodes[1].Nodes[1]是我要访问的内容(我要访问Mesh版本的内容) http://i.imgur .com / hahhG.png


So what I need to do is to access that PRE EXISTING NODE and add a child to it. 因此,我需要做的是访问该既有节点并将其添加一个子节点。 I just can't seem to find any tutorials that detail it in these circumstances and I don't understand it will enough to cater their tutorials to suit my needs. 在这些情况下,我似乎找不到任何详细介绍它的教程,并且我不了解它是否足以满足他们的教程来满足我的需求。

Thanks so much. 非常感谢。

Three Solutions : 三种解决方案:

  1. You could change the visibility of the TreeView in Form1.Designer.cs to public or internal 您可以将Form1.Designer.cs TreeView的可见性更改为publicinternal

  2. Or you could build the TreeView Nodes inside the HeaderData() function and return them. 或者,您可以在HeaderData()函数内部构建TreeView节点并返回它们。

  3. Or as @ Saeid87 said, you could pass the TreeView by reference the HeaderData() function: 或正如@ Saeid87所说,您可以通过引用HeaderData()函数来传递TreeView:

Example : 范例:

public void HeaderData(byte[] hex, ref TreeView treeview)
{
    //First 4 Bytes = Mesh Version
    //Second 4 Bytes = Vertex Version
    byte[] meshVersion = hex.Take(4).ToArray();
    byte[] vertexVersion = hex.Skip(4).Take(4).ToArray();

    //Example: Do something with the Mesh Version Node
    treeView1.Nodes[1].Nodes[0].Nodes[0].Text = "Lorem ipsum";
}

and When you call your function pass the TreeView by reference (inside your button1_Click function) 当调用函数时,通过引用传递TreeView(在button1_Click函数内部)

temp.HeaderData(hex, ref treeView1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM