C#:向控件添加信息类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms; namespace MyWifi
{
public class ListBoxLogs
{
private delegate void AddCtrlValueHandler(Control ctrl, string value);
private delegate void ChangeComboBoxValueHandler(ComboBox ctrl);
private delegate void SetCtrlEnableHandler(Control ctrl, bool value);
private delegate void SetCtrlValueHandler(Control ctrl, string value); public static void AddCtrlValue(Form parentForm, Control ctrl, string value)
{
if (parentForm.InvokeRequired)
{
AddCtrlValueHandler method = new AddCtrlValueHandler(AddCtrlValueMethod);
parentForm.BeginInvoke(method, new object[] { ctrl, value });
}
else
{
AddCtrlValueMethod(ctrl, value);
}
} private static void AddCtrlValueMethod(Control ctrl, string value)
{
if (ctrl is TextBox)
{
TextBox box = ctrl as TextBox;
box.Text = box.Text + value;
}
else if (ctrl is Label)
{
Label label = ctrl as Label;
label.Text = label.Text + value;
}
else if (ctrl is ListBox)
{
ListBox listbox = ctrl as ListBox;
if (listbox.Items.Count > )
{
listbox.Items.Clear();
}
listbox.Items.Add(value);
if (listbox.Items.Count > )
{
listbox.SelectedIndex = (listbox.Items.Count - );
}
}
else if (ctrl is RichTextBox)
{
RichTextBox richtextbox = ctrl as RichTextBox;
richtextbox.Text += value + "\r\n";
if (richtextbox.Text.Length > )
{
richtextbox.Text = string.Empty;
}
} } public static void ChangeComboBoxValue(Form parentForm, ComboBox ctrl)
{
if (parentForm.InvokeRequired)
{
ChangeComboBoxValueHandler method = new ChangeComboBoxValueHandler(ChangeComboBoxValueMethod);
parentForm.BeginInvoke(method, new object[] { ctrl });
}
else
{
ChangeComboBoxValueMethod(ctrl);
}
} private static void ChangeComboBoxValueMethod(ComboBox ctrl)
{
if (ctrl.Items.Count > )
{
if (ctrl.SelectedIndex == )
{
ctrl.SelectedIndex = ;
}
else
{
ctrl.SelectedIndex = ;
}
}
} public static void SetCtrlEnable(Form parentForm, Control ctrl, bool value)
{
if (parentForm.InvokeRequired)
{
SetCtrlEnableHandler method = new SetCtrlEnableHandler(SetCtrlEnableMethod);
parentForm.BeginInvoke(method, new object[] { ctrl, value });
}
else
{
SetCtrlEnableMethod(ctrl, value);
}
} public static void SetCtrlEnable(UserControl parentCtrl, Control ctrl, bool value)
{
if (parentCtrl.InvokeRequired)
{
SetCtrlEnableHandler method = new SetCtrlEnableHandler(SetCtrlEnableMethod);
parentCtrl.BeginInvoke(method, new object[] { ctrl, value });
}
else
{
SetCtrlEnableMethod(ctrl, value);
}
} private static void SetCtrlEnableMethod(Control ctrl, bool value)
{
//if (ctrl is TextBox)
//{
// TextBox box = ctrl as TextBox;
// box.Enabled = value;
//}
//if (ctrl is ComboBox)
//{
// ComboBox box2 = ctrl as ComboBox;
// box2.Enabled = value;
//}
//if (ctrl is Label)
//{
// Label label = ctrl as Label;
// label.Enabled = value;
//}
//if (ctrl is Button)
//{
// Button button = ctrl as Button;
// button.Enabled = value;
//}
//if (ctrl is NumericUpDown)
//{
// NumericUpDown down = ctrl as NumericUpDown;
// down.Enabled = value;
//}
//if (ctrl is Form)
//{
// Form form = ctrl as Form;
// form.Enabled = value;
//}
////if (ctrl is IPTextBox)
////{
//// IPTextBox box3 = ctrl as IPTextBox;
//// box3.Enabled = value;
////}
//if (ctrl is GroupBox)
//{
// GroupBox box4 = ctrl as GroupBox;
// box4.Enabled = value;
//}
//if (ctrl is CheckBox)
//{
// CheckBox box5 = ctrl as CheckBox;
// box5.Enabled = value;
//}
try
{
ctrl.Enabled = value;
}
catch { }
} public static void SetCtrlValue(Form parentForm, Control ctrl, string value)
{
if (parentForm.InvokeRequired)
{
SetCtrlValueHandler method = new SetCtrlValueHandler(SetCtrlValueMethod);
parentForm.BeginInvoke(method, new object[] { ctrl, value });
}
else
{
SetCtrlValueMethod(ctrl, value);
}
} public static void SetCtrlValue(UserControl parentCtrl, Control ctrl, string value)
{
if (parentCtrl.InvokeRequired)
{
SetCtrlValueHandler method = new SetCtrlValueHandler(SetCtrlValueMethod);
parentCtrl.BeginInvoke(method, new object[] { ctrl, value });
}
else
{
SetCtrlValueMethod(ctrl, value);
}
} private static void SetCtrlValueMethod(Control ctrl, string value)
{
if (ctrl is TextBox)
{
TextBox box = ctrl as TextBox;
box.Text = value;
}
else if (ctrl is ComboBox)
{
ComboBox box2 = ctrl as ComboBox;
try
{
int selIndex = ;
try
{
selIndex = int.Parse(value);
if (selIndex < box2.Items.Count - )
{
box2.SelectedIndex = selIndex;
}
else
{
box2.SelectedIndex = box2.FindString(value);
}
}
catch
{
box2.SelectedIndex = box2.FindString(value);
} }
catch (Exception exception)
{
//LogFile.Log.Debug(exception.Message);
}
}
else if (ctrl is Label)
{
Label label = ctrl as Label;
label.Text = value;
}
else if (ctrl is Button)
{
Button button = ctrl as Button;
button.Text = value;
}
else if (ctrl is NumericUpDown)
{
NumericUpDown down = ctrl as NumericUpDown;
down.Value = int.Parse(value);
}
else if (ctrl is Form)
{
Form form = ctrl as Form;
form.Text = value;
}
else if (ctrl is ProgressBar)
{
ProgressBar bar = ctrl as ProgressBar;
bar.Value = int.Parse(value);
}
else if (ctrl is CheckBox)
{
try
{
CheckBox cb = ctrl as CheckBox;
cb.Checked = bool.Parse(value);
}
catch
{
}
}
else
{
ctrl.Text = value;
}
} private delegate void SetCtrlVisibleHandler(Control ctrl, bool value);
public static void SetCtrlVisible(Form parentForm, Control ctrl, bool value)
{
if (parentForm.InvokeRequired)
{
SetCtrlVisibleHandler method = new SetCtrlVisibleHandler(SetCtrlVisibleMethod);
parentForm.BeginInvoke(method, new object[] { ctrl, value });
}
else
{
SetCtrlVisibleMethod(ctrl, value);
}
} private static void SetCtrlVisibleMethod(Control ctrl, bool value)
{
try
{
ctrl.Visible = value;
}
catch { }
} private delegate void SetCtrlTagHandler(Control ctrl, string value);
public static void SetCtrlTag(Form parentForm, Control ctrl, string value)
{
if (parentForm.InvokeRequired)
{
SetCtrlTagHandler method = new SetCtrlTagHandler(SetCtrlTagMethod);
parentForm.BeginInvoke(method, new object[] { ctrl, value });
}
else
{
SetCtrlTagMethod(ctrl, value);
}
} private static void SetCtrlTagMethod(Control ctrl, string value)
{
try
{
ctrl.Tag = value;
}
catch { }
}
}
}
C#:向控件添加信息类的更多相关文章
- 关于在MFC的视图类里面添加各种控件 以及给这些控件添加对用的函数。2015-03-24 13:46:00
首先我们把题目所示的要求分为两个问题: 问题一:如何给基于MFC的单文档视图类里面添加 控件.就是那种类似工具箱里面的控件. 问题二:如何给已经添加的控件 定义一些消息的响应函数. ××××××××× ...
- ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇
原文:ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇 第三章 为控件添加事件 好了,我们之前以前开发一个控件.而且也添加了属性,开发也很规范,但是那个控件还差最后一点:添加事件. 系列 ...
- VS2010/MFC编程入门之五十四(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)
上一节中鸡啄米讲了为Ribbon Bar添加控件的方法.本节教程鸡啄米将继续完善前面的实例,讲解一些稍复杂的控件的添加方法,及如何为它们添加消息处理函数. 一.为Ribbon Bar添加更多Ribbo ...
- VS2010-MFC(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)
转自:http://www.jizhuomi.com/software/255.html 上一节讲了为Ribbon Bar添加控件的方法.本节教程将继续完善前面的实例,讲解一些稍复杂的控件的添加方法, ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
- iOS控件之UIResponder类
iOS控件之UIResponder类 在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,我们知道UIApplication.UIView.UIViewController这几个 ...
- C#.NET 通用控件数据源绑定类
using System.Data; using System.Collections; using System.Collections.Generic; using System.Web.UI; ...
- 【C#】使用IExtenderProvider为控件添加扩展属性,像ToolTip那样
申明: - 本文适用于WinForm开发 - 文中的“控件”一词是广义上的说法,泛指包括ToolStripItem.MenuItem在内单个界面元素,并不特指继承自Control类的狭义控件 用过To ...
随机推荐
- HashMap put,get操作
HashMap中的put方法 public V put(K key, V value) { //当key为null,调用putForNullKey方法,保存null与table第一个位置中,这是Has ...
- iOS UITableView点击按钮滚到顶部
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- ATS连接 https
HTTPS协议是Http Over SSL,简单来说就是HTTP的安全版本,在HTTP的基础上增加SSL/TLS加密传输协议,通过HTTPS加密传输和身份认证保证了传输过程的安全性.在登录网银和电子邮 ...
- iOS调用系统功能发邮件
使用MFMailComposeViewController发送邮件 1.项目需要导入框架:MessageUI.framework 2.使用的Controller的.h文件中添加代理 MFMailCom ...
- 统计哪些程序占用了swap
命令如下 : for i in `cd /proc;ls |grep "^[0-9]"|awk ' $0 >100'` ;do awk '/Swap:/{a=a+$2}END ...
- 伪类写border, transform: scale3d() 及兼容性
.top::before { content: ""; position: absolute; left: 0; width: 200%; height: 0; border-to ...
- Java基础之一组有用的类——为标记定义自己的模式(ScanString)
控制台程序. Scanner类提供了一种方式,用来指定如何识别标记.这需要使用next()方法的两个重载版本.其中的一个版本接受Pattern类型的参数.另一个版本接受String类型的参数,用来指定 ...
- Lintcode: Merge Sorted Array II
Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...
- 源码搭建LNMP
源码安装LNMP 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 前言:非常简单的一个平台LNMP,在生产实际环 ...
- 写了个pager, 可供参考
/* Author: Calos Description: patv2 pager !import: this pager goes with the time, we just temporaril ...