首页 / C# / C#:向控件添加信息类
C#:向控件添加信息类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#:向控件添加信息类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6097字,纯文字阅读大概需要9分钟。
内容图文
![C#:向控件添加信息类](/upload/InfoBanner/zyjiaocheng/1060/83cc813652ab4cd7928b10a1f54ff5ad.jpg)
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); privatedelegatevoid ChangeComboBoxValueHandler(ComboBox ctrl); privatedelegatevoid SetCtrlEnableHandler(Control ctrl, bool value); privatedelegatevoid SetCtrlValueHandler(Control ctrl, string value); publicstaticvoid AddCtrlValue(Form parentForm, Control ctrl, string value) { if (parentForm.InvokeRequired) { AddCtrlValueHandler method = new AddCtrlValueHandler(AddCtrlValueMethod); parentForm.BeginInvoke(method, newobject[] { ctrl, value }); } else { AddCtrlValueMethod(ctrl, value); } } privatestaticvoid AddCtrlValueMethod(Control ctrl, string value) { if (ctrl is TextBox) { TextBox box = ctrl as TextBox; box.Text = box.Text + value; } elseif (ctrl is Label) { Label label = ctrl as Label; label.Text = label.Text + value; } elseif (ctrl is ListBox) { ListBox listbox = ctrl as ListBox; if (listbox.Items.Count > 200) { listbox.Items.Clear(); } listbox.Items.Add(value); if (listbox.Items.Count > 1) { listbox.SelectedIndex = (listbox.Items.Count - 1); } } elseif (ctrl is RichTextBox) { RichTextBox richtextbox = ctrl as RichTextBox; richtextbox.Text += value + "\r\n"; if (richtextbox.Text.Length > 6000) { richtextbox.Text = string.Empty; } } } publicstaticvoid ChangeComboBoxValue(Form parentForm, ComboBox ctrl) { if (parentForm.InvokeRequired) { ChangeComboBoxValueHandler method = new ChangeComboBoxValueHandler(ChangeComboBoxValueMethod); parentForm.BeginInvoke(method, newobject[] { ctrl }); } else { ChangeComboBoxValueMethod(ctrl); } } privatestaticvoid ChangeComboBoxValueMethod(ComboBox ctrl) { if (ctrl.Items.Count > 1) { if (ctrl.SelectedIndex == 0) { ctrl.SelectedIndex = 1; } else { ctrl.SelectedIndex = 0; } } } publicstaticvoid SetCtrlEnable(Form parentForm, Control ctrl, bool value) { if (parentForm.InvokeRequired) { SetCtrlEnableHandler method = new SetCtrlEnableHandler(SetCtrlEnableMethod); parentForm.BeginInvoke(method, newobject[] { ctrl, value }); } else { SetCtrlEnableMethod(ctrl, value); } } publicstaticvoid SetCtrlEnable(UserControl parentCtrl, Control ctrl, bool value) { if (parentCtrl.InvokeRequired) { SetCtrlEnableHandler method = new SetCtrlEnableHandler(SetCtrlEnableMethod); parentCtrl.BeginInvoke(method, newobject[] { ctrl, value }); } else { SetCtrlEnableMethod(ctrl, value); } } privatestaticvoid 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 { } } publicstaticvoid SetCtrlValue(Form parentForm, Control ctrl, string value) { if (parentForm.InvokeRequired) { SetCtrlValueHandler method = new SetCtrlValueHandler(SetCtrlValueMethod); parentForm.BeginInvoke(method, newobject[] { ctrl, value }); } else { SetCtrlValueMethod(ctrl, value); } } publicstaticvoid SetCtrlValue(UserControl parentCtrl, Control ctrl, string value) { if (parentCtrl.InvokeRequired) { SetCtrlValueHandler method = new SetCtrlValueHandler(SetCtrlValueMethod); parentCtrl.BeginInvoke(method, newobject[] { ctrl, value }); } else { SetCtrlValueMethod(ctrl, value); } } privatestaticvoid SetCtrlValueMethod(Control ctrl, string value) { if (ctrl is TextBox) { TextBox box = ctrl as TextBox; box.Text = value; } elseif (ctrl is ComboBox) { ComboBox box2 = ctrl as ComboBox; try { int selIndex = 0; try { selIndex = int.Parse(value); if (selIndex < box2.Items.Count - 1) { box2.SelectedIndex = selIndex; } else { box2.SelectedIndex = box2.FindString(value); } } catch { box2.SelectedIndex = box2.FindString(value); } } catch (Exception exception) { //LogFile.Log.Debug(exception.Message); } } elseif (ctrl is Label) { Label label = ctrl as Label; label.Text = value; } elseif (ctrl is Button) { Button button = ctrl as Button; button.Text = value; } elseif (ctrl is NumericUpDown) { NumericUpDown down = ctrl as NumericUpDown; down.Value = int.Parse(value); } elseif (ctrl is Form) { Form form = ctrl as Form; form.Text = value; } elseif (ctrl is ProgressBar) { ProgressBar bar = ctrl as ProgressBar; bar.Value = int.Parse(value); } elseif (ctrl is CheckBox) { try { CheckBox cb = ctrl as CheckBox; cb.Checked = bool.Parse(value); } catch { } } else { ctrl.Text = value; } } privatedelegatevoid SetCtrlVisibleHandler(Control ctrl, bool value); publicstaticvoid SetCtrlVisible(Form parentForm, Control ctrl, bool value) { if (parentForm.InvokeRequired) { SetCtrlVisibleHandler method = new SetCtrlVisibleHandler(SetCtrlVisibleMethod); parentForm.BeginInvoke(method, newobject[] { ctrl, value }); } else { SetCtrlVisibleMethod(ctrl, value); } } privatestaticvoid SetCtrlVisibleMethod(Control ctrl, bool value) { try { ctrl.Visible = value; } catch { } } privatedelegatevoid SetCtrlTagHandler(Control ctrl, string value); publicstaticvoid SetCtrlTag(Form parentForm, Control ctrl, string value) { if (parentForm.InvokeRequired) { SetCtrlTagHandler method = new SetCtrlTagHandler(SetCtrlTagMethod); parentForm.BeginInvoke(method, newobject[] { ctrl, value }); } else { SetCtrlTagMethod(ctrl, value); } } privatestaticvoid SetCtrlTagMethod(Control ctrl, string value) { try { ctrl.Tag = value; } catch { } } } }
原文:http://www.cnblogs.com/shenchao/p/4756780.html
内容总结
以上是互联网集市为您收集整理的C#:向控件添加信息类全部内容,希望文章能够帮你解决C#:向控件添加信息类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。