zoukankan      html  css  js  c++  java
  • C# widget

    Invoke(Delegate)的用法:

    //例如,要实时update窗体。如果在另一个线程中update,那么可以直接update(可以不在新线程中);也可以在Delegate中给出upate,然后invoke调用(但是invoke必须在另一线程中)
    //区别在于invoke是同步的(在此处,同步是啥意思)
    //参数Delegate拥有某个窗体句柄的控制权,即Delegate是在窗体类中定义的 —— 故update操作应在Delegate中
    //invoke应该在另一个线程中调用;返回值为Delegate的返回值
    class MyFormControl : Form
    {
        public delegate void AddListItem();
        public AddListItem myDelegate;
        private Thread myThread;
        
        public MyFormControl()
        {
            myDelegate = new AddListItem(AddListItemMethod);
            
            myThread = new Thread(new ThreadStart(ThreadFunction));
            myThread.Start();
        }
        public void AddListItemMethod()
        {
            //update
        }
        private void ThreadFunction()
        {
            MyThreadClass myThreadClassObject = new MyThreadClass(this);
            myThreadClassObject.Run();
        }
    }
    class MyThreadClass
    {
        MyFormControl myFormControl1;
        public MyThreadClass(MyFormControl myForm)
        {
            myFormControl1 = myForm;
        }
        public void Run()
        {
            myFormControl1.Invoke(myFormControl1.myDelegate);
            //myFormControl1.myDelegate();
        }
    }

    主要采用纯手写代码,生成窗体界面的方式(偶尔也可能拖控件)。

    空工程创建一个窗体(但是现在是先弹出控制台——想办法去掉):

    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Project1
    {
        class MyFormControl : Form
        {
            public MyFormControl()
            {
                ClientSize = new Size(292, 273);
                Text = "Custom Widget";
            }
            static void Main()
            {
                MyFormControl myForm = new MyFormControl();
                myForm.ShowDialog();
            }
        }
    }

    Button的创建及响应事件(下面的就不AddRange了):

    //基于刚才的工程,在构造函数中新建一个button
    //Location、Size、TabIndex、Text、EventHandler
    myButton = new Button();
    myButton.Location = new Point(72, 160);
    myButton.Size = new Size(152, 32);
    myButton.TabIndex = 1;
    myButton.Text = "Add items in list box";
    myButton.Click += new EventHandler(Button_Click);
    
    Controls.AddRange(new Control[] { myButton });

    ListBox的创建及用法:

    myListBox = new ListBox();
    myListBox.Location = new  Point(48, 32);
    myListBox.Name = "myListBox";
    myListBox.Size = new Size(200, 95);
    myListBox.TabIndex = 2;
    
    //添加项
    string myItem = "1";
    myListBox.Items.Add(myItem);
    myListBox.Update();
  • 相关阅读:
    PDF文件中的Form保存问题
    Understanding IP Fragmentation
    tcp ip guide IPsec IKE
    Windows安全事件日志中的事件编号与描述
    Cisco PIX fix up and Juniper firewall FTP ALG
    很好的IPSec介绍,详细解释了IKE协商的2个阶段的作用
    virtualbox 下运行Ubuntu 8.10的分辨率和guest additions的问题。
    Fixing the ‘Do you want to display nonsecure items’ message
    windows xp 开始菜单里面所有项目右键不起作用。
    HP backup and recovery manager
  • 原文地址:https://www.cnblogs.com/quanxi/p/6550932.html
Copyright © 2011-2022 走看看