110,535
社区成员
发帖
与我相关
我的任务
分享
<html>
<head>
<title>得到焦点与失去焦点事件</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<script type="text/javascript" language="javascript">
<!--
//清除姓名框
function clearText()
{
myForm.myName.value = "";
}
//统计字数
function textCount()
{
alert("您一共输入了"+myForm.myText.value.length+"个字");
}
-->
</script>
</head>
<body>
<form name="myForm">
姓名:<input name="myName" type="text" onfocus="clearText()"
value="张三"><br>
建议:<textarea name="myText" onblur="textCount()"></textarea>
</form>
</body>
</html>
using System;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.txt2.TextChanged += new System.EventHandler(this.txt2_TextChanged);
this.txt1.TextChanged += new System.EventHandler(this.txt1_TextChanged);
}
private void txt1_TextChanged(object sender, EventArgs e)
{
try
{
this.txt2.TextChanged -= new System.EventHandler(this.txt2_TextChanged);
txt2.Text = (int.Parse(txt1.Text) * 2).ToString();
}
catch { }
finally
{
this.txt2.TextChanged += new System.EventHandler(this.txt2_TextChanged);
}
}
private void txt2_TextChanged(object sender, EventArgs e)
{
try
{
this.txt1.TextChanged -= new System.EventHandler(this.txt1_TextChanged);
txt1.Text = (int.Parse(txt2.Text) * 2).ToString();
}
catch { }
finally
{
this.txt1.TextChanged += new System.EventHandler(this.txt1_TextChanged);
}
}
}
}
namespace test
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txt1 = new System.Windows.Forms.TextBox();
this.txt2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txt1
//
this.txt1.Location = new System.Drawing.Point(108, 29);
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(100, 21);
this.txt1.TabIndex = 0;
//
// txt2
//
this.txt2.Location = new System.Drawing.Point(108, 69);
this.txt2.Name = "txt2";
this.txt2.Size = new System.Drawing.Size(100, 21);
this.txt2.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(376, 203);
this.Controls.Add(this.txt2);
this.Controls.Add(this.txt1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txt1;
private System.Windows.Forms.TextBox txt2;
}
}
using System;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.txt2.TextChanged += new System.EventHandler(this.txt2_TextChanged);
this.txt1.TextChanged += new System.EventHandler(this.txt1_TextChanged);
}
private void txt1_TextChanged(object sender, EventArgs e)
{
try
{
this.txt2.TextChanged -= new System.EventHandler(this.txt2_TextChanged);
txt2.Text = (int.Parse(txt1.Text) * 2).ToString();
this.txt2.TextChanged += new System.EventHandler(this.txt2_TextChanged);
}
catch { }
}
private void txt2_TextChanged(object sender, EventArgs e)
{
try
{
this.txt1.TextChanged -= new System.EventHandler(this.txt1_TextChanged);
txt1.Text = (int.Parse(txt2.Text) * 2).ToString();
this.txt1.TextChanged += new System.EventHandler(this.txt1_TextChanged);
}
catch { }
}
}
}