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();
  • 相关阅读:
    (转载)openwrt nginx
    *** 竞赛中的各种低级错误,及编程常见错误小结 ***
    信息学奥赛辅导经验谈 & 问题教学法中的学生思维能力培养
    数学&数论的一些题
    信息学竞赛中的一些经典思维 (题)
    从权值线段树到主席树
    浅谈莫队算法
    CSP-S 2019提高组训练 服务器需求
    NOIP2019 PJ 对称二叉树
    NOIP2017 PJ 跳房子 —— 单调队列优化DP
  • 原文地址:https://www.cnblogs.com/quanxi/p/6550932.html
Copyright © 2011-2022 走看看