C#中如何实现文本控件的格式!
发布时间:2011-06-21 11:33:38 文章来源:www.iduyao.cn 采编人员:星星草
C#中怎么实现文本控件的格式!!!??
形如——————————
| . . . |这样格式的文本控件怎么实现?就是输入IP地址的那一栏!
——————————
------解决方案--------------------
private void MaskIpAddr( System.Windows.Forms.TextBox textBox, KeyPressEventArgs e)
{
int len = textBox.Text.Length;
if(Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8)
{
if (e.KeyChar != 8)
{
if(len == 2 && e.KeyChar != '.')
{
string tmp = textBox.Text + e.KeyChar;
if( textBox.Name == "textBox1" )
{
if(Int32.Parse(tmp)> 223) // 进行验证
{
MessageBox.Show( tmp + " 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。");
textBox.Text = "223";
textBox.Focus();
return;
}
textBox2.Focus();
textBox2.SelectAll();
}
else if( textBox.Name == "textBox2" )
{
if(Int32.Parse(tmp)> 255)
{
MessageBox.Show( tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}
textBox3.Focus();
textBox3.SelectAll();
}
else if( textBox.Name == "textBox3" )
{
if(Int32.Parse(tmp)> 255)
{
MessageBox.Show( tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}
textBox4.Focus();
textBox4.SelectAll();
}
else if( textBox.Name == "textBox4" )
{
if(Int32.Parse(tmp)> 255)
{
MessageBox.Show( tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}
}
}
if(e.KeyChar == '.')
{
if( textBox.Name == "textBox1" && textBox.Text != "" )
{
textBox2.Focus();
textBox2.SelectAll();
}
if( textBox.Name == "textBox2" && textBox.Text != "" )
{
textBox3.Focus();
textBox3.SelectAll();
}
if( textBox.Name == "textBox3" && textBox.Text != "" )
{
textBox4.Focus();
textBox4.SelectAll();
}
if( textBox.Name == "textBox4" && textBox.Text != "" )
{
}
e.Handled = true;
}
}
else
{
if( textBox.Name == "textBox1" && textBox.Text == "")
{
}
if( textBox.Name == "textBox2" && textBox.Text == "")
{
textBox1.Focus();
textBox1.SelectionStart = textBox1.Text.Length;
}
if( textBox.Name == "textBox3" && textBox.Text == "")
{
textBox2.Focus();
textBox2.SelectionStart = textBox2.Text.Length;
}
if( textBox.Name == "textBox4" && textBox.Text == "")
{
textBox3.Focus();
textBox3.SelectionStart = textBox3.Text.Length;
}
e.Handled = false;
}
}
else
e.Handled = true;
}
[Browsable(true)]
/// <summary>
/// 获取 IpBox 的文本。
/// </summary>
public new string Text
{
get
{
if( textBox1.Text == ""
|| textBox2.Text == ""
|| textBox3.Text == ""
|| textBox4.Text == "")
{
_text = "";
return _text;
}
else
{
_text = Convert.ToInt32(textBox1.Text).ToString() + "." + Convert.ToInt32(textBox2.Text).ToString() + "." + Convert.ToInt32(textBox3.Text).ToString() + "." + Convert.ToInt32(textBox4.Text).ToString();
return _text;
}
}
/* set //
{
if(value != null)
{
// 255-255-255-255
//string pattern = @"^([1-9]|d{2}|1[0-9]{1,2}|2[0-5]{2}).(d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2}).(d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2}).(d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
C#读取文本并入库
文本格式固定如下:
**********************:
<中文作者一>=赵逢玲
<中文作者二>=杜秀华
<中文作者三>=王翠东
<中...
-
C#操作Word文档(Office 2007)
首先引入类库,Microsoft.Office.Interop.Word,然后进行编程。代码如下:
using ...
-
请问,ReadLine方法读取文件
一个txt文件,有两行
aaaa
bbbb
代码:
string s1 = @"D:iosamplepp.txt";
using (StreamReader sr = new...
-
验证控件
在gridview页脚行添加了验证控件,可是点击编辑更新时触发了验证事件,无法更新,怎么办?
------解决方案-------------------...
-
在BITMAP上用GDI+画图,如何使画出的图像尽可能清晰?
我在一张bitmap上用GDI+绘制了简单的多边形,并用PictureBox显示。
我发现,如果我...
-
???如何利用webbrower获取ifream元素????
如何利用webbrower获取ifream元素.我在GOOGLE,BAIDU一整天依然无法找到解决方法!!希望各...
-
如何解决“已有打开的与此命令相关联的DataReader,必须首先将它关闭。”错误?
小弟写了一个DBHelper类来控制数据库访问,但是在实际...
-
在做asp.net项目时,在做数据库更新时出现',' 附近有语法错误。(数据库是用sql2000的)
数据库更新语句:
public void Update()
...
-
为什么我这段代码不会发出声音呢?
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
usi...
-
ClickOnce 不支持请求执行级别requireAdministrator
写了一个应用程序,使用ServiceController控制 windows服务的的启动和停止。
...