Anteara
Anteara

Reputation: 759

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:

0100000001000000

It states that the mesh version = 1 and vertex version = 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. 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. 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.

Note: I've tried Levelreader.Form1.treeView1, but it doesn't exist).

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(). I then call HeaderData, and within that class I get the mesh and vertex version.

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. However, if you have suggestions, please state them, as I'm new to c#!

So in summary, here are my two problems:
1) How can I access my treeview from another namespace/class?
2) Do you have any suggestions to improve my code? (and the way I plan to compile my levels?)

Thanks!

P.S. 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.
I've tried to select it with 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) https://i.sstatic.net/Q8bGg.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.

Upvotes: 1

Views: 1976

Answers (1)

Nasreddine
Nasreddine

Reputation: 37888

Three Solutions :

  1. You could change the visibility of the TreeView in Form1.Designer.cs to public or internal

  2. Or you could build the TreeView Nodes inside the HeaderData() function and return them.

  3. Or as @Saeid87 said, you could pass the TreeView by reference the HeaderData() function:

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)

temp.HeaderData(hex, ref treeView1)

Upvotes: 1

Related Questions