0
votes

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
You will refactor that code, will you?flq

1 Answers

1
votes

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;
    }