Home > Back-end >  Using class instance from main form at button click method
Using class instance from main form at button click method

Time:02-13

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?

CodePudding user response:

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;
  • Related