?
Solved

msgbox VB.NET - set position screen centre

Posted on 2010-08-25
6
Medium Priority
?
2,920 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 532 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 532 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 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 268 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 400 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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