Solved

msgbox VB.NET - set position screen centre

Posted on 2010-08-25
6
2,660 Views
Last Modified: 2012-05-10
Hi, how to set msgbox position at screen centre or form ... in VB.NET? I have to use msgbox and not messagebox (due to VB6 conversion project). Does it work with messagebox?

In dual screen or large resolution ... the msgbox goes crazy. Any code sample and help is much appreciated. Cheers.

0
Comment
Question by:tjoseph833
6 Comments
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 133 total points
ID: 33519815
Here is a complete code how to center "messaageBox"...
I am trying to find out about MsgBox
http://www.freevbcode.com/ShowCode.asp?ID=952
0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 133 total points
ID: 33519820
Another Option is make you "Own" MessageBox Creating a new Form and Open it as ShowDialog whenever you want
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 67 total points
ID: 33519888
if you want to build your own messagebox, chech http://www.emoreau.com/Entries/Articles/2006/01/A-custom-MessageBox.aspx
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 100 total points
ID: 33520625
this is what you do.

1. add the following declarations:

Private Const MSG_BOX_TITLE As String = "MsgBoxTitle"

    Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" _
    (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer

    <DllImport("user32.dll")> _
    Public Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
    End Function


2. add the following handler:

 Sub BackgroundProcess()
        Dim h As IntPtr = Nothing
        Dim rct As RECT
        Dim res As Int32 = 0
        While (res <> 1)
            h = FindWindow(Nothing, MSG_BOX_TITLE )
            res = GetWindowRect(h, rct)
        End While

        Dim x As Int32 = (Screen.PrimaryScreen.Bounds.Width - (rct.Right - rct.Left)) / 2
        Dim y As Int32 = (Screen.PrimaryScreen.Bounds.Height - (rct.Bottom - rct.Top)) / 2

        MoveWindow(h, x, y, rct.Right - rct.Left, rct.Bottom - rct.Top, True)
    End Sub

3. just before calling you MsgBox line, add the following:

  Dim t As Thread = New Thread(AddressOf BackgroundProcess)
        t.Start()

        Microsoft.VisualBasic.MsgBox("MsgBox Body...", MsgBoxStyle.ApplicationModal, MSG_BOX_TITLE )

and that's it.

the thread will wait till your msg box is display and then it will move it with a blink of an eye to the center of the screen.
0
 

Author Comment

by:tjoseph833
ID: 33702627
thanks everyone ...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Launch Process with NOT as Administrator 15 66
Total in textboxes 9 35
SQl help with selection 14 44
Round up to 100% in .NET 10 43
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now