既然是限制输入长度 为什么不用TextBox的MaxLength属性来限制 而要做得这么复杂如果是需要判断超出长度的话 可以这样写 private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text.Length > 5)
{
//你需要的代码
//MessageBox.Show("Test");
}
}
{
if(textBox1.Text.Length > 5)
{
//你需要的代码
//MessageBox.Show("Test");
}
}
开始以为是舍去了小说,
if (s.Height * ((float) s.Width / textBox1.Width) > textBox1.Height) 这样也不行SizeF f = g.MeasureString(textBox1.Text + e.KeyChar, textBox1.Font); Size s = TextRenderer.MeasureText(textBox1.Text + e.KeyChar, textBox1.Font); 感觉都不是很准。
{
int dStrLength = Convert.ToInt32(Math.Ceiling(this.CreateGraphics().MeasureString(textBox1.Text, textBox1.Font).Width));
if (dStrLength >= textBox1.Width)
{
MessageBox.Show("超过长度");
}
}
int AllStrLength = Convert.ToInt32(Math.Ceiling(this.CreateGraphics().MeasureString(txtBoxUserName.Text, txtBoxUserName.Font).Width));
//单个字符所占的长度
int xx = Convert.ToInt32(Math.Ceiling(this.CreateGraphics().MeasureString("1", txtBoxUserName.Font).Width));
// 然后 if 一条语句..
// AllStrLength + xx > txtBoxUserName.size.width 就是超出长度了...// 判断过程 放在 textBox1_TextChanged , keydown 都可,
// 放在 textBox1_KeyPress 不准确,少算一个或多算一个字符,
public static extern bool GetCaretPos(out Point lpPoint);
try
{
int dStrLength = Convert.ToInt32(Math.Ceiling(this.CreateGraphics().MeasureString(textBox1.Text, textBox1.Font).Width));
if (dStrLength >= textBox1.Width)
{
MessageBox.Show("超过长度");
}
}
Simple Numeric TextBox
{
int index = textBox1.GetCharIndexFromPosition(new Point(0, 0));
if (index != 0)
{
MessageBox.Show("就是现在!");
}
}还有右方向键和鼠标触发的...