在按钮单击方法中使用来自主窗体的 class 实例
Using class instance from main form at button click method
我是 C# 新手,很抱歉问这个问题...
我有以下代码:
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; }
}
}
}
class Team
是从 XML 文件自动创建的,然后在读取 XML 并解析其内容时创建实例 team
.
这是在 Form1
class 中完成的。当我现在单击 btnAddFromClipboard
时,我想解析实例团队并在其中搜索我从剪贴板获取的字符串。
但是
中没有定义团队
private void btnAddFromClipboard_Click(object sender, EventArgs e
单击按钮时如何使用在 Forms1
中创建的实例 team
?
定义
Team team =null;
内部表格 class。
然后更改表单构造函数中的语句
从 Team team = xs.Deserialize(rd) as Team;
到:
team = xs.Deserialize(rd) as Team;
我是 C# 新手,很抱歉问这个问题... 我有以下代码:
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; }
}
}
}
class Team
是从 XML 文件自动创建的,然后在读取 XML 并解析其内容时创建实例 team
.
这是在 Form1
class 中完成的。当我现在单击 btnAddFromClipboard
时,我想解析实例团队并在其中搜索我从剪贴板获取的字符串。
但是
private void btnAddFromClipboard_Click(object sender, EventArgs e
单击按钮时如何使用在 Forms1
中创建的实例 team
?
定义
Team team =null;
内部表格 class。
然后更改表单构造函数中的语句
从 Team team = xs.Deserialize(rd) as Team;
到:
team = xs.Deserialize(rd) as Team;