在与调用表单相同的屏幕上弹出DLL的表单

时间:2017-09-25 18:15:58

标签: c# vb.net forms dll

我有一个DLL:(MessageBox.DLL) 此DLL的表单看起来就像一个消息框。

msgbox与原始消息框具有相似的参数,

myMsgBox("TEXT HERE")

但问题是,如果有多个屏幕,它有时会弹出错误的屏幕

我想将Calling表单作为参考,而不传递像

那样的参数
Sub myMsgBox(ByVal MsgText As String) 
     formMsgbox.mainText = MsgText
     formMsgbox.Show()
End Sub

我想做的是像这样引用原始表格

Sub myMsgBox(ByVal MsgText As String) 
     formMsgbox.mainText = MsgText
     formMsgbox.size = new size (me.parent.size.x / 2, me.parent.size.y / 2)
     Dim Current_Screen As Screen = Screen.FromControl(me.parent)
     If Current_Screen.Primary = False Then
         Dim HCenter = Current_Screen.Bounds.Left + _
         (((Current_Screen.Bounds.Right - Current_Screen.Bounds.Left) / 2) - ((formMsgbox.Width) / 2))
         Dim VCenter = (Current_Screen.Bounds.Bottom / 2) - ((formMsgbox.Height) / 2)
         formMsgbox.StartPosition = FormStartPosition.Manual
         formMsgbox.Location = New Point(HCenter, VCenter)
     Else
         formMsgbox.StartPosition = FormStartPosition.CenterScreen
     End If
     formMsgbox.Show()
End Sub

显然,如果我每次都通过Form,我可以这样做,但是试图避免这种情况。

我希望它能够像原始的Microsoft Messagebox一样工作,在那里你不传递父文件,它自己得到它。

提前感谢

2 个答案:

答案 0 :(得分:1)

我最后使用

调用了活动表单
Form.ActiveForm

所以这现在按预期工作:

Sub myMsgBox(ByVal MsgText As String) 
     Dim f as Form.ActiveForm
     formMsgbox.mainText = MsgText
     formMsgbox.size = new size (f.size.x / 2, f.size.y / 2)
     Dim Current_Screen As Screen = Screen.FromControl(f)
     If Current_Screen.Primary = False Then
         Dim HCenter = Current_Screen.Bounds.Left + _
         (((Current_Screen.Bounds.Right - Current_Screen.Bounds.Left) / 2) - ((formMsgbox.Width) / 2))
         Dim VCenter = (Current_Screen.Bounds.Bottom / 2) - ((formMsgbox.Height) / 2)
         formMsgbox.StartPosition = FormStartPosition.Manual
         formMsgbox.Location = New Point(HCenter, VCenter)
     Else
         formMsgbox.StartPosition = FormStartPosition.CenterScreen
     End If
     formMsgbox.Show()
End Sub

答案 1 :(得分:0)

如果需要获取调用表单,则必须添加其他参数。为了进一步简化,我将其转换为extension method

Imports System.Runtime.CompilerServices

Public Module Extensions
    <Extension()> _
    Public Sub myMsgBox(ByVal TargetForm As Form, ByVal MsgText As String)
        formMsgbox.mainText = MsgText
        formMsgbox.Size = New Size(TargetForm.Width / 2, TargetForm.Height / 2)
        Dim Current_Screen As Screen = Screen.FromControl(TargetForm)

        ...the rest of your code...
    End Sub
End Module

然后你可以像这样使用它:

Me.myMsgBox("Hello World!")