如何控制多个新表格 [英] How to control multiple New Forms

查看:55
本文介绍了如何控制多个新表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有一个带有3个按钮(开始",停止",新表单"),组框​​和一个面板的MainForm.
我创建了另一个带有DataGridView1的Form->(ModbusPollFormFull),当我单击开始"(经过计时器的方法)时,该表格填充了数据(来自硬件).

我确实在MainForm中有一个groupbox(具有用于控制NewForm->(ModbusPollFormFull)的所有设置).我需要一个新建表单"按钮来创建一个新表单,并使用不同的设置填充数据.因此,用户可以同时查看两个数据.但是,每当我单击新建按钮"时,新的(ModbusPollFormFull2)将开始填充数据(以pollcount = 1开头),而现有的(ModbusPollFormFull1)将停止轮询(pollcount停止).我只希望能够更改(ModbusPollFormFull1或ModbusPollFormFull2)的设置(如果我选择了该表单,并且它们都仍在轮询,直到我单击停止").我不知道如何.

timer.Start将启动pollfunction()

我已经稍微修改了代码.但它仍然无法正常工作.

Hello,
I have a MainForm with 3 buttons("Start","Stop","New Form"),groupbox and one panel.
I create another Form-->(ModbusPollFormFull) with DataGridView1 which populate the data (from hardware) when i clicked "Start" (timer elapsed method).

i do have a groupbox(with all the settings to control the NewForm->(ModbusPollFormFull)) in the MainForm. I need a "New Form" button to create a new form and populate data with different settings. So, the users can view both data at the same time. But whenever i click the "New Button", the new (ModbusPollFormFull2) will start populate the data (starts with pollcount=1) but the existing (ModbusPollFormFull1) will stop the poll(pollcount stop). All i want is to be able to change the settings of the (ModbusPollFormFull1 or ModbusPollFormFull2) if i select the form and they all still polling until i click "Stop". I dont know how.

timer.Start will start the pollfunction()

I already change the code a little bit . but it still not working.

Private Sub StartPoll()
        pollCount = 0
        'Open COM port using provided settings:
        If mb.Open(PortNameCB.SelectedItem.ToString(), CInt(BaudrateCB.SelectedItem.ToString()), 8, Parity.Even, StopBits.One) Then

            'Disable double starts:
            mpf.NoConnectionLabel.Visible = False
            dataType = DataTypeCB.SelectedItem.ToString()
'Settings components
            StartButton.Enabled = False
            QuantityCB.Enabled = False
            SlaveIDTB.Enabled = False
            AddressCB.Enabled = False
            ScanRateCB.Enabled = False
            DataTypeCB.Enabled = False
            'Set polling flag
            isPolling = True
            'Start Timer using provided values:
            timer.AutoReset = True

            If ScanRateCB.Text <> " "  Then
                timer.Interval = CDbl(ScanRateCB.Text)
            Else
                timer.Interval = 1000
            End If
'pollfunction() starts
            timer.Start()
        End If
    End Sub

Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
       StartPoll()
End Sub

Private Sub StopPoll()
        mpf.NoConnectionLabel.Visible = True
        isPolling = False
        timer.Stop()
        StartButton.Enabled = True

'Settings components
        QuantityCB.Enabled = True
        SlaveIDTB.Enabled = True
        AddressCB.Enabled = True
        ScanRateCB.Enabled = True
        DataTypeCB.Enabled = True

        mb.Close() 'Closing the serialport
    End Sub

 Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
        StopPoll()
      
    End Sub

Dim mpf As New ModbusPollFormFull

Private Sub NewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewButton.Click
        Start += 1
        Inc += 30
        For i As Integer = 0 To Start

            mpf = New ModbusPollFormFull
            mpf.Text = "ModbusPoll:" & CStr(i)
            mpf.Name = "ModbusPollFormFull" & i

            Load_Columns()

            For j As Integer = 30 To Inc
                mpf.Left = j
            Next j

        Next i

        If Not StartButton.Enabled = True Then
            StartPoll()
        End If

        mpf.TopLevel = False
        Me.Panel1.Controls.Add(mpf)
        mpf.BringToFront()
        mpf.Focus()
        mpf.Show()

    End Sub

推荐答案

这里的想法是:谁需要MDI?为什么要折磨自己并吓users用户?
帮自己一个忙:根本不要使用MDI.没有设计,您可以更轻松地实现设计,并且质量更高.即使Microsoft也不鼓励使用MDI,实际上,Microsoft已将其从WPF中删除,并且几乎不支持它.更重要的是,如果您使用MDI,则会吓跑所有用户.只是不要.请参阅:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [如何在WPF中创建MDI父窗口? [ ^ ].

我可以解释该怎么做.请查看我过去的答案:
如何在WPF中创建MDI父窗口? [解决方案2 ],
在WPF中使用MDI窗口的问题 [ ^ ],
MDIContainer提供错误 [如何最大程度地设置子表单,最小化最后一个子表单 [ ^ ].

—SA
Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don''t. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA


请参阅我对解决方案1的评论.

您正在尝试以一种最幼稚和危险的方式来模拟线程,而不是以一百万倍的简单方式来使用线程.

所有硬件控制以及通信,联网,执行任何线性逻辑冗长的任务以及许多其他事情都应在单独的线程中进行编程.

现在,您似乎已经忽略了我对第一个问题的建议.您正在混淆UI和硬件控制以及数据获取.您确实需要将UI与其他功能隔离.问题不是类别,问题是层次.如果您在一个简短的答案中一无所知,我真的无法教您如何做.

让我们回到线程.您需要执行轮询并填充一些UI,对不对?一个问题是:您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
Please see my comment to Solution 1.

You are trying to simulate threads in a most naive and dangerous way, instead of using threads in a million times simpler way.

All the hardware control, as well as communication, networking, performing any linear-logic lengthy task and a lot of other things should be programmed in a separate thread.

Now, it looks like you ignored my advice to your very first question. You are mixing up UI and hardware control and data acquisition. You do need isolate UI from other functionality. The problem is not classes, the problem is layers. I cannot really teach how to do it if you have no idea in one short answer.

Let''s come back to threads. You need to perform polling and populate some UI, right? One problem is: you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


请再次查看我对第一个问题和解决方案2的评论.我在谈论UI和分层体系结构的隔离. UI的隔离尤为重要,因为UI往往以更激进的方式发生比其他任何事情都更频繁的更改.

我建议您学习并分析以下建筑模式的适用性( http://en.wikipedia .org/wiki/Architectural_pattern_(computer_science) [^ ]):

MVVM —模型视图视图模型,
http://en.wikipedia.org/wiki/Model_View_ViewModel [> http://en.wikipedia.org/wiki/Model-view-controller [ ^ ]),

MVA —模型视图适配器,
http://en.wikipedia.org/wiki/Model–view–adapter [ ^ ],

MVP —模型视图呈现器,
> http://en.wikipedia.org/wiki/Model-view-presenter [ ^ ].
请注意这些架构的动机.如果您了解它,就可以提出更好的设计思路.

—SA
Please again see my comments to your very first question and Solution 2. I''m talking about isolation of UI and layered architecture. Isolation of UI is especially important, because UI tend to change much more often than anything else, in much more radical ways.

I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

—SA


这篇关于如何控制多个新表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆