0

Is there a way to detect and access properties of an active WinForm messagebox spawned from the MessageBox.Show() method within the same WinForm application?

TheLegendaryCopyCoder
  • 1,658
  • 3
  • 25
  • 46
  • 1
    MessageBox doesn't seem to have any properties https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox%28v=vs.110%29.aspx You might want to create a custom form to add and then read properties in it – Val Aug 18 '15 at 09:54
  • I assume we are speaking of a dialog that inherits from MessageBox? – sara Aug 18 '15 at 09:56
  • Do you want to get the form which has popped up a messagebox? – Shaharyar Aug 18 '15 at 09:56
  • @Val Cool, that would be my choice too but I am looking for a short term work around. – TheLegendaryCopyCoder Aug 18 '15 at 10:04
  • 1
    It is technically possible, pinvoke backdoor required. Like findDialog() in [this post](http://stackoverflow.com/a/2259213/17034). Short-term you probably ought to use Edit > Find in Files. – Hans Passant Aug 18 '15 at 10:23

2 Answers2

1

MessageBox doesn't seem to have any exposed properties https://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox%28v=vs.110%29.aspx. You might want to create a custom form to add and then read properties in it.

public class NewForm : Form
{
   TextBox textProperty = new TextBox(); // <- it's actually a just field but you get the idea
   public NewForm()
   {
      //can modify properties here or from the parent form which spawns this one
   }
}
Val
  • 1,548
  • 1
  • 20
  • 36
0

If you mean window title and displayed message, you could use following code of mine

Public Shared Function GetDialogText() As String
    Dim dialog As IntPtr = Diagnostics.Process.GetCurrentProcess.MainWindowHandle
    Dim title = ""
    Const WM_COPY As UInteger = &H301
    WinDlls.SendMessage(dialog, WM_COPY, IntPtr.Zero, IntPtr.Zero)
    Dim msg = My.Computer.Clipboard.GetText()
    If Not msg.Contains("---") Then ' dialog not standard MessageBox
        Dim sb As New System.Text.StringBuilder(1000)
        WinDlls.GetWindowText(dialog, sb, sb.Capacity)
        title = sb.ToString & ": "
    End If
    Return title & msg
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Public Shared Function SendMessage(hWnd As IntPtr, Msg As UInteger,
    wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
VV5198722
  • 374
  • 5
  • 19