类方面的问题

cs3005 2005-08-17 09:24:44
我想写个类,改变我窗口上的控件的属性。
如何改变

...全文
75 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cocainy 2005-08-17
  • 打赏
  • 举报
回复
using System;
using System.Windows.Forms;

namespace WindowsApp
{
/// <summary>
/// Timer 的摘要说明。
/// </summary>
public class MyTimer
{
private System.Timers.Timer theTimer;
public System.IntPtr handleTextBox;
public double Interval
{
get{return this.theTimer.Interval;}
set{this.theTimer.Interval=value;}
}
public bool Enabled
{
get{return this.theTimer.Enabled;}
set{this.theTimer.Enabled=value;}
}

public MyTimer()
{
//
// TODO: 在此处添加构造函数逻辑
//
//

//Timer
//
//
this.theTimer = new System.Timers.Timer();
this.theTimer.Interval = 1000;
this.theTimer.Enabled = false;
this.theTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.theTimer_Elapsed);
}

private void theTimer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
{
TextBox textBox = (TextBox)FrmMain.FromHandle(this.handleTextBox);
textBox.Text += e.SignalTime.ToString()+"\r\n";

//Form1.ActiveForm.Opacity = 0.50;
//Form1.ActiveForm.Top -= 10;


MessageBox.Show("时间到啦!","提醒",MessageBoxButtons.OKCancel,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1);
//创建一个线程
}
}
}
cocainy 2005-08-17
  • 打赏
  • 举报
回复
参见以下实例:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using ScheduleService;

namespace WindowsApp
{
/// <summary>
/// FrmMain 的摘要说明。
/// </summary>
public class FrmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;

public System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.Label lblTime;
private System.Windows.Forms.Button button3;
private MyTimer theTimer ;


/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public FrmMain()
{
//
// 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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.lblTime = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.theTimer = new MyTimer();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 32);
this.button1.TabIndex = 0;
this.button1.Text = "TestProcess";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 32);
this.button2.TabIndex = 1;
this.button2.Text = "TestLog";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(40, 72);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(520, 376);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// lblTime
//
this.lblTime.Location = new System.Drawing.Point(272, 16);
this.lblTime.Name = "lblTime";
this.lblTime.Size = new System.Drawing.Size(248, 23);
this.lblTime.TabIndex = 3;
//
// button3
//
this.button3.Location = new System.Drawing.Point(24, 40);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(80, 32);
this.button3.TabIndex = 4;
this.button3.Text = "Run Timer";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
//theTimer
//
this.theTimer.Enabled = false;
//
// FrmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(688, 494);
this.Controls.Add(this.button3);
this.Controls.Add(this.lblTime);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "FrmMain";
this.Text = "FrmMain";
this.Load += new System.EventHandler(this.FrmMain_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FrmMain());
}

private void button2_Click(object sender, System.EventArgs e)
{
//LogAccess.WriteLog("Test","TestButton","BeginToWrite");
lblTime.Text = DateTime.Now.ToString("yyyyMMddHHmmss");
lblTime.Text += "|"+DateTime.Now.ToString();
}

private void FrmMain_Load(object sender, System.EventArgs e)
{
//
}

private void button1_Click(object sender, System.EventArgs e)
{
ThreadManager tm=new ThreadManager();
tm.StartProcess();
}

private void button3_Click(object sender, System.EventArgs e)
{
theTimer.handleTextBox = this.textBox1.Handle;
theTimer.Interval = 2000;
theTimer.Enabled = !theTimer.Enabled;
}

}//the END of FrmMain
}//the END of NameSpace
lovewindy 2005-08-17
  • 打赏
  • 举报
回复
把引用传进去,然后在里面就像操作那个控件一样操作传进来的引用,要改什么样就改什么
jinjazz 2005-08-17
  • 打赏
  • 举报
回复
传个控件引用给你的类,然后改啊

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧