【问题标题】:How to: Force users to focus a winform before selecting something from it如何:强制用户在从中选择某些内容之前集中注意力
【发布时间】:2019-09-06 18:06:04
【问题描述】:

我正在开发一个使用 WinForms 的项目,当用户单击 Form 上的控件而不是活动窗口时,我遇到了并发问题。每当用户激活它们以防止它们显示旧/脏信息时,我的表单都会刷新,但如果他们单击非活动 Form 上的按钮,则数据不会刷新,这可能会导致问题。

我知道 windows 中的某些程序会强制您在执行任何其他操作之前选择窗口,有没有办法在我的 VB.NET 程序中执行此操作?

【问题讨论】:

  • it doesn't get a chance to refresh it's data - 为什么?当它的按钮被点击时它会激活,不是吗?
  • 那里有异步处理吗?所以,当有人点击某个东西时,异步的东西还没有完成,你会得到异常/错误行为吗?
  • 您可以设置可变状态,并在刷新任何内容时使其为真,完成后将状态设置为假。并且在你的按钮点击中,如果状态为真,什么也不做,如果为假,则正常执行
  • 它非常模糊,听起来那个按钮的 Click 事件正在做一些非常顽皮的事情。就像挂起主线程太久,没有足够的并发性。用户输入总是在任何挂起的重绘之前处理。将 Me.Update() 作为事件处理程序中的第一条语句,以便在代码在树林中消失之前进行重绘。

标签: vb.net winforms


【解决方案1】:

此代码在我的测试中运行,但必须处理一些问题才能根据需要运行:

Dim mystts As Boolean = False

Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    'here your process, unfortunately this will be performed too after messagebox close back to form2
    'for example the process are:
    For myCnt As Integer = 1 To 4000
        Debug.Print("Test") 'This only for waiting
    Next
    mystts = False
End Sub

Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
    'we must define the other criteria for make mystts=true, for example if 
    'we call messageBox.show, we can pass out from this for example we have msgFlag
    'before call messageBox set msgFlag=true and after it set  msgFlag=false
    'and here you add: if msgFlag=true then exit sub
    mystts = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'if we click this button and myStts=true, the next process will wait until process in form activate finish
    MsgBox("this My Process, run after form2 activated and process finish")
End Sub 

【讨论】:

    猜你喜欢
    • 2020-02-24
    • 2010-10-15
    • 2018-01-25
    • 1970-01-01
    • 2021-12-31
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多