根据 bool 值保存来自多个对象的文本
Save text from multiple objects depending on bool value
除了提供我正在做的事情的图像外,我不知道还有什么其他方式可以解释这一点。
基本上,Save Cheats 按钮会创建一个文本文件,其中包含来自 3 个文本框(cheattbox、cheatobox、cheatbbox)的信息,并分别编号。总共有 20 个盒子,要保存 space 只有有数据的盒子才应该保存。假设有人想保存框 4、7、8 和 13 中的信息,并选中这些复选框,我希望文本文件仅包含这些给定框中的信息。
这是我到目前为止的代码。
public string savemagic(int i)
{
CheckBox tickbox = this.Controls.Find("ccheatcbox" + i.ToString(), true).FirstOrDefault() as CheckBox;
TextBox namebox = this.Controls.Find("ccheatname" + i.ToString(), true).FirstOrDefault() as TextBox;
ComboBox byteselect = this.Controls.Find("ccheatbytebox" + i.ToString(), true).FirstOrDefault() as ComboBox;
TextBox offsetbox = this.Controls.Find("ccheatofsetbox" + i.ToString(), true).FirstOrDefault() as TextBox;
TextBox bytebox = this.Controls.Find("ccheatbytes" + i.ToString(), true).FirstOrDefault() as TextBox;
fu[i] = string.Format("{0} - {1} - {2}\n",namebox.Text, offsetbox.Text, bytebox.Text);
return fu[i];
}
int checkint;
string[] fu = new string[9999];
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog _SD = new SaveFileDialog();
_SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
_SD.FileName = "Untitled";
_SD.Title = "Save As";
if (_SD.ShowDialog() == DialogResult.OK)
{
foreach(var controls in ccheattab.Controls)
{
if (controls is CheckBox && ((CheckBox)controls).Checked)
{
string tmp = ((CheckBox)controls).Name.Replace("ccheatcbox", "");
checkint = Convert.ToInt32(tmp);
File.WriteAllText(_SD.FileName, savemagic(checkint));
}
}
}
}
您可以尝试让复选框看看它是否被选中,就像这样
public string savemagic(int i)
{
CheckBox tickbox = this.Controls.Find("ccheatcbox" + i.ToString(), true).FirstOrDefault() as CheckBox;
if (tickbox.Checked) {
TextBox namebox = this.Controls.Find("ccheatname" + i.ToString(), true).FirstOrDefault() as TextBox;
ComboBox byteselect = this.Controls.Find("ccheatbytebox" + i.ToString(), true).FirstOrDefault() as ComboBox;
TextBox offsetbox = this.Controls.Find("ccheatofsetbox" + i.ToString(), true).FirstOrDefault() as TextBox;
TextBox bytebox = this.Controls.Find("ccheatbytes" + i.ToString(), true).FirstOrDefault() as TextBox;
string Cheats= string.Format("{0} - {1} - {2}\n",namebox.Text, offsetbox.Text, bytebox.Text);
return Cheats;
}
return "";
}
https://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.checked%28v=vs.110%29.aspx
此外,对于实际写入文件,请考虑使用 StreamWriter 而不是 File.WriteAllText,除非您打算使用 StringBuilder 将这些字符串放在一起。
这就是你调用 File.WriteAllText 时发生的情况:
Creates a new file, write the contents to the file, and then closes
the file. If the target file already exists, it is overwritten.
一开始,如果您抽象化您的模型并有一个轻微的分离形式,它会对您有很大帮助UI,它只会在您处理业务逻辑时让您感到困惑。
class CheatObject {
public string Name { get; set; }
public string SelectType { get; set; } // or use enum for this
public string OffsetStr{ get; set; }
public string ByteStr { get; set; }
public ExportToFile() {
// logic to export a CheatModel
}
}
...
一旦有了这个,事情就简单多了。您的问题可能只是,检查哪些行的最佳方法是什么。
然后:foreach(所有勾选框)cheatModelFromRow.ExportToFile();
除了提供我正在做的事情的图像外,我不知道还有什么其他方式可以解释这一点。
基本上,Save Cheats 按钮会创建一个文本文件,其中包含来自 3 个文本框(cheattbox、cheatobox、cheatbbox)的信息,并分别编号。总共有 20 个盒子,要保存 space 只有有数据的盒子才应该保存。假设有人想保存框 4、7、8 和 13 中的信息,并选中这些复选框,我希望文本文件仅包含这些给定框中的信息。
这是我到目前为止的代码。
public string savemagic(int i)
{
CheckBox tickbox = this.Controls.Find("ccheatcbox" + i.ToString(), true).FirstOrDefault() as CheckBox;
TextBox namebox = this.Controls.Find("ccheatname" + i.ToString(), true).FirstOrDefault() as TextBox;
ComboBox byteselect = this.Controls.Find("ccheatbytebox" + i.ToString(), true).FirstOrDefault() as ComboBox;
TextBox offsetbox = this.Controls.Find("ccheatofsetbox" + i.ToString(), true).FirstOrDefault() as TextBox;
TextBox bytebox = this.Controls.Find("ccheatbytes" + i.ToString(), true).FirstOrDefault() as TextBox;
fu[i] = string.Format("{0} - {1} - {2}\n",namebox.Text, offsetbox.Text, bytebox.Text);
return fu[i];
}
int checkint;
string[] fu = new string[9999];
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog _SD = new SaveFileDialog();
_SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
_SD.FileName = "Untitled";
_SD.Title = "Save As";
if (_SD.ShowDialog() == DialogResult.OK)
{
foreach(var controls in ccheattab.Controls)
{
if (controls is CheckBox && ((CheckBox)controls).Checked)
{
string tmp = ((CheckBox)controls).Name.Replace("ccheatcbox", "");
checkint = Convert.ToInt32(tmp);
File.WriteAllText(_SD.FileName, savemagic(checkint));
}
}
}
}
您可以尝试让复选框看看它是否被选中,就像这样
public string savemagic(int i)
{
CheckBox tickbox = this.Controls.Find("ccheatcbox" + i.ToString(), true).FirstOrDefault() as CheckBox;
if (tickbox.Checked) {
TextBox namebox = this.Controls.Find("ccheatname" + i.ToString(), true).FirstOrDefault() as TextBox;
ComboBox byteselect = this.Controls.Find("ccheatbytebox" + i.ToString(), true).FirstOrDefault() as ComboBox;
TextBox offsetbox = this.Controls.Find("ccheatofsetbox" + i.ToString(), true).FirstOrDefault() as TextBox;
TextBox bytebox = this.Controls.Find("ccheatbytes" + i.ToString(), true).FirstOrDefault() as TextBox;
string Cheats= string.Format("{0} - {1} - {2}\n",namebox.Text, offsetbox.Text, bytebox.Text);
return Cheats;
}
return "";
}
https://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.checked%28v=vs.110%29.aspx
此外,对于实际写入文件,请考虑使用 StreamWriter 而不是 File.WriteAllText,除非您打算使用 StringBuilder 将这些字符串放在一起。
这就是你调用 File.WriteAllText 时发生的情况:
Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.
一开始,如果您抽象化您的模型并有一个轻微的分离形式,它会对您有很大帮助UI,它只会在您处理业务逻辑时让您感到困惑。
class CheatObject {
public string Name { get; set; }
public string SelectType { get; set; } // or use enum for this
public string OffsetStr{ get; set; }
public string ByteStr { get; set; }
public ExportToFile() {
// logic to export a CheatModel
}
}
...
一旦有了这个,事情就简单多了。您的问题可能只是,检查哪些行的最佳方法是什么。
然后:foreach(所有勾选框)cheatModelFromRow.ExportToFile();