using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 四角号码查询器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox 输入汉字;
private System.Windows.Forms.TextBox 输出号码;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
// } /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.输入汉字 = new System.Windows.Forms.TextBox();
this.输出号码 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
// 
// 输入汉字
// 
this.输入汉字.Location = new System.Drawing.Point(96, 80);
this.输入汉字.Name = "输入汉字";
this.输入汉字.Size = new System.Drawing.Size(104, 21);
this.输入汉字.TabIndex = 0;
this.输入汉字.Text = "";
this.输入汉字.TextChanged += new System.EventHandler(this.查找);
// 
// 输出号码
// 
this.输出号码.Location = new System.Drawing.Point(152, 160);
this.输出号码.Name = "输出号码";
this.输出号码.TabIndex = 1;
this.输出号码.Text = "";
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(80, 40);
this.label1.Name = "label1";
this.label1.TabIndex = 2;
this.label1.Text = "请输入汉字";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.输出号码);
this.Controls.Add(this.输入汉字);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void 查找(object sender, System.EventArgs e)


TextReader reader = new StreamReader("f:\\csharp\\mb.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
if(line.StartsWith(输入汉字.Text)) //出问题的地方!!!!
{
输出号码.Text = line;
}

}
reader.Close();
}
}
}=============
出现的问题是:在 输入汉字 的textbox输入英文,可以达到预期效果,显示他对应的号码,而输入汉字,则无反应。===
f:\csharp\mb.txt,假设内容为:
的27620
一10000
国60103
在40214
人80000
a90000
b30400
c57890
=====
程序运行时,我输入汉字"的",输出号码应该输出“的27620”,但是出错了。没有反应。
而我输入英文“a”,则能正常输出“a90000”,请教高手,帮忙解决一下。 
是不是要在哪儿设置一下语言啊??另:
最终程序功能是输入“一国在”,则能正确输出“1064”  //即:取第一个字的首2位(如“一”为10),第二字的第一位(如“国”为6),第三字的第一位(如“在”为4)。这个MB.txt文件共有1600行,每行是不同的字,和其对应的编码。感谢。
如果能给出更好的方案,可再送另一百分。
只解决一个问题的,就一百分。
第一个正确解决者,得分。
谢谢各位的参与和支持。

解决方案 »

  1.   

    TextReader reader = new StreamReader(@"c:\p.txt",System.Text.Encoding.Default);你明显的没有加入Encoding,所以不能从文件读出汉字PS:贮存可以用XML来代替txtPS2:可以在程序初始化的时候就把中文或英文当作key,后面的数字当成value,放在一个     Hashtable,那就不用每次查找都打开文件,一行一行的读了
      

  2.   

    在设置StreamReader的时候,最好设置一下Encoding
      

  3.   

    if(line.StartsWith(输入汉字.Text)) //出问题的地方!!!!在这里没有捕获异常,
    如果用户实际上没有输入汉字,就会产生异常.
      

  4.   

    Seraph_98(魂~) ( ) 信誉:98  和 Knight94(愚翁) ( ) 信誉:110 
    两位的思路都很好,可是我还不能达到您认为的程序,个人难以实现你们所说的那种解决方法,虽然能明白,可是不能用C#实现出来。这其实是我的第一个程序,我C#书,都还没看完,可以给个代码片断吗?
    感谢你们的支持。to xrwang(无涯) ( ) 信誉:100 
    同意你的观点,不过,你好像没有认真看,我写的问题哦,还没有try catch那块,就已经出问题了。
      

  5.   

    这其实是我的第一个程序,我C#书,都还没看完/////////////////////////////////////////
    哈哈,我还不如楼主呢,到现在一本C#、.net的书都没看过呢,不过VS.NET的帮助很好阿
      

  6.   

    把 txt中1600行数据导入Execl然后再导入数据库中,再用模糊查询语句(like语句)便可,实现起来一点也不难,干吗非要用文本去保存数据啊?
      

  7.   

    TextReader reader = new StreamReader(@"c:\p.txt",System.Text.Encoding.Default);你明显的没有加入Encoding,所以不能从文件读出汉字PS:贮存可以用XML来代替txtPS2:可以在程序初始化的时候就把中文或英文当作key,后面的数字当成value,放在一个     Hashtable,那就不用每次查找都打开文件,一行一行的读了
      

  8.   

    结了,对不起超了时了,我在看文章。
    由于,====
    Seraph_98(魂~) ( ) 信誉:98  和 Knight94(愚翁) ( ) 信誉:110 
    两位的思路都很好,可是我还不能达到您认为的程序,个人难以实现你们所说的那种解决方法,虽然能明白,可是不能用C#实现出来。这其实是我的第一个程序,我C#书,都还没看完,可以给个代码片断吗?
    感谢你们的支持。to xrwang(无涯) ( ) 信誉:100 
    同意你的观点,不过,你好像没有认真看,我写的问题哦,还没有try catch那块,就已经出问题了。
    ===
    这个原因,后来,也没有更有建议性的答案,我就将100平分给这两位,感谢各位的捧场。