是这样我发现VS 2005中创建PDA程序时,工具箱中没有IP控件,于是自己用几个textbox和label组合出来了一个,想留着以后用,于是在VS中新建了一个用户控件,开发完了,也可以使用。代码见第一个回帖,内容太长,写这里不让发:(估计我的问题和代码没关系)就是这个文件,其他项目中要想使用,目前我只能在代码中
myIPControl = new IPControl();
myIPControl.Location = new Point(20, 20);
this.Controls.Add(myIPControl);
myIPControl.Show();这样用,我的目的是希望可以将控件放到工具箱中,这样其他人想用直接拽过来就能用,不用像现在这样要给Location属性赋值。在网上看有人说自定义的控件是DLL,可我这个是用VS向导模板生成的用户控件呀,没看到做成DLL呀,请各位回答具体点,最好能交我步骤将控件放到工具箱中。我现在项目中有三个文件IPControl.cs,IPControl.Designer.cs,IPControl.resx,别的项目中包含这三个文件后,用我前面提到的方法确实可以使用我定义的这个控件,但就是不能放到工具箱里拖拽过来用。写的太罗嗦了:-) 因为真想学这个呀,网上找好久了
myIPControl = new IPControl();
myIPControl.Location = new Point(20, 20);
this.Controls.Add(myIPControl);
myIPControl.Show();这样用,我的目的是希望可以将控件放到工具箱中,这样其他人想用直接拽过来就能用,不用像现在这样要给Location属性赋值。在网上看有人说自定义的控件是DLL,可我这个是用VS向导模板生成的用户控件呀,没看到做成DLL呀,请各位回答具体点,最好能交我步骤将控件放到工具箱中。我现在项目中有三个文件IPControl.cs,IPControl.Designer.cs,IPControl.resx,别的项目中包含这三个文件后,用我前面提到的方法确实可以使用我定义的这个控件,但就是不能放到工具箱里拖拽过来用。写的太罗嗦了:-) 因为真想学这个呀,网上找好久了
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace ViesSkyIPControl
{
public partial class IPControl : UserControl
{
public IPControl()
{
InitializeComponent();
} public override string Text
{
set
{
string[] strIP = value.Split(new char[] {'.'}); if (strIP.Length == 4)
{
if (IsIPValue(strIP[0]))
{
txtIP1.Text = strIP[0];
}
else
{
txtIP1.Text = "1";
} if (IsIPValue(strIP[1]))
{
txtIP2.Text = strIP[1];
}
else
{
txtIP2.Text = "0";
} if (IsIPValue(strIP[2]))
{
txtIP3.Text = strIP[2];
}
else
{
txtIP3.Text = "0";
} if (IsIPValue(strIP[3]))
{
txtIP4.Text = strIP[3];
}
else
{
txtIP4.Text = "0";
}
}
else
{
txtIP1.Text = "1";
txtIP2.Text = "0";
txtIP3.Text = "0";
txtIP4.Text = "0";
}
} get
{
return txtIP1.Text + "." + txtIP2.Text + "." + txtIP3.Text + "." + txtIP4.Text;
}
} public bool Enable
{
set
{
if (value)
{
txtIP1.Enabled = true;
txtIP2.Enabled = true;
txtIP3.Enabled = true;
txtIP4.Enabled = true; label1.Enabled = true;
label2.Enabled = true;
label3.Enabled = true;
}
else
{
txtIP1.Enabled = false;
txtIP2.Enabled = false;
txtIP3.Enabled = false;
txtIP4.Enabled = false; label1.Enabled = false;
label2.Enabled = false;
label3.Enabled = false;
}
} get
{
return txtIP1.Enabled;
}
} private bool IsIPValue(string strValue)
{
int ipValue; try
{
ipValue = Convert.ToInt32(strValue);
if (ipValue >= 0 && ipValue <= 255)
{
return true;
}
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
} return false;
} private void EmulateIPControl(KeyPressEventArgs e, TextBox txtFore, TextBox txtCur, TextBox txtPost)
{
try
{
if (e.KeyChar.ToString() == "." || e.KeyChar.ToString() == "。" || e.KeyChar.ToString() == " ")
{
if (txtCur.SelectedText.ToString() == "" && txtCur.Text.ToString() != "" && txtPost != null)
{
txtPost.Focus();
txtPost.SelectAll();
}
e.Handled = true;
return;
} if (e.KeyChar.ToString() == "\b")
{
if (txtCur.Text.ToString() == "" && txtFore != null)
{
txtFore.Focus();
txtFore.SelectAll();
e.Handled = true;
}
return;
}
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message);
e.Handled = true;
}
} private void txtIP1_KeyPress(object sender, KeyPressEventArgs e)
{
EmulateIPControl(e, null, txtIP1, txtIP2);
} private void txtIP2_KeyPress(object sender, KeyPressEventArgs e)
{
EmulateIPControl(e, txtIP1, txtIP2, txtIP3);
} private void txtIP3_KeyPress(object sender, KeyPressEventArgs e)
{
EmulateIPControl(e, txtIP2, txtIP3, txtIP4);
} private void txtIP4_KeyPress(object sender, KeyPressEventArgs e)
{
EmulateIPControl(e, txtIP3, txtIP4, null);
} private bool IsEligibleData(string strData, int iFromBase)
{
char[] dataArray = strData.ToCharArray();
int length = dataArray.Length; switch(iFromBase)
{
case 10: //10进制数
for (int i = 0; i < length; i++)
{
if (! (dataArray[i] <= '9' && dataArray[i] >= '0'))
{
return false;
}
}
break; default:
break;
} return true;
} private void DealIPRule(TextBox txtCur)
{
while (txtCur.Text.Length > 3)
{
txtCur.Text = txtCur.Text.Remove(txtCur.Text.Length - 1, 1);
txtCur.SelectionStart = txtCur.Text.Length;
}
txtCur.SelectionStart = txtCur.Text.Length;
if (txtCur.Text.Length > 0)
{
while (! IsEligibleData(txtCur.Text, 10) && txtCur.Text.Length > 0)
{
txtCur.Text = txtCur.Text.Remove(txtCur.Text.Length - 1, 1);
txtCur.SelectionStart = txtCur.Text.Length;
} if (txtCur.Text.Length > 0)
{
int iIP = Convert.ToInt32(txtCur.Text); if (iIP > 255)
{
txtCur.Text = txtCur.Text.Remove(txtCur.Text.Length - 1, 1);
txtCur.SelectionStart = txtCur.Text.Length; return;
}
}
}
}
{
DealIPRule(txtIP1); if (txtIP1.Text.Length > 0 && IsEligibleData(txtIP1.Text, 10))
{
int iIP = Convert.ToInt32(txtIP1.Text);
string strErrorMsg = null;
if (iIP == 127)
{
strErrorMsg = "以 127 起头的 IP 地址无效,因为它们保留用作环回地址。";
strErrorMsg += "请在 1 和 223 之间指定一些其他有效值。";
} if (iIP > 223)
{
strErrorMsg = iIP.ToString() + " 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。";
} if (strErrorMsg != null)
{
MessageBox.Show(strErrorMsg, "错误");
txtIP1.Text = "1";
if (iIP > 223)
{
txtIP1.Text = "223";
}
txtIP1.SelectionStart = txtIP1.Text.Length;
return;
} if (e.KeyCode.ToString() != "Back" && iIP > 99)
{
txtIP2.Focus();
}
}
} private void txtIP2_KeyUp(object sender, KeyEventArgs e)
{
DealIPRule(txtIP2); if (e.KeyCode.ToString() != "Back" && txtIP2.Text.Length > 2)
{
txtIP3.Focus();
}
} private void txtIP3_KeyUp(object sender, KeyEventArgs e)
{
DealIPRule(txtIP3); if (e.KeyCode.ToString() != "Back" && txtIP3.Text.Length > 2)
{
txtIP4.Focus();
}
} private void txtIP4_KeyUp(object sender, KeyEventArgs e)
{
DealIPRule(txtIP4);
} private void TrimFrontZero(TextBox txtCur)
{
if (txtCur.Text.Length > 0 && IsEligibleData(txtCur.Text, 10))
{
int iIP = Convert.ToInt32(txtCur.Text);
txtCur.Text = iIP.ToString();
}
} private void txtIP1_LostFocus(object sender, EventArgs e)
{
TrimFrontZero(txtIP1); if (txtIP1.Text.Length > 0 && IsEligibleData(txtIP1.Text, 10))
{
int iIP = Convert.ToInt32(txtIP1.Text); if (iIP == 0)
{
string strErrorMsg = "0 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。";
MessageBox.Show(strErrorMsg, "错误");
txtIP1.Text = "1";
txtIP1.SelectionStart = txtIP1.Text.Length;
txtIP1.Focus();
}
}
} private void txtIP2_LostFocus(object sender, EventArgs e)
{
TrimFrontZero(txtIP2);
} private void txtIP3_LostFocus(object sender, EventArgs e)
{
TrimFrontZero(txtIP3);
} private void txtIP4_LostFocus(object sender, EventArgs e)
{
TrimFrontZero(txtIP4);
}
}
}
然后就可以自己定制了
http://www.codeproject.com/KB/custom-controls/combobox.aspx
是一个可以进行输入的dropdownlist
这控件我经常用
using System;
using System.Web;
using System.Web.UI;namespace WishControls
{
public class ControlDemo : Control
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write ("<h1>Hello Wish</h1>");
}
}然后在自定义控件项目的相同解决方案下添加一个网站项目,并在这个网站项目中添加对控件项目的引用(类库工程此时应已编译过)或者在工具箱上点击右键,在弹出的快捷菜单上单击”选择项”, 在打开的”选择工具箱项”窗口中选择”浏览”,选择刚才生成的dll文件,IDE会自动列出此dll中的所有控件,单击”确定”后,我们所创建的自定义控件就添加到工具箱中了(此时会自动添加对控件项目的引用)。
如果是VS2003, 需要手动在工具栏中添加整个DLL库
然后右击ToolBox控件箱选择"Choose Itmes",点击浏览,找到你的Dll,确定即可导入
我只看到输出为windows应用程序,命令行应用程序和类库呀