通过代码而不是在设计模式下创建 VB.Net 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16452561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:32:22  来源:igfitidea点击:

Creating VB.Net forms via code instead of in design mode

vb.netformsmultiple-monitors

提问by Dave123

I'm using netzero hardware to manage the contents of a number of monitors. My present solution creates a form in VB.Net that has a pixel offset corresponding to where I've placed the Monitors in display management in the control panel. Each monitor has a dedicated form, and in each form are various objects.

我正在使用 netzero 硬件来管理多个监视器的内容。我目前的解决方案在 VB.Net 中创建了一个表单,该表单的像素偏移与我在控制面板中的显示管理中放置监视器的位置相对应。每个监视器都有一个专用的窗体,并且在每个窗体中都有不同的对象。

The annoyance is that each form must be individually created (so far as I know) at design time. I can't make an array of forms, coupled with an array of offsets and assign all the properties through code.

令人烦恼的是,每个表单都必须在设计时单独创建(据我所知)。我无法制作表单数组,加上偏移数组并通过代码分配所有属性。

There ought to be a way to do this...it would simplify my coding and project management.

应该有办法做到这一点......它会简化我的编码和项目管理。

What I see on MSDN is either over my head or not helpful.

我在 MSDN 上看到的内容要么超出我的头脑,要么没有帮助。

回答by Dave123

I haven't tested this in hardware yet, but it does compile w/o error:

我还没有在硬件中测试过这个,但它确实编译没有错误:

Public Sub makeform()
    Dim MonitorForm(21) As Form
    Dim MPictureBoxes(21) As PictureBox

    Dim a As Integer


    For i As Integer = 0 To n 'up to 21
        MonitorForm(i) = New Form
        MonitorForm(i).Name = "Form" & (i + 1)
        MonitorForm(i).Text = "Form" & (i + 1)
        MonitorForm(i).Controls.Add(MPictureBoxes(i))
        MonitorForm(i).Location= new Point (x(i), y(i))
        With MPictureBoxes(i)
            .Name = "Picture Box " & Convert.ToString(i)
            .Image = Image.FromFile(CurrentPic(i))
            .Location = New System.Drawing.Point(0, 0)
            .Size = New Size(1920, 1080)

            '  Note you can set more of the PicBox's Properties here

        End With

    Next
    End Sub

Where I had gone wrong in my attempts at this was trying to do it this way

我在尝试中出错的地方是尝试这样做

Dim Monitor(21) as New Form

Dim Monitor(21) 作为新形式

That doesn't work, and the difference between Dim Monitor(21) as Form followed by monitor(i)= new Form was simply too subtle for my present understand of classes, namespaces etc.

这不起作用,而且 Dim Monitor(21) as Form 后跟 monitor(i)= new Form 之间的区别对于我目前对类、命名空间等的理解来说太微妙了。

.

.

回答by Dave123

Well, I've had to give up on this approach and go back to creating n forms at design time (which means that they have names of form2...form22, putting each of them at manual start positions in design mode. There just doesn't seem to be a way to do this with an array of forms. So the code I have built around the messiness of forms2...forms22 works just fine, it's just going to be messy to maintain and elaborate on.

好吧,我不得不放弃这种方法并返回到在设计时创建 n 个表单(这意味着它们的名称为 form2...form22,在设计模式下将它们每个放在手动开始位置。只有似乎不是用一系列表单来做到这一点的方法。所以我围绕表单2的混乱构建的代码......表单22工作得很好,只是维护和详细说明会很混乱。

The solution to this may lie in system.screen classes, but the documentation on this is too advanced for me and I'm not finding good code examples for anything other than extracting data about how many screens there are - nothing about writing to them.

对此的解决方案可能在于 system.screen 类,但关于此的文档对我来说太高级了,除了提取有关有多少屏幕的数据之外,我没有找到任何好的代码示例 - 与写入它们无关。

回答by Derek Tomes

This is very easy in code. You want to make many instances of the same form. In this case, I have created a form in the designer called frmTest and I create many instances in code called frmNew:

这在代码中非常容易。您想制作许多相同形式的实例。在这种情况下,我在设计器中创建了一个名为 frmTest 的表单,并在名为 frmNew 的代码中创建了许多实例:

Public Sub Main()
    For x = 100 To 400 Step 100
        For y = 100 To 700 Step 200
            Dim frmNew As New frmTest
            frmNew.Show()
            frmNew.Top = x
            frmNew.Left = y
            frmNew.Height = 100
            frmNew.Width = 200
        Next
    Next
End Sub

I have just used two loops to increment x and y values, but you could do this from a database or config file easily enough.

我刚刚使用了两个循环来增加 x 和 y 值,但是您可以很容易地从数据库或配置文件中执行此操作。

This is how it displays

这是它的显示方式