使用表单保存到文本文件
所以我有一个使用 MSVS 2010 的 GUI,并且我正在创建许多表单。
我的主表单打开我的文本文件“数据库”并将正确的数据连接到另一个打开的文件。如果打开的文件包含与数据库文件不匹配的数据,那么我想将该数据添加到数据库文件中。
话虽如此,当单击“dataBaseSaveButton_Click”按钮时,将弹出一个新表单,其中包含文本框:机器、名称、PTP 名称、CMP 名称、T 宽度、馈线间距 。大多数字段将自动填充,其他字段则必须由用户输入。
表单中还有另一个“保存”按钮。现在,在此表单上,保存按钮实际上将获取文本框中的数据并打开正确的文件(数据库文件)并以某种格式保存(更新数据库)。
“保存到数据库”表单如下所示:
我的代码来自 main< /strong> 表单如下所示(单击按钮时):
private void dataBaseSaveButton_Click(object sender, EventArgs e)
{
int unknownCP4Counter = theCP4UnknownList.Distinct().Count();
foreach (var line in theCP4UnknownList.Distinct())
{
var splitUnknowns = line.Split(' ');
KTS_Save saveForm = new KTS_Save("CP4", splitUnknowns[0], splitUnknowns[1], splitUnknowns[3], splitUnknowns[4], openDataBase2File.FileName, unknownCP4Counter);
saveForm.ShowDialog();
unknownCP4Counter--;
}
}
保存到数据库 表单的代码如下所示:
private string _Machine;
private string _Name;
private string _PtpName;
private string _TapeWidth;
private string _FeederPitch;
private string _DataBaseFileName;
public KTS_Save()
{
InitializeComponent();
}
public KTS_Save(string Machine, string Name, string PtpName, string TapeWidth, string FeederPitch, string DBFileName, int Counter)
{
InitializeComponent();
_Machine = Machine;
_Name = Name;
_PtpName = PtpName;
_TapeWidth = TapeWidth;
_FeederPitch = FeederPitch;
_DataBaseFileName = DBFileName;
machineTextBox.Text = _Machine;
nameTextBox.Text = _Name;
ptpNameTextBox.Text = _PtpName;
tapeWidthTextBox.Text = _TapeWidth;
feederPitchTextBox.Text = _FeederPitch;
counterLabel.Text = Counter.ToString();
}
private void dataBaseSaveButton_Click(object sender, EventArgs e)
{
//This is where I need help.
}
我需要帮助的实际上是将所有文本框数据打印到已经存在的文件。
我知道这可能不是最有效的做事方式,所以如果您有任何建议,请告诉我! :)
(哦,是的..)
我希望程序像这样(或类似的)运行:
- 在主窗体中,单击按钮和保存到数据库表单将会弹出。
- 用户将在适当的字段(文本框)中输入。
- 用户将单击“保存到数据库”按钮,数据将写入通过主表单传入的文本文件。
- 否则,用户将点击右上角的“X”(关闭)以不保存到数据库。
So I have a GUI using MSVS 2010 and I am creating many forms.
My main form opens up my text file "database" and concats the proper data to a different opened file. If the opened file has data that does not match in the database file then I would like to add that data to the database file.
With that being said, when a button "dataBaseSaveButton_Click" is clicked a new form will pop up that has textboxes reading: Machine, Name, PTP Name, CMP Name, T-Width, Feeder Pitch. Most of the fields will be auto-populated and others the user will have to enter in.
Also in the form there is another "save" button. Now, on this form, the save button will actually take the data in the textboxes and open up the proper file (the database file) and save it (update the database) in a certain format.
The "Save To Data Base" form looks like this:
My code from the main form looks like this (when the button is clicked):
private void dataBaseSaveButton_Click(object sender, EventArgs e)
{
int unknownCP4Counter = theCP4UnknownList.Distinct().Count();
foreach (var line in theCP4UnknownList.Distinct())
{
var splitUnknowns = line.Split(' ');
KTS_Save saveForm = new KTS_Save("CP4", splitUnknowns[0], splitUnknowns[1], splitUnknowns[3], splitUnknowns[4], openDataBase2File.FileName, unknownCP4Counter);
saveForm.ShowDialog();
unknownCP4Counter--;
}
}
The code for the Save To Data Base form looks like:
private string _Machine;
private string _Name;
private string _PtpName;
private string _TapeWidth;
private string _FeederPitch;
private string _DataBaseFileName;
public KTS_Save()
{
InitializeComponent();
}
public KTS_Save(string Machine, string Name, string PtpName, string TapeWidth, string FeederPitch, string DBFileName, int Counter)
{
InitializeComponent();
_Machine = Machine;
_Name = Name;
_PtpName = PtpName;
_TapeWidth = TapeWidth;
_FeederPitch = FeederPitch;
_DataBaseFileName = DBFileName;
machineTextBox.Text = _Machine;
nameTextBox.Text = _Name;
ptpNameTextBox.Text = _PtpName;
tapeWidthTextBox.Text = _TapeWidth;
feederPitchTextBox.Text = _FeederPitch;
counterLabel.Text = Counter.ToString();
}
private void dataBaseSaveButton_Click(object sender, EventArgs e)
{
//This is where I need help.
}
What I need help with is actually printing out all of the textbox data into the file that already exists.
I know this is probably not the most efficient way to do things, so if you have any recommendations please let me know! :)
(Oh yeah..)
I would like the program to run like so (or similar):
- From the main form, click on the button and the Save To Data Base form will pop up.
- The user will enter in the appropriate fields (textboxes).
- The user will click the "Save To Data Base" button and the data will be written the the textfile that is passed in through the main form.
- Or else, the user will click the upper right "X" (close) to not save to the data base.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以“从技术上”插入到文件中,但基本上每次插入内容时都必须复制整个文件。更准确地说:这不是真正的插入,因为 文件系统不支持插入。有在线多个示例,但我实际上建议您避免整个崩溃,要么使用嵌入式数据库,要么XML 文件。
我强烈建议您使用 SQLite 数据库并利用 ADO .NET 实体框架,但如果这对您来说很麻烦,那么您可以使用简单的 XML 文件。
以下是一些示例:
祝你好运。
You can "technically" insert into a file, but you basically have to copy the entire file over every time you insert something. To be more precise: it's not a real insert, because the file system does not support inserts. There are multiple examples online, but I would actually recommend that you avoid the whole debacle and either use an embedded database or XML files.
I would highly recommend that you use an SQLite database and take advantage of the ADO .NET Entity Framework, but if that's a big hassle for you then you can do simple XML files.
Here are some examples:
Good luck.