积极答复者
如何编写一个程序,能自由创建控件就像设计时一样

问题
答案
-
我找到方法了!下面说思路。
单击button1,触发click事件,将某个变量的值设为true(暂且定为a)
在窗口的mousedown事件中,判断a的值,是真的话将另一个变量的值也设为真(暂且定为b),然后新建控件
在窗口的mousemove事件中,判断b的值,是真的话将控件调整尺寸(size属性等等,利用鼠标位置来判断)
在窗口的mouseup事件中,将所有变量的值设为false
- 已标记为答案 windows 7 to windows xp shared error 2012年4月29日 13:21
全部回复
-
我就拿textbox举例吧。
设计窗体时,你是不是点了一下texbox的按钮,然后在窗体上进行拖动来创建一个textbox?我想让用户点一下button1,便可以使用户在窗体上拖动,来创建textbox。我使用了Button,在看懂原理之后你自己尝试修改成TextBox:WinForm支持拖拽效果
Public Class Form1 '计数变量,说明输出了第N个Button Private count As Integer = 1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.AllowDrop = True '窗体自身支持接受拖拽来的控件 End Sub Private Sub Button1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown '左键的话,标志位为true(表示拖拽开始) If (e.Button = Windows.Forms.MouseButtons.Left) Then Button1.DoDragDrop(Button1, DragDropEffects.Copy Or DragDropEffects.Move) '形成拖拽效果 End If End Sub Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter If (e.Data.GetDataPresent(GetType(Button))) Then '当Button被拖拽到WinForm上时候,鼠标效果出现 e.Effect = DragDropEffects.Copy End If End Sub Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop '拖放完毕之后,自动生成新控件 Dim btn As New Button btn.Size = Button1.Size btn.Location = Me.PointToClient(New Point(e.X, e.Y)) '用这个方法计算出客户端容器界面的X,Y坐标。否则直接使用X,Y是屏幕坐标 Me.Controls.Add(btn) btn.Text = "按钮" + count.ToString count = count + 1 End Sub End Class
【设计界面】
- 已编辑 ThankfulHeartModerator 2012年4月29日 6:21
-
我找到方法了!下面说思路。
单击button1,触发click事件,将某个变量的值设为true(暂且定为a)
在窗口的mousedown事件中,判断a的值,是真的话将另一个变量的值也设为真(暂且定为b),然后新建控件
在窗口的mousemove事件中,判断b的值,是真的话将控件调整尺寸(size属性等等,利用鼠标位置来判断)
在窗口的mouseup事件中,将所有变量的值设为false
- 已标记为答案 windows 7 to windows xp shared error 2012年4月29日 13:21