asp.net C# winform 視窗傳值與拖動執行個體

來源:互聯網
上載者:User

mousedown(object sender, mouseeventargs e)
mouseup(object sender, mouseeventargs e)
mousemove(object sender, mouseeventargs e)
這三個函數的作用分別是滑鼠按下時啟用,滑鼠鬆開啟用和滑鼠移動
需要考慮的情況如下
1. 只有在滑鼠按下的時候才可以使用滑鼠移動事件
2. 滑鼠移動的時候控制項重新調整位置
為了有效控制第一個條件,所以可以將滑鼠移動事件的綁定延遲到滑鼠按下事件中,而不是在初始化時完成,另外要記錄下初始狀態下滑鼠位於控制項中的座標,這裡注意mouseeventargs參數中的x,y表示的是滑鼠在當前控制項中的layout座標,而不是滑鼠在主視窗中的座標。

rivate void button1_mousedown(object sender, mouseeventargs e)
        {
            this.tmpx = e.x;
            this.tmpy = e.y;
            this.button1.mousemove += new system.windows.forms.mouseeventhandler(this.button1_mousemove);
        }
同時在滑鼠送開事件中再將該方法綁定脫離
        private void button1_mouseup(object sender, mouseeventargs e)
        {
            this.button1.mousemove -= new system.windows.forms.mouseeventhandler(this.button1_mousemove);
        }
最後是mousemove事件
        private void button1_mousemove(object sender, mouseeventargs e)
        {
            this.button1.location = new system.drawing.point(this.button1.location.x + e.x - this.tmpx, this.button1.location.y + e.y - this.tmpy);
        }
這裡重新計算了控制項的新位置。
例子中使用了button作為實驗對象並且額外定義了
        private int tmpx = 0;
        private int tmpy = 0;  

 

private int tmpx = 0; 

private int tmpy = 0; 

private void panel1_mousedown(object sender, mouseeventargs e) 

    if (e.button == mousebuttons.left) 

    { 

        this.tmpx = e.x; 

        this.tmpy = e.y; 

        this.panel1.cursor = cursors.sizeall; 

        this.panel1.mousemove += new mouseeventhandler(panel1_mousemove); 

    } 

 

private void panel1_mouseup(object sender, mouseeventargs e) 

    this.panel1.cursor = cursors.default; 

    this.panel1.mousemove -= new mouseeventhandler(panel1_mousemove); 

 

void panel1_mousemove(object sender, mouseeventargs e) 

    this.panel1.location = new point(this.panel1.location.x + e.x - this.tmpx, this.panel1.location.y + e.y - this.tmpy); 

}


看一個winform,showdialog,子表單向父表單傳值

例子:
下面是子表單代碼,要求輸入phone,然後會返回給父表單。

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;

namespace windowsapplication1
{
    public partial class phone : form
    {
        public phone()
        {
            initializecomponent();
            btnok.dialogresult = dialogresult.ok;
            btnok.dialogresult = dialogresult.cancel;
        }
        public string phonenumber
        {
            get { return textbox1.text; }
            set { textbox1.text = value; }
        }
        private void phone_load(object sender, eventargs e)
        {

        }
    }
}

不包含任何處理按鈕單擊事件的代碼,因為設定了每個按鈕的dialogresult屬性,所以單擊ok或者cancel按鈕後,表單就消失了。下面的代碼顯示了父表單中調用phone對話方塊的方法。

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;

namespace windowsapplication1
{
    public partial class form7 : form
    {
        public form7()
        {
            initializecomponent();
        }

        private void button1_click(object sender, eventargs e)
        {
            phone frm = new phone();
            frm.showdialog();
            if (frm.dialogresult == dialogresult.ok)
            {
                label1.text = "phone number is " + frm.phonenumber;
               
            }
            else if (frm.dialogresult == dialogresult.cancel)
            {
                label1.text = "form was canceled";
              
            }
            frm.close();
        }
    }
}

看起來非常簡單,建立新的phone對象frm,在調用frm.showdialog方法是,代碼停止,等待phone表單返回,接著檢查phone表單的dialogresult屬性,由於表單還沒有釋放,是不可見的,所以仍可以訪問公用屬性phonenumber,一旦擷取了需要的資料,就可以嗲用表單的close方法。
一切正常,但是如果返回的格式不正確怎麼辦,就要把showdialog方法放在迴圈中,就可以再次調用,讓使用者重新輸入,就可以得到正確的值。

上面的代碼改成下面的即可。

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;

namespace windowsapplication1
{
    public partial class form7 : form
    {
        public form7()
        {
            initializecomponent();
        }

        private void button1_click(object sender, eventargs e)
        {
            phone frm = new phone();

            while (true)
            {
                frm.showdialog();
                if (frm.dialogresult == dialogresult.ok)
                {
                    label1.text = "phone number is " + frm.phonenumber;
                    if (frm.phonenumber.length == 8 || frm.phonenumber.length == 12)
                    {
                        break;
                    }
                    else
                    {
                        messagebox.show("");
                    }
                }
                else if (frm.dialogresult == dialogresult.cancel)
                {
                    label1.text = "form was canceled";
                    break;
                }
            }
            frm.close();
        }
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.