如果treeview根节点包含子节点,则显示错误

时间:2010-09-15 16:12:17

标签: c# winforms treeview

在加载表单时,我最初使用根节点进行树视图。我将在运行时添加子节点作为some.txt文件,方法是选择一个选项作为Addnew from contextmenu,当用户右键单击根节点时显示该选项。现在我需要的是,如果树的子节点附加到Root,并且如果用户尝试通过从上下文菜单中单击addnew选项来创建新节点,我想显示错误,因为只允许一个子节点。

我添加子节点的示例代码如下

    private void AddNew_Click(object sender, EventArgs e)
    {
        //if (tvwACH.Nodes.Count==1)
        //{
        //    MessageBox.Show("Only One File allowed");
        //}
        //else
        //{
            if (tvwACH.SelectedNode.Text != null)
            {
                string strSelectedNode = tvwACH.SelectedNode.Text.ToString();
                switch (strSelectedNode)
                {
                    case "ACH":
                        {
                            Stream myStream;

                            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

                            saveFileDialog1.InitialDirectory = @"C:\";
                            saveFileDialog1.DefaultExt = "txt";
                            saveFileDialog1.Filter = "(*.txt)|*.txt";
                            saveFileDialog1.FilterIndex = 2;
                            saveFileDialog1.RestoreDirectory = true;
                            saveFileDialog1.ValidateNames = true;
                            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                            {
                                FileName = saveFileDialog1.FileName;
                                if (FileName.Contains(" \\/:*?<>|"))
                                {
                                    MessageBox.Show("File name should not contain \\/:*?<>|", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                else
                                {
                                    if ((myStream = saveFileDialog1.OpenFile()) != null)
                                    {
                                        FileName = saveFileDialog1.FileName;
                                        TreeNode newNode = new TreeNode(FileName);
                                        newNode.SelectedImageIndex = 1;
                                        tvwACH.SelectedNode.Nodes.Add(newNode);
                                        TreeNode NodeFileHeader = newNode.Nodes.Add("FileHeader");
                                        myStream.Close();
                                    }
                                }

                            }
                            break;
                        }
                    case "FileHeader":
                        {
                            sr = new StreamReader(FileName);
                            strLen = sr.ReadLine();

                            if (strLen == null)
                            {
                                sr.Close();
                                Form frmFileHeader = new frmFileHeader(this);
                                frmFileHeader.ShowDialog(this);

                            }
                            else
                            {
                                MessageBox.Show("Only One File Header is allowed for a file", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            break;
                        }
                    case "BatchHeader":
                        {
                            Form frmBatch = new frmBatch(this);
                            frmBatch.ShowDialog();
                            break;
                        }
                }
            }
        //}

    }

1 个答案:

答案 0 :(得分:1)

没有用户喜欢被一个消息框打耳光,告诉她她做了一些愚蠢的事情。改进您的用户界面,如果不应该使用它,只需禁用该菜单项。使用上下文菜单的Opening事件,如下所示:

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
        addNewToolStripMenuItem.Enabled = tvwACH.Nodes.Count > 1;
    }