主题:求助:从未对字段“……”赋值,字段将一直保持其默认值 n
我编了一个小程序,其功能是:通过Timer控件,每隔60秒钟,就将改变后的数据写回数据库.
存在的两个问题
第一个问题:未能实现通过Timer控件,每隔60秒钟,就将改变后的数据写回数据库.
,但点击"保存"按钮,则可以更新数据库数据.
第二个问题,出现错误提示:
C:\Student\Form1.cs(24): 从未对字段“Student.Form1.components”赋值,字段将一直保持其默认值 null
请问各位高手应如何修改程序.
程序代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Student
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtNumber;
private System.Windows.Forms.Button BtPre;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Button BtNext;
private System.Windows.Forms.Button btsave;
private System.Windows.Forms.Button button1;
private System.Timers.Timer timer1;
private System.ComponentModel.IContainer [color=FF0000]components[/color];
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//初始化组件
bool myResult;
//定义一个Short变量
myResult =OpenDb();
//调用opendb()打开数据库
}
/// <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.label1 = new System.Windows.Forms.Label();
this.txtNumber = new System.Windows.Forms.TextBox();
this.BtPre = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.BtNext = new System.Windows.Forms.Button();
this.btsave = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 24);
this.label1.TabIndex = 0;
this.label1.Text = "学号";
//
// txtNumber
//
this.txtNumber.Location = new System.Drawing.Point(152, 32);
this.txtNumber.Name = "txtNumber";
this.txtNumber.Size = new System.Drawing.Size(152, 25);
this.txtNumber.TabIndex = 1;
this.txtNumber.Text = "";
//
// BtPre
//
this.BtPre.Location = new System.Drawing.Point(344, 32);
this.BtPre.Name = "BtPre";
this.BtPre.Size = new System.Drawing.Size(96, 32);
this.BtPre.TabIndex = 2;
this.BtPre.Text = "上一条";
this.BtPre.Click += new System.EventHandler(this.BtPre_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 104);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 32);
this.label2.TabIndex = 3;
this.label2.Text = "名称";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(152, 104);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(152, 25);
this.txtName.TabIndex = 4;
this.txtName.Text = "";
this.txtName.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txtName_MouseDown);
this.txtName.TextChanged += new System.EventHandler(this.txtName_TextChanged);
//
// BtNext
//
this.BtNext.Location = new System.Drawing.Point(352, 104);
this.BtNext.Name = "BtNext";
this.BtNext.Size = new System.Drawing.Size(88, 32);
this.BtNext.TabIndex = 5;
this.BtNext.Text = "下一条";
this.BtNext.Click += new System.EventHandler(this.BtNext_Click);
//
// btsave
//
this.btsave.Location = new System.Drawing.Point(368, 168);
this.btsave.Name = "btsave";
this.btsave.Size = new System.Drawing.Size(64, 48);
this.btsave.TabIndex = 7;
this.btsave.Text = "保存";
this.btsave.Click += new System.EventHandler(this.btsave_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(360, 248);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 48);
this.button1.TabIndex = 8;
this.button1.Text = "退出";
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.SynchronizingObject = this;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.AutoScroll = true;
this.AutoScrollMinSize = new System.Drawing.Size(250, 1500);
this.ClientSize = new System.Drawing.Size(445, 355);
this.Controls.Add(this.button1);
this.Controls.Add(this.btsave);
this.Controls.Add(this.BtNext);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label2);
this.Controls.Add(this.BtPre);
this.Controls.Add(this.txtNumber);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Location = new System.Drawing.Point(-10, 0);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "Form1";
this.TopMost = true;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//----------------------------------------------
//以下定义了一些DAO对象
//和一些SQL打开数据库字符串
//设计了一个OPenDB()函数
//用DAO对象打开了数据库
//----------------------------------------------
DAO.Database Mydb;
//定义一个DAO.Database数据库对象
DAO.Workspace MyWs;
//定义一个工作空间DAO.Workspace
int tm01=0;
String MySql ;
//定义一个字符串用来存放SQL查询语言
DAO.Recordset Myrec;
//定义一个数据项DAO.Recordset
DAO.DBEngine de=new DAO.DBEngineClass();
//定义一个数据引擎DAO.DBEngine()
public bool OpenDb()
{
//定义一个数据打开函数
String dbstr;
//定义一个字符串来存放数据库所在目录
dbstr =Application.StartupPath +"\\STUDENT.MDB";
//dbstr ="c:\\STUDENT.MDB";
//取得数据库所在目录
MyWs = de.Workspaces[0];
//建立一个工作空间
Mydb = MyWs.OpenDatabase(dbstr,null,null,null);
//打开数据库存放于Mydb
MySql = "select * from 学生名称";
//设置SQL打开方式
Myrec = Mydb.OpenRecordset(MySql,
DAO.RecordsetTypeEnum.dbOpenDynaset,
DAO.RecordsetOptionEnum.dbDenyWrite,
DAO.LockTypeEnum.dbOptimistic);
//打开数据项
return true;
}
private void button1_Click(object sender, System.EventArgs e)
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
txtNumber.Text = Myrec.Fields[0].Value.ToString();
//给 txtnumber.Text赋值为数据项的0单员中的内容
txtName.Text = Myrec.Fields[1].Value.ToString();
//给 txtname.Text赋值为数据项的1单员中的内容
}
private void BtPre_Click(object sender, System.EventArgs e)
{
//点击上一条时产生的操作
Myrec.MovePrevious();
//如果没到则向前移动数据项
if(Myrec.BOF)
{
//判断是否移动到了末尾
MessageBox.Show("已经是第一条");
//如果到了末尾则不向上移动数据项
}
else
{
txtNumber.Text = Myrec.Fields[0].Value.ToString();
//给 txtnumber.Text赋值为数据项的0单员中的内容
txtName.Text = Myrec.Fields[1].Value.ToString();
//给 txtname.Text赋值为数据项的1单员中的内容
//TxtDate.Text = Myrec.Fields[3].Value.ToString().Substring(0, 9);
//用.Substring(0, 9)方法取得时间字符串的前10位
//给 txtdate.Text赋值为数据项的3单员中的内容
//TxtTel.Text = Myrec.Fields[2].Value.ToString();
//给 txttel.Text赋值为数据项的2单员中的内容
}
}
private void BtNext_Click(object sender, System.EventArgs e)
{
//当点击下一条按钮时产生的操作
Myrec.MoveNext();
//数据项先后移动一项
if(Myrec.EOF)
{
//判断是否移动到了末尾
MessageBox.Show("已经是最后一条");
//如果到了末尾则不向下移动数据项
}
else
{
txtNumber.Text = Myrec.Fields[0].Value.ToString();
//给 txtnumber.Text赋值为数据项的0单员中的内容
txtName.Text = Myrec.Fields[1].Value.ToString();
//给 txtname.Text赋值为数据项的1单员中的内容
//TxtDate.Text = Myrec.Fields[3].Value.ToString().Substring(0, 9);
//用.Substring(0, 9)方法取得时间字符串的前10位
//给 txtdate.Text赋值为数据项的3单员中的内容
//TxtTel.Text = Myrec.Fields[2].Value.ToString();
//给 txttel.Text赋值为数据项的2单员中的内容
}
}
private void txtName_TextChanged(object sender, System.EventArgs e)
{
}
private void txtName_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(txtName.Text =="■")
{
txtName.Text = " ";
}
else
{
txtName.Text = "■";
}
}
private void btsave_Click(object sender, System.EventArgs e)
{
Myrec.Edit();
Myrec.Fields[1].Value=txtName.Text;
Myrec.Update(1,false);
}
private void button1_Click_1(object sender, System.EventArgs e)
{
Application.Exit();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
[color=FF00FF]this.timer1.Enabled = true ;
this.timer1.Interval = 1000 ;
tm01 = tm01 + 1;
if (tm01 == 60)
{
tm01 = 0;
Myrec.Edit();
Myrec.Fields[1].Value=txtName.Text;
Myrec.Update(1,false);[/color]
}
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
}
}
}
存在的两个问题
第一个问题:未能实现通过Timer控件,每隔60秒钟,就将改变后的数据写回数据库.
,但点击"保存"按钮,则可以更新数据库数据.
第二个问题,出现错误提示:
C:\Student\Form1.cs(24): 从未对字段“Student.Form1.components”赋值,字段将一直保持其默认值 null
请问各位高手应如何修改程序.
程序代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Student
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtNumber;
private System.Windows.Forms.Button BtPre;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Button BtNext;
private System.Windows.Forms.Button btsave;
private System.Windows.Forms.Button button1;
private System.Timers.Timer timer1;
private System.ComponentModel.IContainer [color=FF0000]components[/color];
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//初始化组件
bool myResult;
//定义一个Short变量
myResult =OpenDb();
//调用opendb()打开数据库
}
/// <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.label1 = new System.Windows.Forms.Label();
this.txtNumber = new System.Windows.Forms.TextBox();
this.BtPre = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.BtNext = new System.Windows.Forms.Button();
this.btsave = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 24);
this.label1.TabIndex = 0;
this.label1.Text = "学号";
//
// txtNumber
//
this.txtNumber.Location = new System.Drawing.Point(152, 32);
this.txtNumber.Name = "txtNumber";
this.txtNumber.Size = new System.Drawing.Size(152, 25);
this.txtNumber.TabIndex = 1;
this.txtNumber.Text = "";
//
// BtPre
//
this.BtPre.Location = new System.Drawing.Point(344, 32);
this.BtPre.Name = "BtPre";
this.BtPre.Size = new System.Drawing.Size(96, 32);
this.BtPre.TabIndex = 2;
this.BtPre.Text = "上一条";
this.BtPre.Click += new System.EventHandler(this.BtPre_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 104);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 32);
this.label2.TabIndex = 3;
this.label2.Text = "名称";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(152, 104);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(152, 25);
this.txtName.TabIndex = 4;
this.txtName.Text = "";
this.txtName.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txtName_MouseDown);
this.txtName.TextChanged += new System.EventHandler(this.txtName_TextChanged);
//
// BtNext
//
this.BtNext.Location = new System.Drawing.Point(352, 104);
this.BtNext.Name = "BtNext";
this.BtNext.Size = new System.Drawing.Size(88, 32);
this.BtNext.TabIndex = 5;
this.BtNext.Text = "下一条";
this.BtNext.Click += new System.EventHandler(this.BtNext_Click);
//
// btsave
//
this.btsave.Location = new System.Drawing.Point(368, 168);
this.btsave.Name = "btsave";
this.btsave.Size = new System.Drawing.Size(64, 48);
this.btsave.TabIndex = 7;
this.btsave.Text = "保存";
this.btsave.Click += new System.EventHandler(this.btsave_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(360, 248);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 48);
this.button1.TabIndex = 8;
this.button1.Text = "退出";
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.SynchronizingObject = this;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.AutoScroll = true;
this.AutoScrollMinSize = new System.Drawing.Size(250, 1500);
this.ClientSize = new System.Drawing.Size(445, 355);
this.Controls.Add(this.button1);
this.Controls.Add(this.btsave);
this.Controls.Add(this.BtNext);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label2);
this.Controls.Add(this.BtPre);
this.Controls.Add(this.txtNumber);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Location = new System.Drawing.Point(-10, 0);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Text = "Form1";
this.TopMost = true;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//----------------------------------------------
//以下定义了一些DAO对象
//和一些SQL打开数据库字符串
//设计了一个OPenDB()函数
//用DAO对象打开了数据库
//----------------------------------------------
DAO.Database Mydb;
//定义一个DAO.Database数据库对象
DAO.Workspace MyWs;
//定义一个工作空间DAO.Workspace
int tm01=0;
String MySql ;
//定义一个字符串用来存放SQL查询语言
DAO.Recordset Myrec;
//定义一个数据项DAO.Recordset
DAO.DBEngine de=new DAO.DBEngineClass();
//定义一个数据引擎DAO.DBEngine()
public bool OpenDb()
{
//定义一个数据打开函数
String dbstr;
//定义一个字符串来存放数据库所在目录
dbstr =Application.StartupPath +"\\STUDENT.MDB";
//dbstr ="c:\\STUDENT.MDB";
//取得数据库所在目录
MyWs = de.Workspaces[0];
//建立一个工作空间
Mydb = MyWs.OpenDatabase(dbstr,null,null,null);
//打开数据库存放于Mydb
MySql = "select * from 学生名称";
//设置SQL打开方式
Myrec = Mydb.OpenRecordset(MySql,
DAO.RecordsetTypeEnum.dbOpenDynaset,
DAO.RecordsetOptionEnum.dbDenyWrite,
DAO.LockTypeEnum.dbOptimistic);
//打开数据项
return true;
}
private void button1_Click(object sender, System.EventArgs e)
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
txtNumber.Text = Myrec.Fields[0].Value.ToString();
//给 txtnumber.Text赋值为数据项的0单员中的内容
txtName.Text = Myrec.Fields[1].Value.ToString();
//给 txtname.Text赋值为数据项的1单员中的内容
}
private void BtPre_Click(object sender, System.EventArgs e)
{
//点击上一条时产生的操作
Myrec.MovePrevious();
//如果没到则向前移动数据项
if(Myrec.BOF)
{
//判断是否移动到了末尾
MessageBox.Show("已经是第一条");
//如果到了末尾则不向上移动数据项
}
else
{
txtNumber.Text = Myrec.Fields[0].Value.ToString();
//给 txtnumber.Text赋值为数据项的0单员中的内容
txtName.Text = Myrec.Fields[1].Value.ToString();
//给 txtname.Text赋值为数据项的1单员中的内容
//TxtDate.Text = Myrec.Fields[3].Value.ToString().Substring(0, 9);
//用.Substring(0, 9)方法取得时间字符串的前10位
//给 txtdate.Text赋值为数据项的3单员中的内容
//TxtTel.Text = Myrec.Fields[2].Value.ToString();
//给 txttel.Text赋值为数据项的2单员中的内容
}
}
private void BtNext_Click(object sender, System.EventArgs e)
{
//当点击下一条按钮时产生的操作
Myrec.MoveNext();
//数据项先后移动一项
if(Myrec.EOF)
{
//判断是否移动到了末尾
MessageBox.Show("已经是最后一条");
//如果到了末尾则不向下移动数据项
}
else
{
txtNumber.Text = Myrec.Fields[0].Value.ToString();
//给 txtnumber.Text赋值为数据项的0单员中的内容
txtName.Text = Myrec.Fields[1].Value.ToString();
//给 txtname.Text赋值为数据项的1单员中的内容
//TxtDate.Text = Myrec.Fields[3].Value.ToString().Substring(0, 9);
//用.Substring(0, 9)方法取得时间字符串的前10位
//给 txtdate.Text赋值为数据项的3单员中的内容
//TxtTel.Text = Myrec.Fields[2].Value.ToString();
//给 txttel.Text赋值为数据项的2单员中的内容
}
}
private void txtName_TextChanged(object sender, System.EventArgs e)
{
}
private void txtName_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(txtName.Text =="■")
{
txtName.Text = " ";
}
else
{
txtName.Text = "■";
}
}
private void btsave_Click(object sender, System.EventArgs e)
{
Myrec.Edit();
Myrec.Fields[1].Value=txtName.Text;
Myrec.Update(1,false);
}
private void button1_Click_1(object sender, System.EventArgs e)
{
Application.Exit();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
[color=FF00FF]this.timer1.Enabled = true ;
this.timer1.Interval = 1000 ;
tm01 = tm01 + 1;
if (tm01 == 60)
{
tm01 = 0;
Myrec.Edit();
Myrec.Fields[1].Value=txtName.Text;
Myrec.Update(1,false);[/color]
}
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
}
}
}