Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1098535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:35:35+00:00 2026-05-17T00:35:35+00:00

I am having a treeview initially with a root node when form is loaded.

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:35:36+00:00Added an answer on May 17, 2026 at 12:35 am

    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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a treeview control on a form which will be used to visually represent
I am having a tree-view on my main form with initially some nodes as
I am having a treeview with one root node . I have written MouseHoverEvent
I am having some serious trouble creating a WPF TreeView with an Object databinding.
I am creating a TreeView at runtime. It has several nodes(TreeViewItem), each one having
I am having a treeview with some nodes. I am also having a panel.
I'm having some trouble with a databound TreeView in WPF, basically I want a
I am having some difficulty figuring out how to template the following TreeView item
I am having a treeview on my main form I have my code from
I am trying to populate a TreeView in silverlight, but I am having some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.