将信息保存在哈希表或数组中然后输出
Holding information in hashtables or arrays then outputting
我正在 c# 中开发一个程序,我不确定如何解决这个问题。
In my program, I have a large amount of checkboxes (Yes and No) and when No is selected, a textbox appears prompting the user to write a comment, example below:
private void checkBox48_CheckedChanged(object sender, EventArgs e)
{
if (checkBox48.Checked == true)
{
// Create an instance of the dialog
frmInputBox input = new frmInputBox();
// Show the dialog modally, testing the result.
// If the user cancelled, skip past this block.
if (input.ShowDialog() == DialogResult.OK)
{
// The user clicked OK or pressed Return Key
// so display their input in this form.
problems = problems + "23. Check Outlet Drainage : " + input.txtInput.Text + Environment.NewLine;
this.txtProblems5.Text = problems;
txtProblems5.Visible = true;
}
// Check to see if the dialog is still hanging around
// and, if so, get rid of it.
if (input != null)
{
input.Dispose();
}
}
}
但是,暂时我让用户输入只是写入一个名为 problems
的 String
。我想将这些值中的每一个保存在不同的地方。
哈希表或数组是否合适? (例如txtInput.Text = Problems[40]
)
如果您使用数组,则意味着您必须按照示例为每个文本框创建条目。
出于偏好,我可能会使用 dictionary<string,string>
,其中键是控件名称。
那么,我的 textbox
值可能是:
txtProblem1.text = dictionary.ContainsKey(txtProblem1.Name) ? dictionary[txtProblem1.Name] : "";
数组或哈希表均可。 Hashtable 可能对开发人员更友好一些,并且可能占用更小的内存。这是一个小例子:
private Dictionary<int, string> problems = new Dictionary<int, string>;
// add key value pair
problems.Add(42, "your problem here");
// get value
string value = "";
if (problems.TryGetValue(42", out value))
{
// the key was present and the value is now set
}
else
{
// key wasn't found
}
我正在 c# 中开发一个程序,我不确定如何解决这个问题。
In my program, I have a large amount of checkboxes (Yes and No) and when No is selected, a textbox appears prompting the user to write a comment, example below:
private void checkBox48_CheckedChanged(object sender, EventArgs e)
{
if (checkBox48.Checked == true)
{
// Create an instance of the dialog
frmInputBox input = new frmInputBox();
// Show the dialog modally, testing the result.
// If the user cancelled, skip past this block.
if (input.ShowDialog() == DialogResult.OK)
{
// The user clicked OK or pressed Return Key
// so display their input in this form.
problems = problems + "23. Check Outlet Drainage : " + input.txtInput.Text + Environment.NewLine;
this.txtProblems5.Text = problems;
txtProblems5.Visible = true;
}
// Check to see if the dialog is still hanging around
// and, if so, get rid of it.
if (input != null)
{
input.Dispose();
}
}
}
但是,暂时我让用户输入只是写入一个名为 problems
的 String
。我想将这些值中的每一个保存在不同的地方。
哈希表或数组是否合适? (例如txtInput.Text = Problems[40]
)
如果您使用数组,则意味着您必须按照示例为每个文本框创建条目。
出于偏好,我可能会使用 dictionary<string,string>
,其中键是控件名称。
那么,我的 textbox
值可能是:
txtProblem1.text = dictionary.ContainsKey(txtProblem1.Name) ? dictionary[txtProblem1.Name] : "";
数组或哈希表均可。 Hashtable 可能对开发人员更友好一些,并且可能占用更小的内存。这是一个小例子:
private Dictionary<int, string> problems = new Dictionary<int, string>;
// add key value pair
problems.Add(42, "your problem here");
// get value
string value = "";
if (problems.TryGetValue(42", out value))
{
// the key was present and the value is now set
}
else
{
// key wasn't found
}