资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb点虐 控件拖放 vba拖动控件

vb点虐 入门之分组控件:GroupBox控件

我们对控件进行分组的原因不外乎三个

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计制作、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的桂东网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

为了获得清晰的用户界面而将相关的窗体元素进行可视化分组

编程分组 如对单选按钮进行分组

为了在设计时将多个控件作为一个单元来移动

在中 有GroupBox Panel TabControl这三个控件可以实现上面所提到的三个分组目的 所以我们称它们为分组控件

这三个控件在功用上十分的相似 特别是GroupBox和Panel控件 只存在一点细微的差别而已(这个差别是 只有GroupBox控件可以显示标题 而只有Panel控件可以有滚动条) 这里我们就先来了解GroupBox控件的使用

GroupBox(控件组)控件一般是作为其他控件的组的容器的形式存在的 这样有利于用户识别 使界面变得更加友好(GroupBox控件相当于Visual Basic以前版本的Frame控件) 使用控件组控件可以将一个窗体中的各种功能进一步进行分类 例如 将各种选项按钮控件分隔开

当移动单个GroupBox控件时 它所包含的所有控件也将一起移动

在大多数情况下 对控件组控件没有实际的操作 我们用它对控件进行分组 通常没有必要响应它的事件 不过 它的Name Text和Font等属性可能会经常被修改 以适应应用程序在不同阶段的要求

GroupBox控件在工具箱中的图标如图所示

一 GroupBox控件的常用属性

Anchor和Dock 这两个属性是所有有用户界面的控件都有的定位属性 这里就不啰嗦了

Name属性 标识控件的对象名称

Text属性 显示在GroupBox控件右上方的标题文字 可以用来标识该控件组的描述

Font和ForeColor属性 用于改变GroupBox控件的文字大小以及文字的颜色 需要注意的时候 它不单改变GroupBox控件的Text属性的文字外观 同时也改变其内部控件的显示的Text属性的文字外观

二 创建一组控件

在窗体上放置GroupBox控件 从工具箱中拖放一个GroupBox控件到窗体上的合适位置 调整大小

在属性窗口中改变GroupBox控件的Text属性 作为它的标题

在GroupBox控件内拖放其它需要的控件 例如RadioButton控件

设置示例 如图一所示

图一 用控件组控件对单选按钮分组

我们在拖动单个GroupBox控件的时候 它内部的控件也会随着移动 以保持和GroupBox的相对位置不变 同理 删除GroupBox控件时 它所包含的所有控件也会被删除掉

当我们调整GroupBox控件所包含的控件的Anchor和Dock属性的时候 其参照物将不是Form窗体 而是GroupBox控件了

三 编程添加GroupBox控件以及它所包含的控件

虽然GroupBox控件是在设计时用视图设计布局效果最好 但是无可避免地 很多特殊情况下也是需要在运行做添加控件到控件组中的 这里我们就用代码来完成上图一界面的绘制

动态添加控件一般需要经过下面三个步骤

创建要添加的控件实例

设置新控件的属性

将控件添加到父控件的 Controls 集合

在Form 代码的任意位置增加初始化控件的过程InitializeControl() 代码如下所示

Sub InitializeControl()

首先添加Label和TextBox控件

Dim Label As New System Windows Forms Label

Dim TextBox As New System Windows Forms TextBox

Label

Label Location = New System Drawing Point( )

Label Name = Label

Label Size = New System Drawing Size( )

Label TabIndex =

Label Text = 户主姓名

TextBox

TextBox Location = New System Drawing Point( )

TextBox Name = TextBox

TextBox Size = New System Drawing Size( )

TextBox TabIndex =

TextBox Text =

把它们添加到父控件Form 的Controls集合中

Me Controls Add(TextBox )

Me Controls Add(Label )

添加三个GroupBox控件

Dim GroupBox As New System Windows Forms GroupBox

Dim GroupBox As New System Windows Forms GroupBox

Dim GroupBox As New System Windows Forms GroupBox

GroupBox

GroupBox BackColor = System Drawing SystemColors Control

GroupBox Location = New System Drawing Point( )

GroupBox Name = GroupBox

GroupBox Size = New System Drawing Size( )

GroupBox TabIndex =

GroupBox TabStop = False

GroupBox Text = 性别

GroupBox

GroupBox Location = New System Drawing Point( )

GroupBox Name = GroupBox

GroupBox Size = New System Drawing Size( )

GroupBox TabIndex =

GroupBox TabStop = False

GroupBox Text = 单元

GroupBox

GroupBox Location = New System Drawing Point( )

GroupBox Name = GroupBox

GroupBox Size = New System Drawing Size( )

GroupBox TabIndex =

GroupBox TabStop = False

GroupBox Text = 楼层

把它们添加到父控件Form 的Controls集合中

Me Controls Add(GroupBox )

Me Controls Add(GroupBox )

Me Controls Add(GroupBox )

添加RadioButton控件并分别绘制在GroupBox控件内

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

Dim RadioButton As New System Windows Forms RadioButton

RadioButton

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 男性

RadioButton

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 女性

RadioButton

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 二单元

RadioButton

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 三单元

RadioButton

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 一单元

RadioButton

RadioButton BackColor = System Drawing SystemColors Control

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 四单元

RadioButton

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 二楼

RadioButton

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 三楼

RadioButton

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 一楼

RadioButton

RadioButton BackColor = System Drawing SystemColors Control

RadioButton Location = New System Drawing Point( )

RadioButton Name = RadioButton

RadioButton Size = New System Drawing Size( )

RadioButton TabIndex =

RadioButton Text = 四楼

分别把它们添加到父控件GroupBox的Controls集合中

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

GroupBox Controls Add(RadioButton )

End Sub

把上一页的代码复制添加后 把控件初始化过程InitializeControl()过程添加到Form 的New构造函数中 如下图二所示

图二 在New构造函数中添加过程InitializeControl()

现在按F 运行 Form 的窗体控件布局(如下图三所示)是不是和我们手工布局的图一的布局是一样的呢?

lishixinzhi/Article/program/ASP/201311/21749

在VB.NET中实现文件的拖放

本文介绍了在VB NET中如何实现接受拖放的文件 即从资源管理器中拖放到应用程序中的时候 自动获取拖放的文件 文中的例子是一个接受拖放文件显示文件内容的VB NET实例程序 引言

对于文本格式的文件 我们可以直接拖到记事本中就可以看到内容 各种类型的图片 拖到Photoshop中 就可以直接对其编辑 我们如何在VB NET开发的程序也实现上述效果呢?

思路

我们知道 每一个Windows的应用程序都有一个消息队列 程序的主体接受系统的消息 然后分发出去(给一个form 或者一个控件) 接受者有相应的程序来处理消息 在 NET的Form中 默认情况下程序是不翻译这些消息的 也就是说默认我们的Class是不加入应用程序的消息泵 能不能把我们的Form Class加入应用程序的消息泵呢?可以!

在 NET中 任何一个实现IMessageFilter 接口的类 可以添加到应用程序的消息泵中 以在消息被调度到控件或窗体之前将它筛选出来或执行其他操作 使用 Application 类中的 AddMessageFilter 方法 可以将消息筛选器添加到应用程序的消息泵中

于是我们在程序加载的时候 调用Application AddMessageFilter(Me) 然而 默认情况下一个Form或者控件是不能接受拖放的文件的 我们调用一个WIN API DragAcceptFiles源码天空 这个API可以设置对应的控件是否能接受拖放的文件 然后可以用DragQueryFile查询拖放到的文件列表 也就是拖放文件地具体路径和文件名

代码

Imports System Runtime InteropServices

Public Class Form

Inherits System Windows Forms Form

Implements IMessageFilter

API申明

Const WM_DROPFILES = H   拖放文件消息

DllImport( shell dll ) Public Shared Sub DragFinish(ByVal hDrop As Integer)

End Sub

DllImport( shell dll ) Public Shared Sub DragAcceptFiles(ByVal hwnd As Integer ByVal fAccept As Boolean)

End Sub

DllImport( shell dll ) Public Shared Function DragQueryFile(ByVal HDROP As Integer ByVal UINT As Integer ByVal lpStr As System Text StringBuilder ByVal ch As Integer) As Integer

End Function

Private Sub Form _Load(ByVal sender As System Object ByVal e As System EventArgs) Handles MyBase Load

Application AddMessageFilter(Me)

DragAcceptFiles(TextBox Handle ToInt True)

End Sub

Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter PreFilterMessage

If m Msg = WM_DROPFILES Then

设置拖放的动作

Dim nfiles As Int

nfiles = DragQueryFile(m WParam ToInt Nothing )

Dim i As Int

Dim *** As New System Text StringBuilder( )

Dim sFirstFileName As String 记录第一个文件名

TextBox Clear()

For i = To nfiles

DragQueryFile(m WParam ToInt i *** )

If i = Then sFirstFileName = *** ToString

TextBox AppendText(ControlChars CrLf *** ToString)

Next

DragFinish(m WParam ToInt ) 拖放完成

显示文件内容

Dim fs As New System IO FileStream(sFirstFileName IO FileMode Open)

Dim sr As New System IO StreamReader(fs System Text Encoding GetEncoding( gb ))

TextBox AppendText(ControlChars CrLf sr ReadToEnd() ToString)

fs Close()

sr Close()

End If

Return False

End Function

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (ponents Is Nothing) Then

ponents Dispose()

End If

End If

Application RemoveMessageFilter(Me)

DragAcceptFiles(TextBox Handle ToInt False)

MyBase Dispose(disposing)

End Sub

lishixinzhi/Article/program/net/201311/13043

vb点虐 textbox1选中的文本,拖放到textbox2?

很久没有上这里了,今天看到了这个问题,尝试做了一个;

本例以源文本框TextBox1全部文字作为拖放文字为例,实现拖放

1、向一个窗体中添加两个文本框,分别名为TextBox1,TextBox2。注意:把TextBox2控件的AllowDrop属性设置成True,这点不要遗漏。

2、完整的代码如下:

Public Class Form1

Private MouseIsDown As Boolean = False

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown

'设置一个标志以显示鼠标已按下。

MouseIsDown = True

End Sub

Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove

If MouseIsDown Then

'开始拖动(将TextBox1的文本内容作为拖放内容)。

TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)

End If

MouseIsDown = False

End Sub

Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter

'检查正在被拖放的数据的格式。

If (e.Data.GetDataPresent(DataFormats.Text)) Then

'显示复制光标(表示是拖放行为)。

e.Effect = DragDropEffects.Copy

Else

'显示不放置光标(表示不是拖放行为)。

e.Effect = DragDropEffects.None

End If

End Sub

Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop

'粘贴文本(将拖放内容作为TextBox2的文本内容)。

TextBox2.Text = e.Data.GetData(DataFormats.Text)

End Sub

End Class


网站题目:vb点虐 控件拖放 vba拖动控件
本文地址:http://cdkjz.cn/article/ddchjci.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220