2010-05-26 2 views
1

내 문제는, 나는 UserControl에서 TreeView를 사용하고있다. 디버깅하는 동안 그 결과는 TreeView에 추가되는 것을 볼 수 있지만, MainControl에 UserControl을 사용할 때 발생하지는 않습니다. TreeView 포함하는 UserControl 주 응용 프로그램의 런타임 중에 비어 있습니다. 또한 MainControl과 함께 UserControl 프로젝트를 참조했습니다. 여기에서 나는 나를 도울 수있는 코드를 제공하고있다.UserControl/TreeView Problem .... 런타임 중에 결과가 나타나지 않는다.

미리 감사드립니다.

코드 : 해당 UserControl 클래스에서

: 내 MainForm에서

public override void Refresh() 
{ 
    PopulateTreeView(); 
} 
private void PopulateTreeView() 
{ 
    TreeNodeCollection treeNodeCollection; 
    treeNodeCollection = CreateParentNode("My Information"); 
    CreateChildNode(treeNodeCollection, "Name"); 
    CreateChildNode(treeNodeCollection, "Address"); 
    this.Update(); 
    myTreeView.ExpandAll(); 
} 
private TreeNodeCollection CreateParentNode(string parentNode) 
{ 
    TreeNode treeNode = new TreeNode(parentNode); 
    myTreeView.Nodes.Add(treeNode); 
    return treeNode.Nodes; 
} 
private void CreateChildNode(TreeNodeCollection nodeCollection, string itemName) 
{ 
    TreeNode treeNode = new TreeNode(itemName); 
    nodeCollection.Add(treeNode); 
} 

: 작성중인 버튼의 클릭 이벤트에서

private void button1_Click(object sender, EventArgs e) 
{ 
    UserControl userControl = new UserControl(); 
    userControl.Refresh(); 
} 

답변

0

새 UserControl 및 실제로 사용자를 사용하지 MainForm에 배치 된 컨트롤.

Mainframe의 UserControl에서 Refresh()를 호출하거나 새로 만든 UserControl을 MainForm의 ControlsCollection에 추가해야합니다.

Designer의 VisualStudio에서 UserControl을 추가 할 때 MainForm에는 userControl1이라는 이름의 변수가 있어야합니다. 여기에서 새로 고침()을 호출합니다. 그런 다음 예상 결과와 채워진 TreeView를 확인해야합니다. 당신이 버튼을 클릭하여 새로운 UserControl을 매번 사용하려면

private void button1_Click(object sender, EventArgs e) 
{ 
    userControl1.Refresh(); // Assume that the name of UserControl is userControl1 
} 

, 당신은 MainForm의 ControlsCollection에 컨트롤을 추가해야합니다. 그러나 그런 다음 몇 가지 레이아웃 로직을 수행해야합니다.

private void button1_Click(object sender, EventArgs e) 
{ 
    UserControl userControl = new UserControl(); 
    userControl.Refresh(); 
    this.Controls.Add(userControl); 
    // Perform layout logic and possibly remove previous added UserControl 
} 
관련 문제