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#:向控件添加信息类的更多相关文章

  1. 关于在MFC的视图类里面添加各种控件 以及给这些控件添加对用的函数。2015-03-24 13:46:00

    首先我们把题目所示的要求分为两个问题: 问题一:如何给基于MFC的单文档视图类里面添加 控件.就是那种类似工具箱里面的控件. 问题二:如何给已经添加的控件 定义一些消息的响应函数. ××××××××× ...

  2. ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇

    原文:ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇 第三章 为控件添加事件 好了,我们之前以前开发一个控件.而且也添加了属性,开发也很规范,但是那个控件还差最后一点:添加事件. 系列 ...

  3. VS2010/MFC编程入门之五十四(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)

    上一节中鸡啄米讲了为Ribbon Bar添加控件的方法.本节教程鸡啄米将继续完善前面的实例,讲解一些稍复杂的控件的添加方法,及如何为它们添加消息处理函数. 一.为Ribbon Bar添加更多Ribbo ...

  4. VS2010-MFC(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)

    转自:http://www.jizhuomi.com/software/255.html 上一节讲了为Ribbon Bar添加控件的方法.本节教程将继续完善前面的实例,讲解一些稍复杂的控件的添加方法, ...

  5. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  6. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  7. iOS控件之UIResponder类

    iOS控件之UIResponder类 在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,我们知道UIApplication.UIView.UIViewController这几个 ...

  8. C#.NET 通用控件数据源绑定类

    using System.Data; using System.Collections; using System.Collections.Generic; using System.Web.UI; ...

  9. 【C#】使用IExtenderProvider为控件添加扩展属性,像ToolTip那样

    申明: - 本文适用于WinForm开发 - 文中的“控件”一词是广义上的说法,泛指包括ToolStripItem.MenuItem在内单个界面元素,并不特指继承自Control类的狭义控件 用过To ...

随机推荐

  1. 第四篇 Replication:事务复制-订阅服务器

    本篇文章是SQL Server Replication系列的第四篇,详细内容请参考原文. 订阅服务器就是复制发布项目的所有变更将传送到的服务器.每一个发布需要至少一个订阅,但是一个发布可以有多个订阅. ...

  2. 单臂路由+DHCP+VLAN

    使用思科模拟软件Cisco Packet Tracer Student,软件功能有限,只能架设简单的网络架构,适合初学者使用.

  3. InitializingBean afterPropertiesSet

    package org.test.InitializingBean; import org.springframework.context.support.ClassPathXmlApplicatio ...

  4. JavaScript解决命名冲突的一种方法

    过程化编码 过程化编码, 表现为 定义若干函数,然后调用定义函数, 随着页面交互逻辑变化, 从简单到复杂, 定义的所有函数.和变量 都挂在 window对象上, window对象 编程者子自定义变量名 ...

  5. swap文件

    # dd if=/dev/zero of=/tmp/myswap bs=1M count=4096 # mkswap /tmp/myswap # swapon /tmp/myswap # vim /e ...

  6. 从零开始攻略PHP(8)——面向对象(下)

    8.编写代码类 每个分离的函数可以执行一个明确的任务.任务越简单,编写与测试这个函数就越简单,当然也不要将这个函数分得太小——若将程序分成太多的小个体,读起来就会很困难. 使用继承可以重载操作.我们可 ...

  7. ps 简介

    1. ps 简介ps 命令就是最根本相应情况下也是相当强大地进程查看命令.运用该命令可以确定有哪些进程正在运行和运行地状态. 进程 是否结束.进程有没有僵死.哪些进程占用了过多地资源等等.总之大部分信 ...

  8. G面经Prepare: Valid Preorder traversal serialized String

    求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...

  9. Climbing Stairs - Print Path

    stair climbing, print out all of possible solutions of the methods to climb a stars, you are allowed ...

  10. oracle安装过程中遇到的问题

    今天遭遇ORA-12560: TNS: 协议适配器错误的问题,经过一番努力问题已经解决,与大家共享. 造成ORA-12560: TNS: 协议适配器错误的问题的原因有三个: 1.监听服务没有起起来.w ...