大家好,一个问题一直困扰着小弟,就是c#中组件和控件的区别,但从visual studio中看表面的区别就是组件不能提供界面,而控件是有设计界面的,根据我在网上查询的资料获知:在编程领域内,组件其实就是一个可以复用并可以和其它对象交互的对象,而控件就是一个提供或实现了图形界面的组件,其实我感觉说的已经挺清楚了,但是我还是理解不透:1、什么时候应该选择使用组件呢,什么时候选择使用控件呢?2、就组件的可复用来说,用一个普通的类完全可以做到的啊,感觉组件类比普通类稍强一点的地方就是能够做到从vs的工具栏中拖拽到界面上,但是本质还是一个类的引用啊,为什么会出现组件呢?3、对实现同一个功能,控件是不是比组件耗费内存要多呢?希望懂的给个提示,小弟是个新手,所以问的问题难免有些肤浅,请大家见谅,谢谢啦,希望能用具体的例子加理论给些指点,我还有还来新手们不胜感激
解决方案 »
- 求助,类似QQ上卷程序,重复上卷下卷问题。
- 怎么在picturebox控件上显示动态画面,在线等,急......
- ~C#备份数据库~急救!
- 有什么办法可以去掉这个窗口在任务栏上的图标呢?
- 请教关于程序死机的故障
- 50分求excel和文本文件倒入数据到acess数据库中(都是固定格式)
- 在Windows Form的TreeView控件中如何捕捉右键点击TreeNode的事件?
- 求助 System.ExecutionEngineException 的问题
- WinForm中如何达到主从表显示效果?
- 如何一登陆界面就让一个文本框获得输入焦点?
- (效率)(效率)如何判断泛型list中是否有相同的元素?怎么去除相同的元素只保留一个
- 求~SQL数据库里面的一张表转换成XML文件的源代码
控件paint肯定比组件费时间
[ToolboxItem(true)]
public partial class Station : Control
{
public Station()
{
InitializeComponent();
System.Resources.ResourceManager rm =
new System.Resources.ResourceManager("Ark.Controls.Business.Station",
System.Reflection.Assembly.GetExecutingAssembly());
try
{
standardImage = (Bitmap)rm.GetObject("StandImage");
//TODO: if other tickets
}
catch
{ }
} protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
} Bitmap standardImage;
public Bitmap StandardImage
{
get { return standardImage; }
set { standardImage = value; }
} Bitmap activeImage;
public Bitmap ActiveImage
{
get { return activeImage; }
set { activeImage = value; }
} private Int32 x1 = 0;
private Int32 y1 = 0;
private List<MapIndicator> mapIndicators = new List<MapIndicator>();
private List<Point> points = new List<Point>(); protected override void OnMouseMove(MouseEventArgs e)
{
this.Cursor = System.Windows.Forms.Cursors.SizeAll;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
x1 = e.X;
y1 = e.Y;
}
base.OnMouseMove(e);
} protected override void OnMouseUp(MouseEventArgs mevent)
{
if (x1 != 0 && y1 != 0)
{
this.Left = this.Left + x1;
this.Top = this.Top + y1;
}
base.OnMouseUp(mevent);
} protected override void OnMouseDoubleClick(MouseEventArgs e)
{
if (mapIndicators.Count == 0)
{
points.Add(new Point(this.Left, this.Top));
MapIndicator one = new MapIndicator(points[0]);
one.Show();
mapIndicators.Add(one);
}
base.OnMouseDoubleClick(e);
}
public partial class LocationIndicator : UserControl
{
public LocationIndicator()
{
InitializeComponent();
this.exLabel1.Size = new Size(57, 21);
ChangeStatusSwitch = true;
this.pictureBox1.Visible = true;
this.mapIndicator1.Visible = false;
this.exLabel1.Visible = false;
this.ContextMenuStrip = this.contextMenuStrip1;
}
public LocationIndicator(Point LocationOfStation,Point LocationOfMap)
: this()
{
try
{
StationLocation = LocationOfStation;
MapLocation = LocationOfMap;
this.Left = StationLocation.X;
this.Top = StationLocation.Y;
this.mapIndicator1.usercontrollocation = LocationOfStation;
}
catch
{
}
} public LocationIndicator(Point LocationOfStation, Point LocationOfMap, String sinformation)
: this()
{
StationLocation = LocationOfStation;
MapLocation = LocationOfMap;
this.Left = StationLocation.X;
this.Top = StationLocation.Y;
this.Information = sinformation;
ChangeStatusSwitch = false; }
public Point StationLocation;
private Point MapLocation;
public String information;
public EventHandler OperationAdd;
public EventHandler OperationDelete; public String Information
{
get { return information; }
set { information = value; }
} private Int32 x1 = 0;
private Int32 y1 = 0;
private Int32 x2 = 0;
private Int32 y2 = 0; private bool changeStatusSwitch; public bool ChangeStatusSwitch
{
get { return changeStatusSwitch; }
set { changeStatusSwitch = value; }
}
public void ChangeStatus()
{
if (ChangeStatusSwitch == true)
{
this.pictureBox1.Image = global::Ark.Controls.Business.Properties.Resources.StandardImage;
this.mapIndicator1.Visible = true;
this.mapIndicator1.Size = new Size(28, 21);
this.exLabel1.Visible = false;
if (this.Information != null)
{
this.mapIndicator1.Text = this.Information;
}
ChangeStatusSwitch = false;
this.mapIndicator1.Focus();
}
else
{
this.pictureBox1.Image = global::Ark.Controls.Business.Properties.Resources.ActiveImage;
this.mapIndicator1.Visible = false;
this.exLabel1.Visible = true;
this.exLabel1.Text = this.Information;
ChangeStatusSwitch = true;
}
} private void LocationIndicator_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (this.mapIndicator1.Text == String.Empty)
{
MessageBox.Show("not empty");
return;
}
this.Information = this.mapIndicator1.Text;
this.ChangeStatus();
if (this.OperationAdd != null)
{
EventHandler tmp = this.OperationAdd;
tmp(sender, null);
}
}
} private void LocationIndicator_MouseMove(object sender, MouseEventArgs e)
{
this.Cursor = System.Windows.Forms.Cursors.SizeAll;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
x1 = e.X;
y1 = e.Y;
}
} private void LocationIndicator_MouseUp(object sender, MouseEventArgs e)
{
x2 = e.X;
y2 = e.Y; if (e.Button == MouseButtons.Left)
{ if (x1 != 0 && y1 != 0)
{
this.Left = this.Left + x2 - x1;
this.Top = this.Top + y2 - y1;
}
if (x2 != x1 && y2 != y1)
{
this.ChangeStatus();
}
}
} private void LocationIndicator_DoubleClick(object sender, EventArgs e)
{
this.ChangeStatus();
} void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
Control c = contextMenuStrip1.SourceControl as Control;
contextMenuStrip1.Items.Clear();
contextMenuStrip1.Items.Add("Edit");
contextMenuStrip1.Items.Add("-");
contextMenuStrip1.Items.Add("Delete");
e.Cancel = false;
} private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.ChangeStatus();
} public void editToolStripMenuItem_Click(object sender, EventArgs e)
{
this.ChangeStatus();
} public void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Dispose();
if (this.OperationDelete != null)
{
EventHandler tmp = this.OperationDelete;
tmp(sender, null);
}
}
}
但是具体在用时有什么区别呢?这主要是从设计的角度去考虑的。
组件和一般类的区别:我们总是习惯于面向对象的设计方式。而对象则是由类实例化而来。我们强调万事万物皆对象,把对象看作设计的基本单元。
所谓组件其实是面向组件的设计方式,我们把组件作为设计的基本单元。组件即为程序的组成部分。其实是构成程序的一个最基本的单元。一个组件其实可以包括多个类。这与传统的面向对象的设计方式有点区别。算是更高一层的封装。
而控件和用户控件其实是组件按照具体的用途的一种划分,它们都是组件。控件的突出特点就是交互式组件。而用户控件则是将某些特定的组件或控件复合从而实现特定的业务功能。所以用户控件往往是从业务角度来设计的。
其实,程序设计现在已经不在是简单的面向对象。
从以往的面向过程到
面向对象
面向组件
面向服务
面向方向
其实有很多。