整理 .Net 2.0 下 WinForms中常用的方法(更新中...)

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif * 整理:夏荣全
InBlock.gif * 日期:2008年1月11日
InBlock.gif * 联系:lyout@live.cn
InBlock.gif * 说明:转载请注明出处
InBlock.gif * 使用:Lyout.FormMethods.方法名
ExpandedBlockEnd.gif
*/

ExpandedBlockStart.gifContractedBlock.gif
namespace  Lyout  dot.gif {
InBlock.gif    
delegate void SetTextCallback(Control control, string text);
InBlock.gif    
delegate void SetEnableCallback(Control control, bool enable);
InBlock.gif    
delegate string GetTextCallback(Control control);
InBlock.gif    
delegate void ClearItemsCallback(ComboBox comboBox);
InBlock.gif    
delegate void AddItemCallback(ComboBox comboBox, object item);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WinForm中常用的方法类
ExpandedSubBlockEnd.gif    
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif    public sealed class FormMethods dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
多线程方法#region 多线程方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置控件的Text属性的值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="control">控件的ID</param>
ExpandedSubBlockEnd.gif        
/// <param name="text">Text属性值</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void SetText(Control control, string text) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( control.InvokeRequired ) dot.gif{
InBlock.gif                SetTextCallback d 
= new SetTextCallback(SetText);
ExpandedSubBlockStart.gifContractedSubBlock.gif                control.FindForm().Invoke(d, 
new object[] dot.gif{ control, text });
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else dot.gif{
InBlock.gif                control.Text 
= text;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置控件的Enabled属性的值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="control">控件的ID</param>
ExpandedSubBlockEnd.gif        
/// <param name="enable">Enabled属性值</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void SetEnable(Control control, bool enable) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( control.InvokeRequired ) dot.gif{
InBlock.gif                SetEnableCallback d 
= new SetEnableCallback(SetEnable);
ExpandedSubBlockStart.gifContractedSubBlock.gif                control.FindForm().Invoke(d, 
new object[] dot.gif{ control, enable });
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else dot.gif{
InBlock.gif                control.Enabled 
= enable;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取控件的Text属性值
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="control">控件的ID</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static string GetText(Control control) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( control.InvokeRequired ) dot.gif{
InBlock.gif                GetTextCallback g 
= new GetTextCallback(GetText);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
return control.FindForm().Invoke(g, new object[] dot.gif{ control }).ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else dot.gif{
InBlock.gif                
return control.Text;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清空组合框包含的所有项
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="comboBox">组合框的ID</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void ClearItems(ComboBox comboBox) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( comboBox.InvokeRequired ) dot.gif{
InBlock.gif                ClearItemsCallback c 
= new ClearItemsCallback(ClearItems);
ExpandedSubBlockStart.gifContractedSubBlock.gif                comboBox.FindForm().Invoke(c, 
new object[] dot.gif{ comboBox });
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else dot.gif{
InBlock.gif                comboBox.Items.Clear();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 向组合框中增加项
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="comboBox">组合框的ID</param>
ExpandedSubBlockEnd.gif        
/// <param name="item"></param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void AddItem(ComboBox comboBox, object item) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( comboBox.InvokeRequired ) dot.gif{
InBlock.gif                AddItemCallback c 
= new AddItemCallback(AddItem);
ExpandedSubBlockStart.gifContractedSubBlock.gif                comboBox.FindForm().Invoke(c, 
new object[] dot.gif{ comboBox, item });
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else dot.gif{
InBlock.gif                comboBox.Items.Add(item);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Mdi窗口方法#region Mdi窗口方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 是否已经打开指定的子窗口
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="frmName">子窗口名称</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static bool IsFormOpened(string frmName) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( Form.ActiveForm.IsMdiContainer ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
foreach( Form form in Form.ActiveForm.MdiChildren ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if( form.Name == frmName ) dot.gif{
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 激活存在的子窗口
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="frmName">子窗口名称</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static void ActiveExistForm(string frmName) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( Form.ActiveForm.IsMdiContainer ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
foreach( Form form in Form.ActiveForm.MdiChildren ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if( form.Name == frmName ) dot.gif{
InBlock.gif                        form.Focus();
InBlock.gif                        
return;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 取得已打开的子窗口
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="frmName">子窗口的名称</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static Form GetOpenedForm(string frmName) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( Form.ActiveForm.IsMdiContainer ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
foreach( Form form in Form.ActiveForm.MdiChildren ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if( form.Name == frmName ) dot.gif{
InBlock.gif                        
return form;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


当然,跨线程也可以用 backgroundWorker 组,这就看个人所需了。

转载于:https://www.cnblogs.com/lyout/archive/2008/01/11/1034871.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值