如何继续添加到表单上的现有文本框?输入,保存,清除和重复? [英] How to keep adding to exsiting textbox on a form? input, save, clear and repeat?

查看:141
本文介绍了如何继续添加到表单上的现有文本框?输入,保存,清除和重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System .Windows.Forms;

使用System.IO;



名称空间Angie_assignment

{

公共部分类frmInventory:表格

{

//声明公共变量并将用户信息输入文件流

const string FILENAME = INVENTORY.txt;

FileStream outFile = new FileStream(FILENAME,FileMode.Append,FileAccess.Write);



public frmInventory()

{

InitializeComponent();

}



private void btnAddSave_Click(对象发件人,EventArgs e)

{

//声明可变对象

字符串描述;

字符串价格;

字符串代码;

StreamWriter writer = new StreamWriter(outFile); //打开文本文件



do

{

//输入数据

code = txtBoxCode.Text;

description = txtBoxDrscription.Text;

price = txtBoxCost.Text;



//将信息写入文本文件

writer.WriteLine(code);

writer.WriteLine(description);

writer.WriteLine (价格);



txtBoxCode.Text =;

txtBoxDrscription.Text =;

txtBoxCost.Text =;

txtBoxCode.Focus();

}

while(rbtnLastEntry.Checked == false);





//关闭文件

writer.Close();

outFile。关闭();



}



}

}





我试图做一个循环并添加了一个单选按钮,说明用户是否在最后一个条目中工作但仍然没有用。任何帮助,将不胜感激! :D

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Angie_assignment
{
public partial class frmInventory : Form
{
//declare public variables and input user info into filestream
const string FILENAME = "INVENTORY.txt";
FileStream outFile = new FileStream(FILENAME, FileMode.Append, FileAccess.Write);

public frmInventory()
{
InitializeComponent();
}

private void btnAddSave_Click(object sender, EventArgs e)
{
//declare vairables
string description;
string price;
string code;
StreamWriter writer = new StreamWriter(outFile); // open text file

do
{
//input data
code = txtBoxCode.Text;
description = txtBoxDrscription.Text;
price = txtBoxCost.Text;

//write in info to text file
writer.WriteLine(code);
writer.WriteLine(description);
writer.WriteLine(price);

txtBoxCode.Text = "";
txtBoxDrscription.Text = "";
txtBoxCost.Text = "";
txtBoxCode.Focus();
}
while (rbtnLastEntry.Checked == false);


//close file
writer.Close();
outFile.Close();

}

}
}


I tried to do a loop and added a radio button that states whether the user is at their last entry to work around but still no use. Any help would be appreciated! :D

推荐答案

假设点击触发btnAddSave_Click(顾名思义):

- 你不需要,只需添加到文件。

- 要添加到文件而不是让它超过你需要

Assuming the btnAddSave_Click is triggered by a click (as its name implies):
- you don't need while, just add to file.
- to ADD to file instead of having it OVERWRITTEN you need
StreamWriter writer = new StreamWriter(outFile, True)

如果你没有第二个参数,文件将被最后的值覆盖。



- 考虑使用指令(

if you don't have second parameter, the file will be overwritten with the last values.

- consider using directive (

using writer = new StreamWriter...{}



)用于自动关闭和处置。上面的代码有内存泄漏错误 - 如果写入文件StreamWriter有异常而File将不会关闭。



将整个内容包装成试试...最后并在finally块中关闭/处置。更好的解决方案是简单地使用using关键字。


) for automatic closure and disposal. The code above has memory leak bug - if there is an exception in writing to file StreamWriter and File will not be closed.

Wrap entire thing into Try...Finally and close / dispose in finally block. Better solution is to simply use "using" keyword.


这篇关于如何继续添加到表单上的现有文本框?输入,保存,清除和重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆