[英]Display multiple content to textbox depending on which option was selected from combobox
- 編輯 -
值得指出的是。 雖然我遇到了不同的家庭作業問題但我能夠使用單獨的對象作為條目(我可能應該這樣做)。 這樣可以更輕松地解決我的最終問題。 這個問題可能對我有用。
我正在嘗試使用foreach循環將數據顯示到文本框。 應該顯示與組合框的選擇相對應的數據。 例如,如果我想顯示PC,我應該只看到UserName和Password,如果我添加另一個條目,例如WebSite,我應該看到它的格式的前一個條目和帶有字段URL,用戶名和密碼的新條目。 我在上一個問題中嘗試過IF語句,但似乎沒有正常工作。
StringBuilder sb = new StringBuilder();
foreach (AddEntry list in addedEntry)
{
sb.AppendLine();
sb.AppendLine("URL: " + list.URL);
sb.AppendLine("Software Name: " + list.SoftwareName);
sb.AppendLine("Serial Code: " + list.SerialCode);
sb.AppendLine("User Name: " + list.UserName);
sb.AppendLine("Password: " + list.Password);
sb.AppendLine();
}
mainWindow.ChangeTextBox = sb.ToString();
問候。
StringBuilder sb = new StringBuilder();
foreach (AddEntry list in addedEntry)
{
sb.AppendLine();
if (!string.IsNullOrEmpty(list.URL))
sb.AppendLine("URL: " + list.URL);
if (!string.IsNullOrEmpty(list.SoftwareName))
sb.AppendLine("Software Name: " + list.SoftwareName);
if (!string.IsNullOrEmpty(list.SerialCode))
sb.AppendLine("Serial Code: " + list.SerialCode);
if (!string.IsNullOrEmpty(list.UserName))
sb.AppendLine("User Name: " + list.UserName);
if (!string.IsNullOrEmpty(list.Password))
sb.AppendLine("Password: " + list.Password);
sb.AppendLine();
}
mainWindow.ChangeTextBox = sb.ToString();
編輯:我使用了UnhandledException的版本,因為它比我的解決方案更易讀(並且在大多數情況下,條件運算符通常不受歡迎)。
我還想指出,使用自動屬性可以更容易地編寫AddEntry類(假設您使用的是.NET 3.0+)。
看到:
namespace Store_Passwords_and_Serial_Codes
{
class AddEntry
{
// Auto properties make this class a lot easier to read.
public string type { get; set; }
public string url { get; set; }
public string softwareName { get; set; }
public string serialCode { get; set; }
public string userName { get; set; }
public string password { get; set; }
// Non-default constructor.
public AddEntry(string type, string url, string softwareName, string serialCode, string userName, string password)
{
this.type = type;
this.url = url;
this.softwareName = softwareName;
this.serialCode = serialCode;
this.userName = userName;
this.password = password;
}
}
}
最后,正如您所說,重要的是您不保存屬於另一個條目類型的一種條目類型的信息(例如,您不應將URL保存到PC條目類型中,因為它沒有意義)。 使用更強大的類型對象(即WebPassword,PCPassword,SoftwareSerialCode等),整個解決方案可能會更好。 這些都可以從基類(Entry或其他類似的東西)繼承,以便更容易強烈地鍵入列表。
例如:
class Entry { }
class PCPassword : Entry
{
string userName { get; set; }
string password { get; set; }
public PCPassword(string uName, string pass)
{
this.userName = uName;
this.password = pass;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("User Name: " + this.userName);
sb.AppendLine("Password: " + this.password);
sb.AppendLine();
return sb.ToString();
}
}
然后,您將在代碼中引用它:
private void btnAddEntry_Click(object sender, EventArgs e)
{
// Making sure that type is selected.
if (cmbType.SelectedIndex != -1)
{
if (cmbType.SelectedIndex == 0)
{
if(textUserName.Text == String.Empty || textPassword.Text == String.Empty)
MessageBox.Show("Please fill all the fields!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
addedEntry.Add(new PCPassword(textUserName.Text, textPassword.Text));
MessageBox.Show("Entry was successfully added!", "Entry Added!", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearFields();
}
}
// etc, etc
// Print our items
StringBuilder sb = new StringBuilder();
foreach (Entry item in addedEntry)
{
sb.Append(item.ToString());
}
mainWindow.ChangeTextBox = sb.ToString();
}
}
只是以為我會把它扔出去;)
StringBuilder sb = new StringBuilder();
foreach (AddEntry list in addedEntry)
{
sb.AppendLine();
if (!string.IsNullOrEmpty(list.URL))
sb.AppendLine("URL: " + list.URL);
if (!string.IsNullOrEmpty(list.SoftwareName))
sb.AppendLine("Software Name: " + list.SoftwareName);
if (!string.IsNullOrEmpty(list.SerialCode))
sb.AppendLine("Serial Code: " + list.SerialCode);
if (!string.IsNullOrEmpty(list.UserName))
sb.AppendLine("User Name: " + list.UserName);
if (!string.IsNullOrEmpty(list.Password))
sb.AppendLine("Password: " + list.Password);
sb.AppendLine();
}
mainWindow.ChangeTextBox = sb.ToString();
第二選擇
將以下方法添加到AddEntry類
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
if (!string.IsNullOrEmpty(this.URL))
sb.AppendLine("URL: " + list.URL);
if (!string.IsNullOrEmpty(this.SoftwareName))
sb.AppendLine("Software Name: " + this.SoftwareName);
if (!string.IsNullOrEmpty(this.SerialCode))
sb.AppendLine("Serial Code: " + this.SerialCode);
if (!string.IsNullOrEmpty(this.UserName))
sb.AppendLine("User Name: " + this.UserName);
if (!string.IsNullOrEmpty(this.Password))
sb.AppendLine("Password: " + this.Password);
sb.AppendLine();
return sb.ToString();
}
然后您可以顯示所有添加的條目,如下所示
StringBuilder sb = new StringBuilder();
foreach (AddEntry entry in addedEntry)
{
sb.Append(entry.ToString());
}
mainWindow.ChangeTextBox = sb.ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.