
Private Sub Form_Click()
Dim txt() As String, i As Integer, st As String
FileName = "C:\1txt" '文件路径
Text1 = "": Text2 = ""
Open FileName For Input As #112
While Not EOF(112)
Line Input #112, st
If st <> "" Then
ReDim Preserve txt(i): txt(i) = st: i = i + 1
End If
Wend
Close #112
Rand txt() '随机
For i = 0 To UBound(txt) Step 2
Text1 = Text1 & txt(i) & vbNewLine
Text2 = Text2 & txt(i + 1) & vbNewLine
Next i
End Sub
'随机函数
Private Function Rand(ary() As String) As Long
Dim i As Integer, j As Integer, tp As String
Randomize
For i = 0 To UBound(ary)
j = Int(Rnd (UBound(ary)))
tp = ary(i): ary(i) = ary(j): ary(j) = tp
Next i
End Function
我们对控件进行分组的原因不外乎三个
为了获得清晰的用户界面而将相关的窗体元素进行可视化分组
编程分组 如对单选按钮进行分组
为了在设计时将多个控件作为一个单元来移动
在中 有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
Private Sub Command1_Click()
GetNextNumber " ", 0, 4, 10
End Sub
Private Sub GetNextNumber(ByVal 已取得串 As String, Minimum As Integer, 还要取个数 As Integer, ByVal 总元素个数 As Integer)
'Got - 已经获得的数
'Minimum - 为了保证不重复,下一个取的数必须大于这个值,使得每个组合总是递增排列
'Rest - 还要取几个数
Dim X As Integer
For X = Minimum + 1 To 总元素个数 - 还要取个数 + 1
If 还要取个数 = 1 Then
List1AddItem 已取得串 & Str(X)
Else
GetNextNumber 已取得串 & Str(X), X, 还要取个数 - 1, 总元素个数
End If
Next X
End Sub
我的代码又重新改了一下
Text1是原始数据
Text2是排列后的结果
Command1是执行分组的按钮
注意:
Text1MultiLine = True
Text1ScrollBars = 2
Text2MultiLine = True
Text2ScrollBars = 2
要预先设置
下面是代码
Private Sub Command1_click()
Text2Text = ""
Open "c:\atxt" For Output As #1
Print #1, Text1Text
Close #1
Dim a As String
Dim b() As String, i As Integer
Open "c:\atxt" For Input As #1
Do While Not EOF(1)
Line Input #1, a
ReDim Preserve b(i) As String
b(i) = b(i) & a
i = i + 1
Loop
Close #1
Dim j As Integer
For j = 0 To UBound(b) - 5 '5代表每组5条数据
Text2Text = Text2Text & "第" & j + 1 & "组" & Chr(13) & Chr(10)
For i = j To j + 4 '4代表每组4+1条数据
Text2Text = Text2Text & b(i) & Chr(13) & Chr(10)
Next i
Next j
Kill "c:\atxt"
End Sub
没看出这么分组的意义和逻辑,不是定长非组 也不是按大小分,没有规律分组就只能写死长度了 用mid\left\right就行
Dim strNum as string
Dim strNum1 as string
Dim strNum2 as string
Dim strNum3 as string
Dim strNum4 as string
Dim strNum5 as string
Dim strNum6 as string
strNum='0x0000000400004001124000000020000000000000001c00800000000000000000'
strNum1=Left(strNum,9)
strNum2=Min(strNum,8,9)
strNum6=Right(strNum,16)
every radio button has a property of 'GroupName' in Excel macro
设置第二组的groupname名称为同一名称,就OK了。
比如:
ActiveSheetOLEObjects("optionbutton4")ObjectGroupName = "SecondGroup"
是不是该给分了。
以上就是关于朋友想请教一下VB随机分组的解决版法。全部的内容,包括:朋友想请教一下VB随机分组的解决版法。、vb.net入门之分组控件:GroupBox控件、VB 求方法 1,2,3,4,5,6,7,8,9,10 n个一组 求分组等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)