私のポイントは、StatusStripLabel にすべての子ノードの数を表示することです。私の要点は、子ノードの数が変更されるたびに StatusStripLabel が更新されるようにしたいということです-既に存在するもののいくつかを追加または削除します。まず、コードを に配置しましたPublic Form
が、期待どおりに動作しませんでした。しばらくして、実際に機能するアイデアが浮かびました。ボタンメソッド内にコードを配置しました。しかしその後、ノードを削除する場合に備えて、2 番目に配置する必要があることに気付きました。だから私の質問は次のとおりです。それをより簡単にすることができるものはありますか? 私の説明が十分でない場合は、教えてください。最善を尽くします。
コードからPublic Form
(ボタンを押した後ではなく、最初からカウンターを機能させたいため)
childNodeCounter();
toolStripStatusLabel1.Text = "Number of games in database: " + NodeCounter.ToString();
方法:
public void childNodeCounter()
{
NodeCounter = 0;
foreach (TreeNode RootNode in treeView1.Nodes)
{
foreach (TreeNode ChildNode in RootNode.Nodes)
{
NodeCounter++;
}
}
toolStripStatusLabel1.Text = "Number of games in database: " + NodeCounter.ToString();
}
ボタンメソッド内のコード:
private void button1_Click(object sender, EventArgs e)
{
NodeCounter = 0;
foreach (TreeNode RootNode in treeView1.Nodes)
{
foreach (TreeNode ChildNode in RootNode.Nodes)
{
NodeCounter++;
}
}
toolStripStatusLabel1.Text = "Number of games in database: " + NodeCounter.ToString();
}
編集:氏に感謝します。Hans Passant 私はこれを書きましたが、非常にうまく機能します。
public int childNodeCounter(TreeNodeCollection nodes)
{
int count = 0;
foreach (TreeNode RootNode in nodes)
{
foreach (TreeNode ChildNode in RootNode.Nodes)
count++;
}
return count;
イベント ハンドラーは次のようになります。
toolStripStatusLabel1.Text = "Number of games in database: " + childNodeCounter(treeView1.Nodes);