c#,winform,UserContrl,用户控件开发,窗体

本文介绍了一个用户控件的设计与实现,该控件能够响应文本框内容的变化,并触发相应的事件。通过设置属性,可以获取和设置地址及编号等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 66.JPG
用户控件效果如上图
代码如下:
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Drawing;
None.gif
using System.Data;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
None.gif
namespace WindowsControlLibrary1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class UserControl1 : UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public event EventHandler AddressLine1Changed;
InBlock.gif        
public event EventHandler AddressLine2Changed;
InBlock.gif        
public event EventHandler ZoneChanged;
InBlock.gif        
public event EventHandler NumberChanged;
InBlock.gif
InBlock.gif        
private void TextBoxControls_TextChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (((TextBox)sender).Name)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "txtAddress1":
InBlock.gif                    
if (AddressLine1Changed != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        AddressLine1Changed(
this, EventArgs.Empty);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
InBlock.gif                
case "txtAddress2":
InBlock.gif                    
if (AddressLine2Changed != null)
InBlock.gif                        AddressLine2Changed(
this, EventArgs.Empty);
InBlock.gif                    
break;
InBlock.gif                
case "txtZone":
InBlock.gif                    
if (ZoneChanged != null)
InBlock.gif                        ZoneChanged(
this, EventArgs.Empty);
InBlock.gif                    
break;
InBlock.gif                
case "txtNumber":
InBlock.gif                    
if (NumberChanged != null)
InBlock.gif                        NumberChanged(
this, EventArgs.Empty);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Category(
"NumberData"), DescriptionAttribute("Gets or sets the Number value"), DefaultValue("")]
InBlock.gif        
public string Number
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this.txtNumber .Text; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (txtNumber.Text != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    txtNumber.Text 
= value;
InBlock.gif                    
if (NumberChanged != null)
InBlock.gif                        NumberChanged(
this, PropertyChangedEventArgs.Empty);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Category(
"AddressData"), DescriptionAttribute("Gets or sets the AddressLine1 value"), DefaultValue("")]
InBlock.gif        
public string AddressLine1
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn txtAddress1.Text; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (txtAddress1.Text != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    txtAddress1.Text 
= value;
InBlock.gif                    
if (AddressLine1Changed != null)
InBlock.gif                        AddressLine1Changed(
this, PropertyChangedEventArgs.Empty);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Category(
"AddressData"), DescriptionAttribute("Gets or sets the AddressLine2 value"), DefaultValue("")]
InBlock.gif        
public string AddressLine2
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn txtAddress2.Text; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (txtAddress2.Text != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    txtAddress2.Text 
= value;
InBlock.gif                    
if (AddressLine2Changed != null)
InBlock.gif                        AddressLine2Changed(
this, PropertyChangedEventArgs.Empty);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        [Category(
"ZoneData"), DescriptionAttribute("Gets or sets the Zone value"), DefaultValue("")]
InBlock.gif        
public string Zone
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn txtZone.Text; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (txtZone.Text != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    txtZone.Text 
= value;
InBlock.gif                    
if (ZoneChanged!= null)
InBlock.gif                        ZoneChanged(
this, PropertyChangedEventArgs.Empty);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
InBlock.gif        
public UserControl1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值