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 ...
随机推荐
- Java 收集的代码 transient
public class Main { public static void main(String[] args) { ((NULL)null).haha(); } } class NULL { p ...
- [Slimdx]顶点和索引缓冲,绘制了2个分离的三角形
定义网格顶点和索引缓冲,绘制了2个分离的三角形. using System; using System.Drawing; using RGeos.SlimScene.Core; using SlimD ...
- 清除UIWebView的缓存
//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...
- lua对模块接口扩展的一种方法
module lua中模块的实现,对于使用者来说就是一个库,引用此库后,可以调用库中实现的任意函数. 使用库,可以将一类功能相关的接口做封装,并提供开放接口. 参考: http://blog.codi ...
- jQuery:多个AJAX/JSON请求对应单个回调并行加载
因为我们使用jQuery,这意味着需要调用 jQuery.getScript 和 jQuery.getJSON 函数. 我知道这些函数都是异步执行(asyncronously)并且会延迟一段时间返回, ...
- SC-控制Windows服务的命令
Windows自带一个控制服务的命令-SC,下面用Terminal Service做个简单例子: 查询Terminal Service的配置 C:\Users\jackie.chen>sc qc ...
- iOS 判断字符串中含有某个字符串 rangeOfString
//判断roadTitleLab.text 是否含有qingjoin if([roadTitleLab.text rangeOfString:@"qingjoin"].locati ...
- Effective C++ 1.让自己习惯C++
//条款01:视C++为一个语言联邦 // 1:C++主要包含的语言为: // A:C.说到底C++仍然以C为基础.区块(blocks).语句.预处理器.内置数据类型.数组.指针等均来自于C.许多时候 ...
- Swift动画编程指南-01 简介
大家好,我是老镇,这段时间家里和工作上发生了很多的事情,所以很长一段时间都没有出来搞什么小动作了.在接下来的一段时间内我会制作一些列关于使用Swift进行动画编程的视频,希望和大家胃口. 在iOS的世 ...
- Leetcode: Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...