在.net应用程序中使用用户控件

做过asp.net的人都知道开发的时候使用用户控件很方便,为功能模块化提供了相当大的灵活性。开发Windows窗体也可以使用用户控件,这里我们来看看为用户控件添加属性和事件,并实现把消息发送到父容器。  

         用户控件的实现比较简单,直接从System.Windows.Forms.UserControl。  

public class UserControl1 : System.Windows.Forms.UserControl  

为了便于测试我在上面添加了一个TextBox,并注册TextBox的TextChanged事件,  

this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);  

事件处理函数,  

private void textBox1_TextChanged(object sender, System.EventArgs e)  

{  

     MessageBox.Show(this.textBox1.Text);  

}  

这里演示如果控件中文本框的内容改变就会用MessageBox显示当前的文本框内容。  

控件显示如下:  


在窗体中添加上面的用户控件,当我们改变textBox的文本时,可以看到跳出一个对话框,很简单吧。

 下面来看看对控件添加属性。
这里定义一个私有变量。  

private string customValue;  

添加访问他的属性  

public string CustomValue  

{  

     get{return customValue;}  

     set{customValue =value;}  

}  

在窗体中使用的时候像普通控件一样进行访问,  

userControl11.CustomValue = "用户控件自定义数据";  

通过事件可以传递消息到窗体上,在定义之前我们先来写一个简单的参数类。  

public class TextChangeEventArgs : EventArgs
{
	private string message;
	public TextChangeEventArgs(string message)
	{
		this.message = message;
	}
public string Message
	{
		get{return message;}
	}
}

定义委托为,  

public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);

 接下去在用户控件中添加事件, 

//定义事件 

public event TextBoxChangedHandle UserControlValueChanged;  

为了激发用户控件的新增事件,修改了一下代码,  

private void textBox1_TextChanged(object sender, System.EventArgs e)  

{  

     if(UserControlValueChanged != null)  

         UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));  

好了,为了便于在Csdn上回答问题,把完整的代码贴了出来:  

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

namespace ZZ.WindowsApplication1
{
	public class UserControl1 : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.TextBox textBox1;
		private string customValue;
		
private System.ComponentModel.Container components = null;

		public string CustomValue
		{
			get{return customValue;}
			set{customValue =value;}
		}

		//定义事件
		public event TextBoxChangedHandle UserControlValueChanged;

		public UserControl1()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region 组件设计器生成的代码
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			this.textBox1.Location = new System.Drawing.Point(12, 36);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "textBox1";
			this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
			this.Controls.Add(this.textBox1);
			this.Name = "UserControl1";
			this.Size = new System.Drawing.Size(150, 92);
			this.ResumeLayout(false);

		}
		#endregion

		private void textBox1_TextChanged(object sender, System.EventArgs e)
		{
			if(UserControlValueChanged != null)
				UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
			
		}
	}
	//定义委托
	public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);

	public class TextChangeEventArgs : EventArgs
	{
		private string message;
		public TextChangeEventArgs(string message)
		{
			this.message = message;
		}
		public string Message
		{
			get{return message;}
		}
	}
}

 使用时要在窗体中注册上面的事件,比较简单都贴源代码了,  

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

namespace ZZ.WindowsApplication1
{
	public class Form1 : System.Windows.Forms.Form
	{
		private WindowsApplication1.UserControl1 userControl11;
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			InitializeComponent();
			userControl11.CustomValue = "用户控件自定义数据";
			userControl11.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		private void InitializeComponent()
		{
			this.userControl11 = new WindowsApplication1.UserControl1();
			this.SuspendLayout();
			this.userControl11.Location = new System.Drawing.Point(8, 8);
			this.userControl11.Name = "userControl11";
			this.userControl11.Size = new System.Drawing.Size(150, 84);
			this.userControl11.TabIndex = 0;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 193);
			this.Controls.Add(this.userControl11);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		 [STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void userControl11_UserControlValueChanged(object sender, TextChangeEventArgs e)
		{
			MessageBox.Show("当前控件的值为:" + e.Message);
		}
	}
}

 另外需要动态加载,就把控件添加在容器的Controls集合就行了,下面是在构造函数中添加控件, 

public Form1()
{
	InitializeComponent();
	UserControl1 uc = new UserControl1();
	uc.CustomValue = "动态加载的用户控件";
	uc.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
	this.Controls.Add(uc);
}

另外从VS.net中的工具箱中拖动用户控件到窗体上,如果是第一次需要编译一下项目。       

### RT-DETRv3 网络结构分析 RT-DETRv3 是一种基于 Transformer 的实时端到端目标检测算法,其核心在于通过引入分层密集正监督方法以及一系列创新性的训练策略,解决了传统 DETR 模型收敛慢和解码器训练不足的问题。以下是 RT-DETRv3 的主要网络结构特点: #### 1. **基于 CNN 的辅助分支** 为了增强编码器的特征表示能力,RT-DETRv3 引入了一个基于卷积神经网络 (CNN) 的辅助分支[^3]。这一分支提供了密集的监督信号,能够与原始解码器协同工作,从而提升整体性能。 ```python class AuxiliaryBranch(nn.Module): def __init__(self, in_channels, out_channels): super(AuxiliaryBranch, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1) self.bn = nn.BatchNorm2d(out_channels) def forward(self, x): return F.relu(self.bn(self.conv(x))) ``` 此部分的设计灵感来源于传统的 CNN 架构,例如 YOLO 系列中的 CSPNet 和 PAN 结构[^2],这些技术被用来优化特征提取效率并减少计算开销。 --- #### 2. **自注意力扰动学习策略** 为解决解码器训练不足的问题,RT-DETRv3 提出了一种名为 *self-att 扰动* 的新学习策略。这种策略通过对多个查询组中阳性样本的标签分配进行多样化处理,有效增加了阳例的数量,进而提高了模型的学习能力和泛化性能。 具体实现方式是在训练过程中动态调整注意力权重分布,确保更多的高质量查询可以与真实标注 (Ground Truth) 进行匹配。 --- #### 3. **共享权重解编码器分支** 除了上述改进外,RT-DETRv3 还引入了一个共享权重的解编码器分支,专门用于提供密集的正向监督信号。这一设计不仅简化了模型架构,还显著降低了参数量和推理时间,使其更适合实时应用需求。 ```python class SharedDecoderEncoder(nn.Module): def __init__(self, d_model, nhead, num_layers): super(SharedDecoderEncoder, self).__init__() decoder_layer = nn.TransformerDecoderLayer(d_model=d_model, nhead=nhead) self.decoder = nn.TransformerDecoder(decoder_layer, num_layers=num_layers) def forward(self, tgt, memory): return self.decoder(tgt=tgt, memory=memory) ``` 通过这种方式,RT-DETRv3 实现了高效的目标检测流程,在保持高精度的同时大幅缩短了推理延迟。 --- #### 4. **与其他模型的关系** 值得一提的是,RT-DETRv3 并未完全抛弃经典的 CNN 技术,而是将其与 Transformer 结合起来形成混合架构[^4]。例如,它采用了 YOLO 系列中的 RepNCSP 模块替代冗余的多尺度自注意力层,从而减少了不必要的计算负担。 此外,RT-DETRv3 还借鉴了 DETR 的一对一匹配策略,并在此基础上进行了优化,进一步提升了小目标检测的能力。 --- ### 总结 综上所述,RT-DETRv3 的网络结构主要包括以下几个关键组件:基于 CNN 的辅助分支、自注意力扰动学习策略、共享权重解编码器分支以及混合编码器设计。这些技术创新共同推动了实时目标检测领域的发展,使其在复杂场景下的表现更加出色。 ---
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值