Display a msgbox in the foreground

Solved
quent -  
 quent -
Hello,
I created a program with Microsoft Visual Basic 2010 Express. This program is very simple: I enter a number of hours, minutes, and seconds, and it counts down. When it reaches the end, I display a msgbox to notify that it's finished. So far, no problem.
My issue comes from the fact that when the countdown ends and I'm working on another software at that moment, it doesn't bring the window to the forefront, and I can't see that it's finished. I haven't found any solution online.

Thank you in advance for your responses.

18 answers

  1. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Yes, it's possible:

    Form1
     My.Computer.Audio.Play("C:\Windows\Media\Alarm01.wav", AudioPlayMode.BackgroundLoop) 'plays in loop Form2.ShowDialog()


    Form2
     'Stops the sound playback My.Computer.Audio.Stop() Me.Hide()


    --
    @+ The Woodpecker
    1
    1. quent
       
      That's exactly what I wanted, thank you very much!
      0
  2. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Hello,

    Why not play a sound instead of a MsgBox, like this:

     My.Computer.Audio.Play("C:\Windows\Media\Alarm01.wav", AudioPlayMode.WaitToComplete) 'plays completely, only stops at the end of the track


    the path to the wave file is for Windows 8.1, to be checked on the pc
    --
    @+ The Woodpecker
    0
  3. MrWhitediamond Posted messages 31 Status Member 1
     
    To create a MsgBox in the foreground, you must use one of the following values:

    16 --> OK
    17 --> OK + CANCEL
    18 --> ABORT + RETRY + IGNORE
    19 --> YES + NO + CANCEL
    20 --> YES + NO
    21 --> RETRY + CANCEL

    The window will display in CRITICAL, which means in the foreground but also with the Windows sound.

    Example code in vbscript:

    x= MsgBox ("The text of the window",20,"The title of the window")
    if x = 6 then //if the user clicks on YES
    .....
    elseif x = 7 then
    .....
    end if
    0
  4. quent
     
    Hello and thank you for your responses,
    to respond to cs_Le Pivert, indeed your method works and that’s the one I will use if I don’t find another solution but the problem is that I still have my PC sound on :) (unless there’s a command to automatically turn on the sound)

    To respond to MrWhitediamond, that doesn’t solve my problem because you did like my code but changed the OK button to yes and no buttons but it still doesn’t display the window in the foreground. However, you mentioned displaying it in critical, I don’t know what you meant by that but it reminded me that there is a parameter to display the window with an exclamation point as if there was an error which might have the effect of bringing it to the foreground. I haven’t been able to test it because I don’t remember that parameter so if someone knows it, I could try.

    Thanks again for trying to solve my problem.
    0
  5. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Here is an example

     MessageBox.Show("Operation canceled by the user!", "Open", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)


    put a dot after MessageBoxIcon. and you will have a context menu with other options

    --
    @+ Le Pivert
    0
    1. quent
       
      Thank you,
      I was able to test it but unfortunately it doesn't work.
      Otherwise, do you know of a function that allows to turn on the computer sound or play the ringtone even if the sound is off? (some applications do this)
      0
  6. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    I had my suspicions!

    For now, I suggest a second solution:

    Make your custom MsgBox with a form

    Go to project - Add a Windows Form.
    It should normally be named Form2, otherwise you can adjust it

    In form1, instead of calling your MsgBox, you put this:

     Form2.ShowDialog()


    And in your Form2, add a Label and a button with this code:

    Public Class Form2 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Me.Hide() End Sub Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.Text = "Custom MsgBox" Label1.Text = "This is a message" & Environment.NewLine & "this is the 2nd line" & Environment.NewLine & "this is the 3rd line" Label1.AutoSize = True TopMost = True 'Form displays above others ControlBox = False Button1.Text = "Close" End Sub End Class


    There you go, happy coding
    --
    @+ Le Pivert
    0
  7. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    For sound, try the files that start with Windows - like this:

    Windows - Battery alarm

    See if they work without sound

    --
    @+ The Woodpecker
    0
  8. quent
     
    Your method with form2 works perfectly!!! Thank you so much!
    I just need to adjust the window the way I want.

    For the sound, I tried to use the ringtone "Windows - Alarme batterie.wav" but it crashes and says it can't find the file while with "Alarm01.wav", everything goes well!
    Maybe it can't read files with spaces in their names...

    Thanks again!
    0
  9. quent
     
    Hello,
    I would like to ask you for additional help with my project:
    your idea of adding a ringtone interested me, so I will add one in addition to the message. I would like to have the message open at the same time as the ringtone, and if we close the message before the ringtone finishes, then we turn off the ringtone in the middle. Is that possible?
    0
  10. quent
     
    Hello,
    One more thing: can we either turn on the computer sound automatically or check if the computer sound is on?
    0
  11. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    No, I don't see it, edit a new post and you'll get more replies!

    --
    @+ The Woodpecker
    0
  12. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    You are lucky, I found what you are looking for!!!

    You put 3 buttons with this code:

    Option Strict On Imports System.Runtime.InteropServices Public Class Form1 <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr End Function Const WM_APPCOMMAND As UInteger = &H319 Const APPCOMMAND_VOLUME_UP As UInteger = &HA Const APPCOMMAND_VOLUME_DOWN As UInteger = &H9 Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Button1.Text = "Volume Up" Button2.Text = "Volume Down" Button3.Text = "Mute" End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_UP * &H10000, IntPtr)) End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_DOWN * &H10000, IntPtr)) End Sub Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H200EB0, IntPtr), CType(APPCOMMAND_VOLUME_MUTE * &H10000, IntPtr)) End Sub End Class 


    You click on Mute a second time to restore the sound. It's up to you to adapt it to your project.

    --
    See you later, The Woodpecker
    0
    1. quent
       
      Hello,
      have you tested this code?
      because when I use these lines
      Option Strict On Imports System.Runtime.InteropServices

      it underlines all the elements of my form1 and if I don't include them, it underlines the lines
      <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr End Function

      and in both cases it cannot compile.
      0
  13. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Of course I tested it!

    Here, try it:

    http://www.cjoint.com/data3/3AjiiXwTTly.htm

    --
    @+ The Woodpecker
    0
  14. quent
     
    The code you provided works well for me, but I understand what is wrong:
    I have the variables h, m, and s which correspond to the hours, minutes, and seconds of my countdown, and I retrieve this information from textboxes. Normally, it correctly performs the string-to-integer conversion, but with the lines
    Option Strict On Imports System.Runtime.InteropServices

    it no longer allows conversion from one type to another. Do you have any ideas?
    0
  15. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    Normally, you have your variables that display the error in red, you just need to right-click on them to fix it. Otherwise, the easiest way is to delete that line

    Option Strict On

    I put Option Strict On to code properly, it's a good habit to adopt

    --
    @+ Le Pivert
    0
  16. quent
     
    I found it! I had to use the CInt() function to convert my variable.
    So the code you gave me works fine, but I have another problem: if the sound is on and I mute it, it turns off the sound. Secondly, I can increase and decrease the sound, but I can't set a fixed value; however, I would like to turn on the sound and set its value so that it is always the same, and the functions you gave me don't allow me to do that.
    Do you know a way to fix the sound at a certain value or to know its current value to increase or decrease by the necessary amount?
    0
  17. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    What you want to do is a sound control application. I think associating it with your MsgBox is a bit too heavy. I propose an alternative.

    When you use this to increase the volume, it removes the mute mode:

    SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_UP * &H10000, IntPtr))


    If you do it 3 times it increases it threefold, I tested it:

     SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_UP * &H10000, IntPtr)) SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_UP * &H10000, IntPtr)) SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_UP * &H10000, IntPtr))


    The advantage with 3 lines of code is that you remove the mute mode and increase the volume three times as much.

    Have a good weekend
    --
    @+ The Woodpecker
    0
  18. quent
     
    Hello,
    I adapted your idea to get exactly the volume I want:
    I lower the sound 50 times because the maximum sound of my PC is 100 and the line
    SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_DOWN * &H10000, IntPtr))

    lowers it by 2, so I'm at 0 and then I increase it the number of times I want (for example 15 times if I want a volume of 30) and in the process it removes the mute. It gives:
    For i = 1 To 50 SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_DOWN * &H10000, IntPtr)) Next For i = 1 To 15 SendMessage(Me.Handle, WM_APPCOMMAND, CType(&H30292, IntPtr), CType(APPCOMMAND_VOLUME_UP * &H10000, IntPtr)) Next

    it's not very optimized but it works well.
    thank you very much for your help!
    0