高手进,textbox自动显示问题

字体: | 打印

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
SqlConnection sqlconn1=DB.createConnection();
System.Data.SqlClient.SqlCommand sqlcomm1=new SqlCommand("select * from sjid where id=40",sqlconn1);
sqlconn1.Open();
System.Data.SqlClient.SqlDataReader comm1=sqlcomm1.ExecuteReader();

while(comm1.Read())
{
this.textBox1.Text=comm1["sj"].ToString();
}
comm1.Close();
sqlconn1.Close();
}
为什么在一运行就是不能在textbox中显示呢?

怎样写才行!还是要改textbox的什么属性吗?

我也来说两句 查看全部评论 相关评论

  • txf (2008-7-05 19:43:41)

    不知道你想实现什么功能。如果单纯的想在程序运行的时候显示数据那么你可以在窗体的构造函数中写这段代码
    例如 可以这样写
    using System.Data.SqlClient;
    using System.Configuration;
    using Microsoft.ApplicationBlocks.Data;
    using GoldPrinter;
    using System.Collections;
    using System.IO;
    using Excel;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Diagnostics;

    namespace Device
    {
    public partial class FormMain : Form
    {
    public FormMain()
    {
    InitializeComponent();
    SqlConnection sqlconn1=DB.createConnection();
    System.Data.SqlClient.SqlCommand sqlcomm1=new SqlCommand("select * from sjid where id=40",sqlconn1);
    sqlconn1.Open();
    System.Data.SqlClient.SqlDataReader comm1=sqlcomm1.ExecuteReader();

    while(comm1.Read())
    {
    this.textBox1.Text=comm1["sj"].ToString();
    }
    comm1.Close();
    sqlconn1.Close();

    }
    }
    }

    如果还是不显示 那么有可能是你的数据库中没有数据:)
  • huyuhei (2008-7-05 19:43:43)

    TextChange不能改变TextBox已经发生的事情,也就是说这段代码已经是文字输入之后的事情了。可以自己封装一个TextBox

    using System.Windows.Forms;

    namespace YourNamespace
    {
    public class NumOnlyText : System.Windows.Forms.TextBox
    {
    public NumOnlyText()
    {

    }

    private const int WM_CHAR=0x102;

    //覆盖ProcessKeyMessage
    protected override bool ProcessKeyMessage(ref Message m)
    {
    //保持以前的处理
    base.ProcessKeyMessage(ref m);
    //加入DB处理等等
    return true;

    }

    }
    }