4 Replies - 1235 Views - Last Post: 24 December 2014 - 02:06 PM Rate Topic: -----

#1 tlmarker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 14

Working with the tag property in c#

Posted 22 December 2014 - 10:47 PM

I am working on a program to create lessons for a computer aided instruction system. I plan to use a Tree View to create the lesson where each section of the lesson is a node in the Tree View. I want to use the tag property to store data about the section, such as the section name, and the section summary. The problem is that when add a new section, all previous tags are overwritten with the new data. I have tried everything I can think of, but nothing seems to work. I would like the solve this problem, as I use the tag property data to create and XML file for the lesson. I know this is possible, as I have done the same thing in VB.NET. I decided the create the system in C#, so I started over. I would greatly any assistance, if someone is willing to help, I will provide any source code I have created.

Thank you for you attention,
Troy Marker

Is This A Good Question/Topic? 0
  • +

Replies To: Working with the tag property in c#

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Working with the tag property in c#

Posted 22 December 2014 - 10:54 PM

I am not sure what you mean by the previous tags are overwritten. They shouldn't be. You can add nodes and such to a treeview without it overwriting any tags of other nodes. As you build nodes, set the tag property and add it to the treeview. The treeview acts as a container and whenever any node is rebuilt again, make sure you rebuild the tag. But again, adding or deleting a node would not "magically" reset tags of other nodes. The treeview control acts like a container and manager. Not in the business of editing nodes content.

:)
Was This Post Helpful? 0
  • +
  • -

#3 tlmarker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 14

Re: Working with the tag property in c#

Posted 22 December 2014 - 11:21 PM

Thank you for the response. What I do is use a custom control to enter the tag data. The code for the control is here:

// <copyright file="CtrlSection.cs" company="Troy Marker Software Solutions">
//  Copyright (c) 2014 By Troy Marker Software Solutions
// </copyright>
// <summary>
//  This file contains the code file for the Section Control.
// </summary>

namespace CAIControls.Controls
{
    using System;
    using System.Windows.Forms;

    using CAILibrary.Types;

    /// <summary>
    /// Partial class for the Section Control
    /// </summary>
    public partial class CtrlSection : UserControl
    {
        /// <summary>
        /// Property representing a Section Object.
        /// </summary>
        private CAISECTION _Section = new CAISECTION();

        /// <summary>
        /// Flag to create a new section
        /// </summary>
        private bool _Create = false;

        /// <summary>
        /// Initializes a new instance of the <see cref="CtrlSection"/> class.
        /// </summary>
        public CtrlSection()
        {
            this.InitializeComponent();
            this._Section.Name = string.Empty;
            this._Section.Summery = string.Empty;
            this.txtSectionName.Text = string.Empty;
            this.txtSectionSummary.Text = string.Empty;
        }

        /// <summary>
        /// Section Changed Event Handler delegate definition
        /// </summary>
        /// <param name="newSection">The new section structure</param>
        public delegate void SectionchangedEventHandler(CAISECTION newSection);

        /// <summary>
        /// Section Create Event Handler delegate definition
        /// </summary>
        /// <param name="newSection"></param>
        public delegate void SectionCreateEventHandler(CAISECTION newSection);

        /// <summary>
        /// Section changed event handler definition
        /// </summary>
        public event SectionchangedEventHandler Sectionchanged;

        /// <summary>
        /// Section create event handler definition
        /// </summary>
        public event SectionCreateEventHandler SectionCreated;
        /// <summary>
        /// Gets or sets a value representing a complete Section structure.
        /// </summary>
        public CAISECTION Section
        {
            get
            {
                this._Section.Name = this.txtSectionName.Text;
                this._Section.Summery = this.txtSectionSummary.Text;
                return this._Section;
            }

            set
            {
                this.txtSectionName.Text = value.Name;
                this.txtSectionSummary.Text = value.Summery;
            }
        }

        /// <summary>
        /// Gets or sets a value representing the creation flag
        /// </summary>
        public bool Create
        {
            get
            {
                return this._Create;
            }

            set
            {
                if (value)
                {
                    this.btnCommit.Text = "Create";
                    this.txtSectionName.Text = string.Empty;
                    this.txtSectionSummary.Text = string.Empty;
                }

                else
                {
                    this.btnCommit.Text = "Commit";
                }
                this._Create = value;
            }
        }

        /// <summary>
        /// Method to resize the control
        /// </summary>
        /// <param name="sender">The control calling the handler.</param>
        /// <param name="e">Event arguments.</param>
        private void CtrlSection_Resize(object sender, EventArgs e)
        {
            this.grpSectionName.Width = this.Width - 15;
            this.txtSectionName.Width = this.grpSectionName.Width - 10;
            this.grpSectionSummary.Width = this.Width - 15;
            this.txtSectionSummary.Width = this.grpSectionSummary.Width - 10;
            this.btnCommit.Left = (this.grpSectionSummary.Left + this.grpSectionSummary.Width) - this.btnCommit.Width;
        }

        /// <summary>
        /// Commit button event handler
        /// </summary>
        /// <param name="sender">object calling the handler</param>
        /// <param name="e">Event parameters</param>
        private void Commit_Click(object sender, EventArgs e)
        {
            if (btnCommit.Text == "Create")
            {
                if (this.SectionCreated != null)
                {
                    this.SectionCreated(this.Section);
                }
            }

            else
           {
                if (this.Sectionchanged != null)
                {
                    this.Sectionchanged(this.Section);
                }
            }
        }
    }
}



As you can see, I am trying to use a custom event handler to tell when the user commits the section. Listed below is the event handler.

        /// <summary>
        /// Method to create a new section in the lesson
        /// </summary>
        /// <param name="newSection">CAISECTION data structure</param>
        private void CtrlSection_SectionCreated(CAISECTION newSection)
        {
            TreeNode newNode = new TreeNode();
            newNode.Text = newSection.Name;
            newNode.Tag = newSection;
            newNode.ImageIndex = 1;
            newNode.SelectedImageIndex = 1;
            this.tvMain.SelectedNode.Nodes.Add(newNode);
        }



As mentioned, it seems that there is a loop the changes each tag. I know there is no loop, and I hope I provided the correct code to find the problem.

Once again, thanks for the response.
Troy L. Marker

This post has been edited by Skydiver: 24 December 2014 - 01:42 PM
Reason for edit:: Fixed botched code tags. Use the forward slash.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7447
  • View blog
  • Posts: 25,085
  • Joined: 05-May 12

Re: Working with the tag property in c#

Posted 24 December 2014 - 01:53 PM

First an aside: It's very poor practice to call something an "event handler" in .NET, but not follow the pattern used by .NET for events. In .NET, event handlers are based on the delegate:
void EventHandler(object sender, EventArgs e)



The pattern is that the object parameter is the object sending the event, and the e are the event arguments. If you need more than what comes with the properties of the out of the box EventArgs, you derive a subclass from it. Ex.

class ProbeLandedEventArgs : EventArgs
{
    public Point Location { get; }
    public bool HarpoonsLocked { get; }
}



With newer versions of the .NET Framework, all you need to do is use your specialized one and the generic EventHandler<T>:
class CometLander
{
    public event EventHandler<ProbeLandedEventArgs> ProbeLanded;
    :
}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7447
  • View blog
  • Posts: 25,085
  • Joined: 05-May 12

Re: Working with the tag property in c#

Posted 24 December 2014 - 02:06 PM

Second thing: Your use of the names CtrlSection and CAISECTION are against the naming conventions used in .NET and C#. You should not use abbreviations/acronyms, and you should use Camel casing.

Now, back to your main question: I suspect that you don't fully understand the difference between object instances and value types. If you are always recycling the same CAISECTION instance, then all your nodes will be pointing to that single instance. When you change the value of the members of that instance, all the nodes will "see" the changed value. As an example:

class Food
{
    public string Name { get; set; }
}

class Plate
{
    Food _food;

    public void Serve(Food food)
    {
        _food = food;
    }

    public void Show()
    {
        Console.WriteLine(_food);
    }
}

class Test
{
    static void Main()
    {
        var food = new Food();

        var bluePlate = new Plate();
        food.Name = "blueberries";
        bluePlate.Serve(food);

        bluePlate.Show();    // this will print "blueberries"

        var redPlate = new Plate();
        food.Name = "apples";
        redPlate.Serve(food);

        redPlate.Show();     // this will print "apples"

        bluePlate.Show();    // this will print "apples"
    }
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1