自定義控件???

jastion 2003-10-10 12:08:18
我想自定義一個控件,而這個自定義控件想繼承某個現有的控件(如TextBox,DataGrid等),這樣我這個自定義的控件就有所繼承的控件的功能(如繼承TextBox,哪麼這個自定的控件就有TextBox所有功能)。
現在我不知如何實現(能否實現),請各位指點。
...全文
46 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sgsh51 2003-10-10
  • 打赏
  • 举报
回复
可以,因为不管是现有的控件还是自定义控件都继承自Control类,
所以肯定是可以实现的
6HZ 2003-10-10
  • 打赏
  • 举报
回复
C#提供了强大的控件开发功能,你可参照CSDN控件类开发主题。。。
qq7good 2003-10-10
  • 打赏
  • 举报
回复
我也要一个
wukun52@163.com
KimSoon98 2003-10-10
  • 打赏
  • 举报
回复
给我一个
sashilover@163.com

wideroad 2003-10-10
  • 打赏
  • 举报
回复
看不懂
superhood 2003-10-10
  • 打赏
  • 举报
回复
一个文本控件的例子,用于日期及数值的验证,很好用。但代码太长,我贴不上去,你要的话联系我,我给你发过去
EMAIL:mengqingyu@163.net
QQ:67690945
downsome 2003-10-10
  • 打赏
  • 举报
回复
一个button的例子:
Introduction
This is a short and simple demonstration of .NET framework's capability of creating custom controls.

Here I'm going to make a custom control and then, test my control in a Windows application. I have implemented some custom properties for my control, so you can learn how it is done in C#.

Building the Control
Open the Visual Studio and start a new project. Your project must be based on the Windows Control Library template. Call your project ctlCuteButton and click OK.

Once you have your project open, delete the UserControl from the project. Just remove it because the 'User Control' is not exactly what we need here.

Now go to the 'Project' menu: Project->Add User Control... and select the Custom Control template there. 'Custom Control' is what we need in this case. You may call it cuteButton. Now click OK. A new Custom control has been added to your project.

The first thing we must do here is to change the base class of the cuteButton:

Override the following line:

public class cuteButton : System.Windows.Forms.Control
by this one:

public class cuteButton : System.Windows.Forms.Button
Now your control is based on the System.Windows.Forms.Button class.

Now let's create some custom properties for our control. This is done by inserting the following code inside the cuteButton class:

private Color m_color1 = Color.LightGreen; //first color
private Color m_color2 = Color.DarkBlue; // second color
private int m_color1Transparent = 64; // transparency degree
// (applies to the 1st color)
private int m_color2Transparent = 64; // transparency degree
// (applies to the 2nd color)

public Color cuteColor1
{
get { return m_color1; }
set { m_color1 = value; Invalidate(); }
}

public Color cuteColor2
{
get { return m_color2; }
set { m_color2 = value; Invalidate(); }
}

public int cuteTransparent1
{
get { return m_color1Transparent; }
set { m_color1Transparent = value; Invalidate(); }
}

public int cuteTransparent2
{
get { return m_color2Transparent; }
set { m_color2Transparent = value; Invalidate(); }
}
The Invalidate() methoid is used to refresh the design view and all controls inside (the tip from Tom Welch).

And the last thing to do before compiling our control is to override the Paint event. So let's do it:

// Calling the base class OnPaint
base.OnPaint(pe);
// Create two semi-transparent colors
Color c1 = Color.FromArgb(m_color1Transparent , m_color1);
Color c2 = Color.FromArgb(m_color2Transparent , m_color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,
c1, c2, 10);
pe.Graphics.FillRectangle (b, ClientRectangle);
b.Dispose();
Now you may compile the control by pressing <Ctrl>+<Shift>+<B>.

Here is the complete contents of cuteButton.cs file (just in case…):

COMPLETE CODE:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;


namespace ctlCuteButton
{
/// <summary>
/// Summary description for cuteButton.
/// </summary>

public class cuteButton : System.Windows.Forms.Button
{
private Color m_color1 = Color.LightGreen; // first color
private Color m_color2 = Color.DarkBlue; // second color
private int m_color1Transparent = 64; // transparency degree
// (applies to the 1st color)
private int m_color2Transparent = 64; // transparency degree
// (applies to the 2nd color)

public Color cuteColor1
{
get { return m_color1; }
set { m_color1 = value; Invalidate(); }
}

public Color cuteColor2
{
get { return m_color2; }
set { m_color2 = value; Invalidate(); }
}

public int cuteTransparent1
{
get { return m_color1Transparent; }
set { m_color1Transparent = value; Invalidate(); }
}

public int cuteTransparent2
{
get { return m_color2Transparent; }
set { m_color2Transparent = value; Invalidate(); }
}


public cuteButton()
{
}


protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);
// Create two semi-transparent colors
Color c1 = Color.FromArgb
(m_color1Transparent , m_color1);
Color c2 = Color.FromArgb
(m_color2Transparent , m_color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush
(ClientRectangle, c1, c2, 10);
pe.Graphics.FillRectangle (b, ClientRectangle);
b.Dispose();
}
}
}
Testing the Control
Open a new instance of the VS .NET. Create a new project choosing the Windows Application template.

From a new Windows Forms project, we can add the compiled custom control to the toolbox. I do this by right-clicking the toolbox, selecting Customize Toolbox, and from the .NET Framework Components tab, clicking Browse and locating the Control Library DLL # (in our case, ctlCuteButton\bin\debug\cuteButton.dll). The component cuteButton will then appear in the Toolbox.

You can play a bit changing it’s properties (cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2).

That’s all so far about building and using custom controls.

110,499

社区成员

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

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

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