c# - Can't expand nodes in TreeView (WinForms) -


I am filling programmatically TreeView (if this is the case in separate thread) .

When I load TreeView on the window, then I want the first level of nodes for the extension. I almost everywhere (in the worker thread, in the main thread, Form handouts, etc.), but TreeView <

Object tree) = & gt; {Tree View. Nodes [0] .Nodes.Add (text);}); Public Static Zero UpdateTree (this control ctrl, action & lt; object & gt; code) {if (ctrl.InvokeRequired) {ctrl.BeginInvoke (code, (TreeView) ctrl); } Other {code.Invoke ((TreeView) ctrl); }}

UPDATE 2

  Private zeros btnFillTree_Click (Object Sender, EventArgs e) {...... treeDirectoryContents .Nodes.Add ("GeneralFolder"); ...... / I create threads () which fills the tree directory contents ...... tree directory content Expand all (); }  

As far as I know (.NET 3.5) you can separate the GUI Can not retrieve elements thread (you can prepare some data but use TreeView.Nodes from the main thread - Use Control.BeginInvoke for this. .. You can also check the control .vokRequired ).

After filling all the nodes you can do this

  foreach (tree node node in tree view) node. Expand ()  

Edit UPDATE2:

  1. Nodes can be extended only when they have children Be there
  2. Controls can only be accessed from the main thread (check Control.InvokeRequired )
  3. Initialize () asynchronous () does not wait while
  4. / Li> done = wrong; t hread.Start (); while (! Done) application.Downs ()
< P> Example:

  // Start the download operation in the background this.backgroundWorker1.RunWorkerAsync (); // Download for the duration of the download Do not disable this.downloadButton.Enabled = false; // Once you start the background thread // you can exit the // handler and the app will wait until the runwalker is not being picked up. // or if you want to do something else on the main thread, like // update the progress bar, you can do this in a loop / when it looks like background // is still going on Or not. While (this.backgroundWorker1.Is Busy) {progressBar1.Increment (1); // Keep the UI messages forward, the form // remains accountable during asynchronous operation. Application.DoEvents (); }  Edit - How should I do this (using threads)   
  Using the system; Using System.Threading; Using System.Windows.Forms; Class MyForm: form {public static zero main () {applications .sat comp page filtering default (wrong); Application Play (New MyForm ()); } TreeView tree = New TreeView () {Doc = Dokstyle. Fill}; MyForm () {Controls.Add (tree); Tree.Nodes.Add ("Loading ..."); } Secure Override Zero OnLoad (EventArgs e) {New Thread (Fill). Start (); Base.OnLoad (E); } Create zero (string text) {If invocure radiation invitation (new action & lt; string & gt; (create it), text); Other trees Nodes [0] .Nodes.Add (text); } {} InvokeRequired call (New Action (this.Finish)); Other {tree node [0]. Text = "nodes"; tree.ExpandAll ();}} Fill zero () { For (int i = 0; i & lt; 10; i ++) {Create ("node #" + i.ToString ()); Thread.Sleep (100);} Finish ();}}  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -