我正在尝试添加一个搜索按钮,所以当我点击按钮时,它会打开另一个框,允许我键入我要搜索的内容然后按搜索。但我似乎无法从form2中的form1访问我的arraylist和我的DataStructure。所以我想知道如何传递这些变量,以便我可以在我的form2`
中访问它们 #region Variables
FootballClub PlayerMemberData = new FootballClub(0);
ArrayList ClubData = new ArrayList();
public int CurrentPlayerShown = 0;
// Path to the file for loading/saving. Note the '@' sign in front of the string. Without it all '\' have to be double '\\'
string PlayerDetails = @"C:\Users\Clare\Desktop\Football_Player.txt";
#endregion
#region DataStructure
// ==============================The Data Structure=====================================
[Serializable]
private struct FootballClub
{
//the data types making up the struct
public string Firstname;
public string Surname;
public int age;
public string Address1;
public string Address2;
public string City;
public string Country;
public string Postcode;
public int HomeNum;
public int MobNum;
public string Email;
public string EmergencyName;
public int EmergencyNum;
public int MinutesPlayed;
public int Goals;
public int Penalties;
public char CardGiven;
public int TotalTime;
public int TotalGoals;
public int TotalPenalties;
//the "constructor". Fills data structure with default vaules
public FootballClub(int x)
{
Firstname = "";
Surname = "";
age = 0;
Address1 = "";
Address2 = "";
City = "";
Country = "";
Postcode = "";
HomeNum = 0;
MobNum = 0;
Email = "";
EmergencyName = "";
EmergencyNum = 0;
MinutesPlayed = 0;
Goals = 0;
Penalties = 0;
CardGiven = ' ';
TotalTime = 0;
TotalGoals = 0;
TotalPenalties = 0;
}
}
// ==============================End of Data Structure=====================================
#endregion
#region MatchDetails
private void NewMatch_Butt_Click(object sender, EventArgs e)
{
//create a new TabPage
TabPage newTP = new TabPage();
//add it to the TabControl
tabControl1.TabPages.Add(newTP);
//set the title to 'Match'plus the tab number
int TabPageNumber = tabControl1.SelectedIndex+1; //adds 1 from the current tab
tabControl1.TabPages[TabPageNumber].Text = "Match " + (TabPageNumber+1);
//make it the selected tab
tabControl1.SelectTab(TabPageNumber);
DeleteMatch_Butt.Enabled = true;// now that we have something to delete we can enable this button
//make the new tab page the parent of the panel that holds all the controls
TabPanel.Parent = tabControl1.SelectedTab; //
}
private void DeleteMatch_Butt_Click(object sender, EventArgs e)
{
// Removes the selected tab:
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
//disable button if only one tabpage left
if (tabControl1.SelectedIndex < 1)
{
DeleteMatch_Butt.Enabled = false;
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
TabPanel.Parent = tabControl1.SelectedTab;
}
private void MinutesPlayed_Num_ValueChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.MinutesPlayed = (int)MinutesPlayed_Num.Value;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void GoalsScored_Num_ValueChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Goals = (int)GoalsScored_Num.Value;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void PenaltiesScored_Num_ValueChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Penalties = (int)PenaltiesScored_Num.Value;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void YellowCard_RadioButt_CheckedChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.CardGiven = 'Y';
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void RedCard_RadioButt_CheckedChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.CardGiven = 'R';
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
#endregion
#region PlayerDetails
private void FirstNameTextBox_TextChanged(object sender, EventArgs e)
{
//1. Create a new empty structure to hold member data
FootballClub PlayerMemberData = new FootballClub(0);
//2. Copy content of the current entry into the new empty structure
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
//3. Change only the name currently shown in the GUI in the structure
PlayerMemberData.Firstname = FirstNameTextBox.Text;
//4. overwrite the old data with the changed one.
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void SurnameTextBox_TextChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Surname = SurnameTextBox.Text;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void PlayerAge_ValueChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.age = (int)PlayerAge.Value;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void Address1_Text_TextChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Address1 = Address1_Text.Text;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void Address2_Text_TextChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Address2 = Address2_Text.Text;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void City_Text_TextChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.City = City_Text.Text;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void Country_Text_TextChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Country = Country_Text.Text;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void Postcode_Text_TextChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Postcode = Postcode_Text.Text;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void HomeNumber_Text_TextChanged(object sender, EventArgs e)
{
int HomeNum;
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
HomeNum = Convert.ToInt32(HomeNumber_Text.Text);
PlayerMemberData.HomeNum = HomeNum;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void MobileNum_Text_TextChanged(object sender, EventArgs e)
{
int MobNum;
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
MobNum = Convert.ToInt32(MobileNum_Text.Text);
PlayerMemberData.HomeNum = MobNum;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void Email_Text_TextChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Email = Email_Text.Text;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void EmergName_Text_TextChanged(object sender, EventArgs e)
{
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.EmergencyName = EmergName_Text.Text;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
private void EmergNum_Text_TextChanged(object sender, EventArgs e)
{
int EmergNumber;
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
EmergNumber = Convert.ToInt32(EmergNum_Text.Text);
PlayerMemberData.EmergencyNum = EmergNumber;
ClubData[CurrentPlayerShown] = PlayerMemberData;
}
#endregion
#region ShowMember
private void ShowCurrentMember()
{
FootballClub PlayerMemberData;
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
FirstNameTextBox.Text = PlayerMemberData.Firstname;
SurnameTextBox.Text = PlayerMemberData.Surname;
PlayerAge.Value = PlayerMemberData.age;
Address1_Text.Text = PlayerMemberData.Address1;
Address2_Text.Text = PlayerMemberData.Address2;
City_Text.Text = PlayerMemberData.City;
Country_Text.Text = PlayerMemberData.Country;
Postcode_Text.Text = PlayerMemberData.Postcode;
HomeNumber_Text.Text = " " + PlayerMemberData.HomeNum;
MobileNum_Text.Text = " " + PlayerMemberData.MobNum;
Email_Text.Text = PlayerMemberData.Email;
EmergName_Text.Text = PlayerMemberData.EmergencyName;
EmergNum_Text.Text = " " + PlayerMemberData.EmergencyNum;
MinutesPlayed_Num.Value = PlayerMemberData.MinutesPlayed;
GoalsScored_Num.Value = PlayerMemberData.Goals;
PenaltiesScored_Num.Value = PlayerMemberData.Penalties;
if (PlayerMemberData.CardGiven == 'Y') YellowCard_RadioButt.Checked = true;
else RedCard_RadioButt.Checked = true;
TotalTime_Text.Text = " " + PlayerMemberData.MinutesPlayed;
TotalGoals_Text.Text = " " + PlayerMemberData.Goals;
TotalPenalties_Text.Text = " " + PlayerMemberData.Penalties;
Player1.Text = "Player " + (CurrentPlayerShown + 1);
UpdatePrevNextBtnStatus();
}
private void UpdatePrevNextBtnStatus()
{
if (CurrentPlayerShown <= 0) PrevPlayer_Butt.Enabled = false;
else PrevPlayer_Butt.Enabled = true;
if (CurrentPlayerShown >= (ClubData.Count - 1)) NextPlayer_Butt.Enabled = false;
else NextPlayer_Butt.Enabled = true;
}
#endregion
#region Clear
private void Clear_Butt_Click(object sender, EventArgs e)
{
ClearDetails();
}
private void ClearDetails()
{
//clear the data struct
FootballClub PlayerMemberData = new FootballClub(0);
PlayerMemberData = (FootballClub)ClubData[CurrentPlayerShown];
PlayerMemberData.Firstname = "";
PlayerMemberData.Surname = "";
PlayerMemberData.age = 0;
PlayerMemberData.Address1 = "";
PlayerMemberData.Address2 = "";
PlayerMemberData.City = "";
PlayerMemberData.Country = "";
PlayerMemberData.Postcode = "";
PlayerMemberData.HomeNum = 0;
PlayerMemberData.MobNum = 0;
PlayerMemberData.Email = "";
PlayerMemberData.EmergencyName = "";
PlayerMemberData.EmergencyNum = 0;
PlayerMemberData.MinutesPlayed = 0;
PlayerMemberData.Goals = 0;
PlayerMemberData.Penalties = 0;
PlayerMemberData.CardGiven = ' ';
ClubData[CurrentPlayerShown] = PlayerMemberData;
ShowCurrentMember();
}
#endregion
#region Save
private void SaveDetails()
{
// CREATE a file for saving (technically a 'FileStream')
FileStream fs = new FileStream(PlayerDetails, FileMode.Create);
// Construct a BinaryFormatter and use it to serialise the data to the stream.
// this "serialises" the ArrayList into binary (i.e. not to human readable text file)
BinaryFormatter binFormatter = new BinaryFormatter();
// Write the file to disk
binFormatter.Serialize(fs, ClubData);
// Close the file
fs.Close();
} // end of Save method
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
SaveDetails();
}
#endregion
#region Load
private void LoadDetails()
{
// OPEN file for loading
FileStream fs = new FileStream(PlayerDetails, FileMode.Open);
// Construct a BinaryFormatter for de-serialisation of data from the stream.
BinaryFormatter binFormatter = new BinaryFormatter();
// Read the file from disk
ClubData = (ArrayList)binFormatter.Deserialize(fs);
// Close the file
fs.Close();
CurrentPlayerShown = 0;
ShowCurrentMember();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadDetails();
}
#endregion
private void PrevPlayer_Butt_Click(object sender, EventArgs e)
{
--CurrentPlayerShown;
ShowCurrentMember();
UpdatePrevNextBtnStatus();
}
private void NextPlayer_Butt_Click(object sender, EventArgs e)
{
++CurrentPlayerShown;
ShowCurrentMember();
UpdatePrevNextBtnStatus();
if (Regex.IsMatch(Postcode_Text.Text, @"^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKPS-UW]) {0,1}[0-9][ABD-HJLNP-UW-Z]{2})$"))
{
errorProvider1.SetError(Postcode_Text, String.Empty);
}
else
{
errorProvider1.SetError(Postcode_Text, "Not a UK postcode!\nPlease Try again.");
}
if (Regex.IsMatch(Email_Text.Text, @"^(?<name>[\w\.]+)\@(?<domain>.+)\.(?<extension>\w{2,}$)"))
{
errorProvider1.SetError(Email_Text, String.Empty);
}
else
{
errorProvider1.SetError(Email_Text, "Not a Valid Emailaddress!\n Please Try Again");
}
if(Regex.IsMatch(HomeNumber_Text.Text, @"(\s*\(?0\d{4}\)?(\s*|-)\d{3}(\s*|-)\d{3}\s*)|(\s*\(?0\d{3}\)?(\s*|-)\d{3}(\s*|- )\d{4}\s*)|(\s*(7|8)(\d{7}|\d{3}(\-|\s{1})\d{4})\s*)"))
{
errorProvider1.SetError(HomeNumber_Text, "Not a Valid UK Phone Number!\n Please Try Again.");
}
else
{
errorProvider1.SetError(HomeNumber_Text, string.Empty);
}
if (Regex.IsMatch(MobileNum_Text.Text, @"^[0-9]{10,11}$"))
{
errorProvider1.SetError(MobileNum_Text, "Not a Valid Mobile Number\n Please Try Again");
}
else
{
errorProvider1.SetError(MobileNum_Text, string.Empty);
}
if (Regex.IsMatch(EmergNum_Text.Text, @"^[0-9]{10,11}$"))
{
errorProvider1.SetError(EmergNum_Text, "Not a Valid Mobile Number\n Please Try Again");
}
else
{
errorProvider1.SetError(EmergNum_Text, string.Empty);
}
}
private void NewPlayer_Butt_Click(object sender, EventArgs e)
{
FootballClub PlayerMemberData;
PlayerMemberData = new FootballClub(1);
PlayerMemberData.Firstname = "";
PlayerMemberData.Surname = "";
PlayerMemberData.age = 0;
PlayerMemberData.Address1 = "";
PlayerMemberData.Address2 = "";
PlayerMemberData.City = "";
PlayerMemberData.Country = "";
PlayerMemberData.Postcode = "";
PlayerMemberData.HomeNum = 0;
PlayerMemberData.MobNum = 0;
PlayerMemberData.Email = "";
PlayerMemberData.EmergencyName = "";
PlayerMemberData.EmergencyNum = 0;
PlayerMemberData.MinutesPlayed = 0;
PlayerMemberData.Goals = 0;
PlayerMemberData.Penalties = 0;
PlayerMemberData.CardGiven = ' ';
ClubData.Add(PlayerMemberData);
ClubData.Sort(new MonarchNameComparer());
ShowCurrentMember();
}
public class MonarchNameComparer : IComparer
{
public int Compare(object x, object y)
{
return ((FootballClub)x).Firstname.CompareTo(((FootballClub)y).Firstname);
}
}
private void DeletePlayer_Butt_Click(object sender, EventArgs e)
{
ClubData.RemoveAt(CurrentPlayerShown);
ShowCurrentMember();
}
private void Searching_Butt_Click(object sender, EventArgs e)
{
Search_Form Search = new Search_Form(); //"this" is passing the main form to the second form to be able to control the main form...
Search.Show();
}
}
}
private void SearchButt_Click(object sender, EventArgs e)
{
string toFind;
string source;
toFind = Search_Text.Text;
toFind = toFind.ToLower(); // Convert toFind string to lowercase
for (int i = 0; i < ClubData.Count; ++i)
{ // Assemble the source string through which we then can search
source = (()ClubData[i]).FirstName + ((FootballData)ClubData[i]).age;
source = source.ToLower(); // Convert source string to lowercase
if (source.Contains(toFind))
{
currentMemberShown = i;
ShowCurrentMember();
break; // break out of the 'for' loop
}
if (i == (ClubData.Count - 1))
{
MessageBox.Show(toFind + " not found");
}
} // end of 'for' loop
this.Close();
} // end of SearchBtn even handler
}
} `
答案 0 :(得分:2)
有多种方法可以做到这一点。一种方法是为 [ 0.340s] [cast.receiver.CastReceiverManager] Dispatching CastReceiverManager system ready event
desorona_cast_receiver.html:45 Received Ready event: {"id":"E9198EB3","name":"DESORONA","sessionId":"93EB8C53-4589-4D49-B578-E46D289B57F9","namespaces":[],"launchingSenderId":"40:com.example.test-145"}
cast_receiver.js:40 [ 0.353s] [cast.receiver.IpcChannel] IPC message sent: {"namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender","data":"{\"type\":\"setappstate\",\"statusText\":\"Application status is ready...\"}"}
cast_receiver.js:40 [ 0.373s] [cast.receiver.IpcChannel] Received message: {"data":"{\"level\":1.0,\"muted\":false,\"type\":\"volumechanged\"}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.375s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.378s] [cast.receiver.CastReceiverManager] Dispatching system volume changed event [1, false]
cast_receiver.js:40 [ 0.382s] [cast.receiver.IpcChannel] Received message: {"data":"{\"type\":\"visibilitychanged\",\"visible\":true}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.385s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.388s] [cast.receiver.CastReceiverManager] Dispatching visibility changed event true
cast_receiver.js:40 [ 0.392s] [cast.receiver.IpcChannel] Received message: {"data":"{\"standby\":false,\"type\":\"standbychanged\"}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.393s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.395s] [cast.receiver.CastReceiverManager] Dispatching standby changed event false
创建Search_Form
的新构造函数。
ClubData
然后您可以实例化
// Search_Form.cs
FootballClub ClubData;
public Search_Form(FootballClub cdata)
{
ClubData = cdata;
}
此外,您应该try and avoid using ArrayLists并选择通用集合,例如Search_Form Search = new Search_Form(ClubData);
Search.Show();
。
答案 1 :(得分:0)
另一种方法是公开变量/无论你想要访问什么,然后就是这样:
我们在表单1中说我们有这个变量:
string greeting = "Hello, how are you?"
然后你想要在表格2中访问:
Form1 f = new Form1();
//And then you acces the variable writing an f. before then name of it. What you´re doing here is creating an instance of Form 1 in Form 2.
//Example:
string ui = f.greeting;
//Another one:
if(f.greeting != "Hello")
{
//Whatever
}
希望它有效!!