Timer定期变更TextBox.Text的问题 在窗体 InitializeComponent();后加 代码 注册 事件 看看this.timer_Tick += new EventHandler(timer_Tick); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 晕,你的SetText是成员函数,不是静态函数,当然直接用类名调用是不行的.要声明为static才可以. 重写吧:1、一个UserControl类的对象userControl1包含有一个TextBox类的textBox1。2、在Form1内部的private void timer_Tick(object sender, System.EventArgs e){ userControl1.textBox1.text="XXX";//成功}3、但在Form1内部的private void timer_Tick(object sender, System.EventArgs e){ ABC1.SetText("XXX");}class ABC{ public void SetText(String xxx) { userControl1.textBox1.text=xxx;//失败 }}备注:1、跟踪调试一点问题都没有,但是textBox1的Text就是不变。2、this.timer_Tick += new EventHandler(timer_Tick) 加了,事件也触发了;3、跟踪 userControl1.textBox1.text=xxx;这一条语句也执行了,XXX变量有值,但是textBox1.Text就是不变。 class ABC{ public void SetText(String xxx) { userControl1.TextBox.text=xxx;//失败了 }}再试试。理论上没有问题,另外楼上的类和实例的关系的答案似乎没有什么意义^_^ 哦,你class abc里面的userControl1是那里来的?这个东西你在这个类里面没有实例化吧。 lx1920(我思,谁在?) 厉害,确实你的类和对象没有弄清楚。你的abc里面怎么访问userControl1 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Runtime.InteropServices;namespace SysMind{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class FormMain : System.Windows.Forms.Form { SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX sendFile_ZBX1 =new SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX() ; ManageRemind mRemind; byte[] bytesSound; Icon icoSound; private const int SND_ASYNC = 0x1; private System.Windows.Forms.MenuItem menuItem_SeeMind; private System.Windows.Forms.MenuItem menuItem_CloseMind; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Panel panel_Left; private System.Windows.Forms.Label label_Top; public System.Windows.Forms.Timer timer; private const int SND_MEMORY = 0x4; /// <summary> /// 导入Sound支持Dll库 /// </summary> [DllImport("winmm.dll")] private static extern int sndPlaySoundA(byte[] lpszSoundName, int uFlags); /// <summary> /// 是否允许关闭窗口 /// </summary> private bool isClose=false; private System.Windows.Forms.ContextMenu contextMenu; private System.Windows.Forms.NotifyIcon notifyIcon; private System.ComponentModel.IContainer components; private void IniData() { notifyIcon.Icon = this.Icon; notifyIcon.ContextMenu = this.contextMenu; notifyIcon.Text = this.Text; notifyIcon.Visible = true; try { //将a1.wav添加入工程并设置为嵌入的资源 //现在是将它读入内存备用 Type t=this.GetType(); System.Reflection.Assembly a=t.Assembly; System.IO.Stream streamSound=a.GetManifestResourceStream(t.Namespace+".a1.wav"); bytesSound=new byte[streamSound.Length]; streamSound.Read(bytesSound,0, bytesSound.Length); streamSound.Close(); System.IO.Stream streamSoundPic=a.GetManifestResourceStream(t.Namespace+".sound.ico"); this.icoSound=new Icon(streamSoundPic); } catch(Exception e2) { this.notifyIcon.Text = this.Text; } RegisteRemindItem(); } public FormMain(String employee_Code) { this.mRemind=new ManageRemind(employee_Code); InitializeComponent(); IniData(); string sql=" SELECT HR_Employee.EMP_Code, HR_Employee.EMP_Name, "+ " HR_Department.DEP_Code, HR_Department.DEP_Name "+ " FROM HR_Employee INNER JOIN "+ " HR_Department ON "+ " HR_Employee.EMP_DEPCode = HR_Department.DEP_Code "; System.Data.SqlClient.SqlDataAdapter adapter=new System.Data.SqlClient.SqlDataAdapter(sql+ " where EMP_Code='"+employee_Code.Trim()+"'",Genius.LZZLS.SystemFrameworks.SystemFramework.CONNECT); DataTable table=new DataTable(); adapter.Fill(table); if(table.Rows.Count == 1) { this.label_Top.Text="当前用户:"+table.Rows[0]["EMP_Name"].ToString()+" 部门:"+table.Rows[0]["DEP_Name"].ToString(); } else { this.label_Top.Text="当前提醒用户不存在(employee_Code)"; } } /// <summary> /// 注册提醒监控项目 /// </summary> private void RegisteRemindItem() { SysMind.RemindItemList.RemindItem_OA_SendFile_SJX sendFile_SJX= new SysMind.RemindItemList.RemindItem_OA_SendFile_SJX(); sendFile_SJX.SetSubject("sdf四面推开"); this.mRemind.AddItem( new SysMind.RemindItemList.RemindItem_OA_SendFile_SJX() ); AddItem(sendFile_SJX); SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX sendFile_ZBX =new SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX() ; this.mRemind.AddItem( new SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX() ); AddItem(sendFile_ZBX); this.mRemind.AddItem( sendFile_ZBX1); AddItem(sendFile_ZBX1); } private void AddItem(System.Windows.Forms.Control c) { c.Dock=System.Windows.Forms.DockStyle.Top; this.panel_Left.Controls.Add(c); } #region 设计器生成 /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } .......................... /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new FormMain("00156")); } #endregion #region 托盘控制 /// <summary> /// 播放声音 /// </summary> private void PlaySound() { try { //播放缓存 sndPlaySoundA(bytesSound, SND_MEMORY); } catch(Exception e2) { } } /// <summary> /// 有新消息 /// </summary> private void NewMessage() { this.PlaySound(); this.notifyIcon.Icon=this.icoSound; } /// <summary> /// 双击托盘图标 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void notifyIcon_DoubleClick(object sender, System.EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Visible=true; this.WindowState = FormWindowState.Normal; this.Activate(); } else { WindowState=FormWindowState.Minimized; this.Visible=false; } } /// <summary> /// 窗口大小变更事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormMain_Resize(object sender, System.EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Visible = false; this.notifyIcon.Icon = this.Icon; } } /// <summary> /// 关闭提醒监控系统事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormMain_Closing(object sender, System.ComponentModel.CancelEventArgs e) { WindowState=FormWindowState.Minimized; this.Visible=false; this.notifyIcon.Icon = this.Icon; e.Cancel=!this.isClose; } /// <summary> /// 打开提醒监控系统 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuItem_SeeMind_Click(object sender, System.EventArgs e) { this.Visible=true; this.WindowState = FormWindowState.Normal; this.Activate(); } /// <summary> /// 关闭提醒监控系统 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuItem_CloseMind_Click(object sender, System.EventArgs e) { this.isClose=true; this.Close(); } #endregion /// <summary> /// 时钟定期事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Tick(object sender, System.EventArgs e) { lock(this) { sendFile_ZBX1.SetSubject("xxxx"); this.mRemind.RefurbishRemindData(); } } }} DirectDraw绘了花屏出来... Exchange 2007 EWS COM 模块, 有人需要吗? c# 语音包 线程同步中 发生死锁的 条件是不是至少要两把锁 想跳槽,求大家出意见。 DOTNET三层框架 C#可以用非托管的形似调用C# 自己写的动态链接库么? C# 如何利用combobox实现类似效果 初学者请教C#队列类的代码分析 [散分]请高手比较NetAdvantage和ComponentOne控件 新手提问:如何让两个WINFORM使用同一个DATASET,以显示相同的数据? 初级问:怎样删除datagrid中的一行?
要声明为static才可以.
1、一个UserControl类的对象userControl1包含有一个TextBox类的textBox1。
2、在Form1内部的
private void timer_Tick(object sender, System.EventArgs e)
{
userControl1.textBox1.text="XXX";//成功
}3、但在Form1内部的
private void timer_Tick(object sender, System.EventArgs e)
{
ABC1.SetText("XXX");
}class ABC
{
public void SetText(String xxx)
{
userControl1.textBox1.text=xxx;//失败
}
}
备注:
1、跟踪调试一点问题都没有,但是textBox1的Text就是不变。
2、this.timer_Tick += new EventHandler(timer_Tick) 加了,事件也触发了;
3、跟踪 userControl1.textBox1.text=xxx;这一条语句也执行了,XXX变量有值,但是textBox1.Text就是不变。
{
public void SetText(String xxx)
{
userControl1.TextBox.text=xxx;//失败了
}
}
再试试。理论上没有问题,另外楼上的类和实例的关系的答案似乎没有什么意义^_^
这个东西你在这个类里面没有实例化吧。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;namespace SysMind
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FormMain : System.Windows.Forms.Form
{
SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX sendFile_ZBX1 =new SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX() ; ManageRemind mRemind;
byte[] bytesSound;
Icon icoSound;
private const int SND_ASYNC = 0x1;
private System.Windows.Forms.MenuItem menuItem_SeeMind;
private System.Windows.Forms.MenuItem menuItem_CloseMind;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Panel panel_Left;
private System.Windows.Forms.Label label_Top;
public System.Windows.Forms.Timer timer;
private const int SND_MEMORY = 0x4; /// <summary>
/// 导入Sound支持Dll库
/// </summary>
[DllImport("winmm.dll")]
private static extern int sndPlaySoundA(byte[] lpszSoundName, int uFlags);
/// <summary>
/// 是否允许关闭窗口
/// </summary>
private bool isClose=false; private System.Windows.Forms.ContextMenu contextMenu;
private System.Windows.Forms.NotifyIcon notifyIcon;
private System.ComponentModel.IContainer components; private void IniData()
{
notifyIcon.Icon = this.Icon;
notifyIcon.ContextMenu = this.contextMenu;
notifyIcon.Text = this.Text;
notifyIcon.Visible = true;
try
{
//将a1.wav添加入工程并设置为嵌入的资源
//现在是将它读入内存备用
Type t=this.GetType();
System.Reflection.Assembly a=t.Assembly;
System.IO.Stream streamSound=a.GetManifestResourceStream(t.Namespace+".a1.wav");
bytesSound=new byte[streamSound.Length];
streamSound.Read(bytesSound,0, bytesSound.Length);
streamSound.Close(); System.IO.Stream streamSoundPic=a.GetManifestResourceStream(t.Namespace+".sound.ico");
this.icoSound=new Icon(streamSoundPic);
}
catch(Exception e2)
{
this.notifyIcon.Text = this.Text; } RegisteRemindItem();
}
public FormMain(String employee_Code)
{
this.mRemind=new ManageRemind(employee_Code);
InitializeComponent();
IniData(); string sql=" SELECT HR_Employee.EMP_Code, HR_Employee.EMP_Name, "+
" HR_Department.DEP_Code, HR_Department.DEP_Name "+
" FROM HR_Employee INNER JOIN "+
" HR_Department ON "+
" HR_Employee.EMP_DEPCode = HR_Department.DEP_Code ";
System.Data.SqlClient.SqlDataAdapter adapter=new System.Data.SqlClient.SqlDataAdapter(sql+ " where EMP_Code='"+employee_Code.Trim()+"'",Genius.LZZLS.SystemFrameworks.SystemFramework.CONNECT);
DataTable table=new DataTable();
adapter.Fill(table);
if(table.Rows.Count == 1)
{
this.label_Top.Text="当前用户:"+table.Rows[0]["EMP_Name"].ToString()+" 部门:"+table.Rows[0]["DEP_Name"].ToString();
}
else
{
this.label_Top.Text="当前提醒用户不存在(employee_Code)";
}
} /// <summary>
/// 注册提醒监控项目
/// </summary>
private void RegisteRemindItem()
{
SysMind.RemindItemList.RemindItem_OA_SendFile_SJX sendFile_SJX= new SysMind.RemindItemList.RemindItem_OA_SendFile_SJX();
sendFile_SJX.SetSubject("sdf四面推开");
this.mRemind.AddItem( new SysMind.RemindItemList.RemindItem_OA_SendFile_SJX() );
AddItem(sendFile_SJX); SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX sendFile_ZBX =new SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX() ;
this.mRemind.AddItem( new SysMind.RemindItemList.RemindItem_OA_SendFile_ZBX() );
AddItem(sendFile_ZBX); this.mRemind.AddItem( sendFile_ZBX1);
AddItem(sendFile_ZBX1); } private void AddItem(System.Windows.Forms.Control c)
{
c.Dock=System.Windows.Forms.DockStyle.Top;
this.panel_Left.Controls.Add(c);
}
#region 设计器生成
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} ..........................
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FormMain("00156"));
}
#endregion #region 托盘控制
/// <summary>
/// 播放声音
/// </summary>
private void PlaySound()
{
try
{
//播放缓存
sndPlaySoundA(bytesSound, SND_MEMORY);
}
catch(Exception e2)
{
}
}
/// <summary>
/// 有新消息
/// </summary>
private void NewMessage()
{
this.PlaySound();
this.notifyIcon.Icon=this.icoSound;
}
/// <summary>
/// 双击托盘图标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_DoubleClick(object sender, System.EventArgs e)
{ if (this.WindowState == FormWindowState.Minimized)
{
this.Visible=true;
this.WindowState = FormWindowState.Normal;
this.Activate();
}
else {
WindowState=FormWindowState.Minimized;
this.Visible=false;
} }
/// <summary>
/// 窗口大小变更事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMain_Resize(object sender, System.EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Visible = false;
this.notifyIcon.Icon = this.Icon;
}
} /// <summary>
/// 关闭提醒监控系统事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
WindowState=FormWindowState.Minimized;
this.Visible=false;
this.notifyIcon.Icon = this.Icon;
e.Cancel=!this.isClose;
} /// <summary>
/// 打开提醒监控系统
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem_SeeMind_Click(object sender, System.EventArgs e)
{
this.Visible=true;
this.WindowState = FormWindowState.Normal;
this.Activate();
} /// <summary>
/// 关闭提醒监控系统
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem_CloseMind_Click(object sender, System.EventArgs e)
{
this.isClose=true;
this.Close();
}
#endregion /// <summary>
/// 时钟定期事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer_Tick(object sender, System.EventArgs e)
{
lock(this)
{
sendFile_ZBX1.SetSubject("xxxx");
this.mRemind.RefurbishRemindData();
}
}
}
}