Solved

msgbox VB.NET - set position screen centre

Posted on 2010-08-25
6
2,699 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

16 Experts available now in Live!

Get 1:1 Help Now