logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

C# treeView. Pobranie pola nadrzędnego węzła. Przypisanie referencji.

tzok 06 Sty 2006 20:17 5808 2
REKLAMA
  • #1 2153553
    tzok
    VIP Zasłużony dla elektroda
    Posty: 38784
    Pomógł: 3174
    Ocena: 6527
    1) Jak pobrać np. pole Text nadrzędnego węzła treeView?
    2) Czy do poszczególnych elementów drzewa można w prosty sposób przypisywać referencje do obiektów?

    node = treeView1.Nodes.Add("a0");
    node.Nodes.Add("b1");
    node.Nodes.Add("b2");
    node = treeView1.Nodes.Add("a1");
    node.Nodes.Add("b1");
    node.Nodes.Add("b2");

    to da coś takiego:
    a0 --.
     |   |-b1
     |   |-b2
     |
    a1 --.
         |-b1
         |-b2


    ale jeśli kliknę na a1/b2 i w zdarzeniu wpiszę coś takiego:
    private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    		{
    			textBox1.Text=e.Node.Parent.Text;
    		}

    to dostane błąd:
    Cytat:
    An unhandled exception of type 'System.NullReferenceException' occurred in TestDrawing2.exe

    Additional information: Object reference not set to an instance of an object.


    Co robię nie tak?
  • REKLAMA
  • Pomocny post
    #2 2153955
    elektryk
    Poziom 42  
    Posty: 11029
    Pomógł: 439
    Ocena: 241
    Sprawdziłem pod delphi że windows API umożliwia taką operacje jaką chcesz wykonać. A nie jest to przypadkiem tak że najpierw jest zgłaszany komunikat że chcesz "odznaczyć" stary węzeł i przychodzi on z parametrem jednego najwyższych elementów drzewa (który oczywiście nie ma parenta)? Sprawdź jeszcze jakiekolwiek pole "parenta" może jest taki haczyk że jest kopiowany tylko jeden element do struktury przekazywanej jako argument (w celu bezpieczeństwa????) a nie całe drzewo.

    Co do przechowywania dodatkowych danych to w msdn jest przykład jak to zrobić poprzez dziedziczenia (nowa klasa która dziedziczy po treenode wszystko i dokłada nowe pole dla własnego zastosowania).
  • #3 2153973
    tzok
    VIP Zasłużony dla elektroda
    Posty: 38784
    Pomógł: 3174
    Ocena: 6527
    Popełniałem błąd przy tworzeniu węzłów drzewa, nie można korzystać z konstruktora pobierającego String lecz tego biorącego TreeNode.

    Przykład widziałem... ale prosty to on nie jest, łatwiej jest wykorzystać istniejące pole Tag obiektu TreeNode.

    Jak by ktoś na przyszłość potrzebował wiedzieć to załączam kod, który wszystko wyjaśnia:
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.TreeView treeView1;
    		private System.Windows.Forms.Button button1;
    		private System.Windows.Forms.TextBox textBox1;
    		private System.Windows.Forms.TextBox textBox2;
    
    		private System.ComponentModel.Container components = null;
    
    		public Form1()
    		{
    
    			InitializeComponent();
    
    		}
    
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    
    		private void InitializeComponent()
    		{
    			this.treeView1 = new System.Windows.Forms.TreeView();
    			this.button1 = new System.Windows.Forms.Button();
    			this.textBox1 = new System.Windows.Forms.TextBox();
    			this.textBox2 = new System.Windows.Forms.TextBox();
    			this.SuspendLayout();
    			// 
    			// treeView1
    			// 
    			this.treeView1.ImageIndex = -1;
    			this.treeView1.Location = new System.Drawing.Point(8, 8);
    			this.treeView1.Name = "treeView1";
    			this.treeView1.SelectedImageIndex = -1;
    			this.treeView1.Size = new System.Drawing.Size(184, 248);
    			this.treeView1.TabIndex = 0;
    			this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
    			// 
    			// button1
    			// 
    			this.button1.Location = new System.Drawing.Point(208, 120);
    			this.button1.Name = "button1";
    			this.button1.Size = new System.Drawing.Size(72, 24);
    			this.button1.TabIndex = 1;
    			this.button1.Text = "button1";
    			this.button1.Click += new System.EventHandler(this.button1_Click);
    			// 
    			// textBox1
    			// 
    			this.textBox1.Location = new System.Drawing.Point(8, 264);
    			this.textBox1.Name = "textBox1";
    			this.textBox1.Size = new System.Drawing.Size(272, 20);
    			this.textBox1.TabIndex = 2;
    			this.textBox1.Text = "textBox1";
    			// 
    			// textBox2
    			// 
    			this.textBox2.Location = new System.Drawing.Point(8, 288);
    			this.textBox2.Name = "textBox2";
    			this.textBox2.Size = new System.Drawing.Size(272, 20);
    			this.textBox2.TabIndex = 3;
    			this.textBox2.Text = "textBox2";
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(292, 317);
    			this.Controls.Add(this.textBox2);
    			this.Controls.Add(this.textBox1);
    			this.Controls.Add(this.button1);
    			this.Controls.Add(this.treeView1);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		// Create a new ArrayList to hold the Customer objects.
    		private ArrayList customerArray = new ArrayList(); 
    
    		private void FillMyTreeView()
    		{
    			// Add customers to the ArrayList of Customer objects.
    			for(int x=0; x<1000; x++)
    			{
    				customerArray.Add(new Customer("Customer" + x.ToString()));
    			}
    
    			// Add orders to each Customer object in the ArrayList.
    			foreach(Customer customer1 in customerArray)
    			{
    				for(int y=0; y<15; y++)
    				{
    					customer1.CustomerOrders.Add(new Order("Order" + y.ToString()));    
    				}
    			}
    
    			// Display a wait cursor while the TreeNodes are being created.
    //			Cursor.Current = new Cursor("MyWait.cur");
            
    			// Suppress repainting the TreeView until all the objects have been created.
    			treeView1.BeginUpdate();
    
    			// Clear the TreeView each time the method is called.
    			treeView1.Nodes.Clear();
    
    			// Add a root TreeNode for each Customer object in the ArrayList.
    
    			TreeNode tmp;
    			
    			foreach(Customer customer2 in customerArray)
    			{
    				tmp = new TreeNode(customer2.CustomerName);
    				tmp.Tag = customer2;
    				treeView1.Nodes.Add(tmp);
    
    				// Add a child treenode for each Order object in the current Customer object.
    				foreach(Order order1 in customer2.CustomerOrders)
    				{
    					tmp = new TreeNode(customer2.CustomerName + "." + order1.OrderID);
    					tmp.Tag = order1;
    					treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
    						tmp);
    				}
    			}
    
    			// Reset the cursor to the default for all controls.
    			Cursor.Current = Cursors.Default;
    
    			// Begin repainting the TreeView.
    			treeView1.EndUpdate();
    		}
    
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			FillMyTreeView();
    		}
    
    		private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    		{
    			if (e.Node.Tag is Customer) textBox1.Text = ((Customer)e.Node.Tag).CustomerName;
    			if (e.Node.Tag is Order) textBox1.Text = ((Order)e.Node.Tag).OrderID;
    			if (e.Node.Tag is Order) textBox2.Text = e.Node.Parent.Text;
    		}
    	}
    
    	public class Order
    	{
    		public String OrderID;
    		public Order (String ON)
    		{
    			OrderID = ON;
    		}
    	}
    
    	public class Customer
    	{
    		public String CustomerName;
    		public ArrayList CustomerOrders;
    		public Customer ()
    		{
    			CustomerOrders = new ArrayList();
    		}
    		public Customer (String CN)
    		{
    			CustomerName = CN;
    			CustomerOrders = new ArrayList();
    		}
    	}
REKLAMA