views:

65

answers:

1

I am having a treeview initially with a root node when form is loaded. I will add child node as some.txt file at the runtime by selecting an option as Addnew from contextmenu which was displayed when the user right clicks on the root node. Now what i need is if the tree has child node appended to the Root and if user tries to create a new node by clicking the option addnew from context menu i would like to display an error as only one child allowed.

My sample code to add a child node is as follows

    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  A: 

No user ever likes to get slapped with a message box that tells her she did something dumb. Improve your user interface, simply disable the menu item if it shouldn't be used. Use the context menu's Opening event, like this:

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
        addNewToolStripMenuItem.Enabled = tvwACH.Nodes.Count > 1;
    }
Hans Passant
But it is not showing the context menu even if the root node does not consists a child node.
Dorababu
Erm, what? I cannot make sense of your remark.
Hans Passant
For the code you given it is not allowing me to display the context menu even if the root as no childs. What i asked for is if the tree ROOT has already a child node and if i right click on the Root and if i try to add a new node it should be get disabled
Dorababu
This code does not prevent a context menu from showing up. I have no idea what you are talking about. You need to take the idea behind it and adapt it to your own needs.
Hans Passant
Ok thanks for yur code
Dorababu