Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using class instance from main form at button click method

I’m new to C# so sorry for this question…
I have following code:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;

namespace Test2MailVerteiler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            string MyXmlFile = "C:/Test/Text.xml";
            XmlSerializer xs = new XmlSerializer(typeof(Team));
            using (StreamReader rd = new StreamReader(MyXmlFile))
            {
                Team team = xs.Deserialize(rd) as Team;
                int n = team.TeamMember.Length;
                for (int i = 0; i < n; i++)
                {
                    checkedListBox1.Items.Add(team.TeamMember[i].Name);
                }
            }        
        }
        public void btnReset_Click(object sender, EventArgs e)
        {
       
        }
        private void btnAddFromClipboard_Click(object sender, EventArgs e)
        {
            string sTextClipboard;
            sTextClipboard = Clipboard.GetText();
            //HOW TO USE TEAM HERE      
        }

    }

    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class Team
    {
        private TeamTeamMember[] teamMemberField;
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("TeamMember")]
        public TeamTeamMember[] TeamMember
        {
            get{ return this.teamMemberField; }
            set{ this.teamMemberField = value; }
        }
    }

    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class TeamTeamMember
    {
        private string nameField;
        private string mailAddressField;
        private string searchNameField;
        private string altSearchNameField;

        public string Name
        {
            get{ return this.nameField; }
            set{ this.nameField = value; }
        }

        public string MailAddress
        {
            get { return this.mailAddressField; }
            set{ this.mailAddressField = value; }
        }

        public string SearchName
        {
            get { return this.searchNameField; }
            set { this.searchNameField = value; }
        }

        /// <remarks/>
        public string AltSearchName
        {
            get { return this.altSearchNameField; }
            set { this.altSearchNameField = value; }
        }
    }
}

The class Team is created automatically from an XML file, the instance team is then created when reading the XML and parsing its contents.
This is done in Form1 class. When I now click on btnAddFromClipboard I want to parse the instance team and search for a string in it which I get from clipboard.
But team is not defined in

private void btnAddFromClipboard_Click(object sender, EventArgs e

How can I use the instance team created in Forms1 when the button is clicked?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Define

Team team =null;

inside form class.
Then change the statement in the form constructor
From Team team = xs.Deserialize(rd) as Team; to:

team = xs.Deserialize(rd) as Team;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading