生成多个新的窗口,并控制它们!

初学额 2011-09-27 11:18:28
http://topic.csdn.net/u/20110919/13/08965b10-0075-462a-8175-7d2ee2407e95.html?1265005039
接上贴

继续请教csdn 的大侠们!
下面是 hztltgg 帮我写的代码,再次表示感谢!
我的程序很简单,有两个 From , from1 from2
点击 Form1 按纽,引用 From2 生成多个 新的窗口(点一次生成一个)。

生成的窗口执行的代码都是一样的(From2)呵呵 废话。。。
每个新窗口在生成后,把窗口名(From2.text) 根据生成的顺序改为 1,2,3,4,5,6,。。。。。。。 , 18,19

hztltgg 说过,可以根据窗口名来控制新建立的窗口。

不管根据什么控制,我只想要打开 和 关闭 这些新窗口。 请 hztltgg 已经各位大人帮帮忙,万分感谢!


Public Class Form1

Dim htForm As New List(Of Form2)

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click


Dim newform2 As Form2 = htForm.Item(CInt(TextBox1.Text))
htForm.Remove(newform2)
newform2.Close()

End Sub

Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim newform2 As New Form2
newform2.Show()
htForm.Add(newform2)
End Sub

End Class



...全文
57 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hztltgg 2011-09-27
  • 打赏
  • 举报
回复
newform2.Text = "窗口 " & index.ToString
因为我打开的窗口标题上 窗口 1 这样的形式,前面加了窗口和空格三个字符,所以取的时候从第三个开始取u.Text.Substring(3)。

你的代码直接newform2.Text = index.ToString,当然不用从第三个开始取了
初学额 2011-09-27
  • 打赏
  • 举报
回复
我试着将

Dim newform2 As Form2 = htForm.First(Function(u) u.Text.Substring(3) = TextBox1.Text)

改为

Dim newform2 As Form2 = htForm.First(Function(u) u.Text = TextBox1.Text)

测试通过,请问 hztltgg 这样有什么不妥吗?
初学额 2011-09-27
  • 打赏
  • 举报
回复
{"startIndex 不能大于字符串长度。
参数名: startIndex"}
初学额 2011-09-27
  • 打赏
  • 举报
回复
我测试过,提示错误! 大哥帮我看看那里不对

Public Class Form1
Dim htForm As New List(Of Form2)


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static index As Integer = 10

Dim newform2 As New Form2
newform2.Text = index.ToString
'newform2.Tag 这个属性保存窗口的特征数据比用标题更合适,因为实际的窗口标题可能没有1,2,3这样有规律
index += 1
newform2.Show()
htForm.Add(newform2)



End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try

Dim newform2 As Form2 = htForm.First(Function(u) u.Text.Substring(3) = TextBox1.Text)
'或者:Dim newform2 As Form2 = htForm.Find(Function(u) u.Text.Substring(3) = TextBox1.Text)
'或者:如果不支持linq的话,通过循环获取也一样

htForm.Remove(newform2)
newform2.Close()

Catch ex As InvalidOperationException
MessageBox.Show("窗口已关闭")
End Try


End Sub
End Class
bdmh 2011-09-27
  • 打赏
  • 举报
回复
以名字为键值,添加到列表中,操作时根据名字取出来就是了
hztltgg 2011-09-27
  • 打赏
  • 举报
回复
没太明白你的意思


Public Class Form1

Dim htForm As New List(Of Form2)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Static index As Integer = 1

Dim newform2 As New Form2
newform2.Text = "窗口 " & index.ToString
'newform2.Tag 这个属性保存窗口的特征数据比用标题更合适,因为实际的窗口标题可能没有1,2,3这样有规律
index += 1
newform2.Show()
htForm.Add(newform2)

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

Try

Dim newform2 As Form2 = htForm.First(Function(u) u.Text.Substring(3) = TextBox1.Text)
'或者:Dim newform2 As Form2 = htForm.Find(Function(u) u.Text.Substring(3) = TextBox1.Text)
'或者:如果不支持linq的话,通过循环获取也一样

htForm.Remove(newform2)
newform2.Close()

Catch ex As InvalidOperationException
MessageBox.Show("窗口已关闭")
End Try


End Sub

End Class

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧