views:

25

answers:

2

I created a usercontrol with a treeview inside it. The treeview will be populated if I add nodes in the onload handler of the usercontrol. But after that(for example, I click a button in its parent form), the treeview will not refresh. I can see the nodes was updated in memory, but it just cannot display on the screen. I called refresh/update after adding nodes. Any suggestion is appreciated.

A: 

I put a quick test together based on your description and it seems to paint just fine.

UserControl1

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class UserControl1
    Inherits System.Windows.Forms.UserControl

    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
    Me.TreeView1 = New System.Windows.Forms.TreeView
    Me.SuspendLayout()
    '
    'TreeView1
    '
    Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.TreeView1.Location = New System.Drawing.Point(0, 0)
    Me.TreeView1.Name = "TreeView1"
    Me.TreeView1.Size = New System.Drawing.Size(150, 150)
    Me.TreeView1.TabIndex = 0
    '
    'UserControl1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.Controls.Add(Me.TreeView1)
    Me.Name = "UserControl1"
    Me.ResumeLayout(False)

  End Sub
  Friend WithEvents TreeView1 As System.Windows.Forms.TreeView

End Class

Public Class UserControl1

  Public Sub AddNewNode(ByVal text As System.String)
    TreeView1.Nodes.Add(text)
  End Sub

End Class

Put the usercontrol on a form with a button

Public Class Form1

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        UserControl11.AddNewNode(Now.ToString)
      End Sub
    End Class

If you are seeing proper painting as well then look at any graphics handling in the parent form then the usercontrol then the controls within the usercontrol. We really need more info.

DaveWilliamson
Thank you for your help. What I did is in the parent form there is a menuitem used to load a file. The treeview is built basing on the file. Whenever a new file is loaded, the file path property of the usercontrol will be updated. Then the treeview will be recreated. ---------------------------------- mTreeView.Nodes.Clear(); mTreeView.BeginUpdate(); // add node one by one according to file content mTreeView.EndUPdate(); mTreeView.Refresh. --------------------------------
leon
A: 

Thank you, Dave. I figured it out. I put the usercontrol twice to my form by mistake(I cannot remember how I did it). And the one I operate is underneath the other one. That's why I cannot see it. Sorry for wasting your time.

leon
you didn't waste time. many a time just talking through the issue helps solve it.
DaveWilliamson