如何从TXT文本中读取数据到textBox.Text里
要求是:从窗体里输入一个名字,然后搜索,得到地址和电话。假如名字在txt里第一行,地址就在第二行,电话就在第三行。txt里面有好多这些信息。 --------------------编程问答-------------------- StreamReader reader = new StreamReader("c:\\temp\\1.txt",Encoding.Default);textBox1.Text = reader.ReadToEnd(); --------------------编程问答-------------------- 根据你的文本格式设定搜寻逻辑不就行了
--------------------编程问答-------------------- 用TextFile这种模拟数据库,文本内容不多的话,用string[] values=File.ReadAllLine(...)方法得到文本,然后对values用相关方法找一下就可以了,你要的值就是找到元素的后一个(或后第二个)值。 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 参考 C# 读取有分隔符的txt文件--代码三人帮 --------------------编程问答-------------------- 数据是在文件里的话,最好做记录文件;读取和写入都比较方便
StreamReader reader = new StreamReader("c:\\1.txt", Encoding.Default);
string [] str = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
string name = string.Empty;
string phone = string.Empty;
string address = string.Empty;
for (int i = 0; i < str.Length; )
{
if (str[i] == "从窗体里输入一个名字")
{
phone = str[i + 1];
address = str[i + 2];
}
i += 3;
}
如果在数据库里,就简单一点了。
--------------------编程问答-------------------- 按行读取
File.ReadAllLines 方法 (String, Encoding)
读出来可以按照字符分割 --------------------编程问答-------------------- 用ajax异步获取txt里边的内容 --------------------编程问答-------------------- 2楼正解 用string[] values=File.ReadAllLine(...)方法得到文本 --------------------编程问答-------------------- 先读取txt里面的内容,然后再做数据处理不就行了?? --------------------编程问答-------------------- private void btnSearch_Click(object sender, EventArgs e)
{
string path = @"D:\Work\info.txt";
StreamReader sr = new StreamReader(path,Encoding.Default );
string strFilter=string.Empty;
while ((strFilter= sr.ReadLine())!="" )
{
if (strFilter == this.textBox1.Text.Trim())
{
this.txtAddress.Text = sr.ReadLine();
this.txtPhoneNum.Text = sr.ReadLine();
break;
}
}
} --------------------编程问答--------------------
/// <summary>
/// 数据
/// </summary>
static List<string> DataList = new List<string>();
public void GetData(string FilePath)
{
StreamReader reader = new StreamReader(FilePath,Encoding.Default);
string TempDataStr=reader.ReadToEnd();
DataList.AddRange(TempDataStr.Replace("\r\n", "*").Split('*'));
}
public Form1()
{
InitializeComponent();
GetData("c:\\temp\\1.txt");
this.textBox1.AutoCompleteCustomSource.Clear();
this.textBox1.AutoCompleteCustomSource.AddRange(DataList.ToArray());
this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode ==Keys.Enter)
{
int TempI= UsersList.FindIndex(delegate(string a) { return a == textBox1.Text; });
MessageBox.Show(UsersList[TempI+1]);//地址
MessageBox.Show(UsersList[TempI+2]);//电话
}
}
这样可以解决你的问题 --------------------编程问答-------------------- 发个效果图吧 --------------------编程问答-------------------- 楼主莫非不在?
补充:.NET技术 , C#