Home » VB.NetRSS

How to create an alert

How do i create an alert using DateTimePicker1 and MonthCalendar1? 

There are two seperate forms. The main one, where the one opens from and the other one where you set the date/time. And how do you integrate the notification with NotifyIcon1?

I have taken a look at this: http://blogs.msdn.com/b/coding4fun/archive/2006/10/31/908744.aspx. However the following link is regarding the 2005 version of Visual Studio. 

Any help will be greatly appreciated. Thanks. 

 

4 Answers Found

 

Answer 1

Ok, so I may have found a solution to it now. Here is my code so far: 

 

PublicClass reminders

  PublicSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim t1 AsString = DateTimePicker1.Value
    Dim t2 AsString = System.DateTime.Now

    Label3.Text = DateTimePicker1.Value



  EndSubPrivateSub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
    If Label3.Text = Label2.Text Then
      MessageBox.Show("Alert")
    EndIfEndSubPrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Label2.Text = System.DateTime.Now
    Label3.Text = DateTimePicker1.Value

    If Label3.Text = Label2.Text Then
      MessageBox.Show("Alert")
    EndIfEndSubPrivateSub reminders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
    Timer1.Start()
  EndSubEndClass

 Now my only problem is how do I hide that form from visibility? I need it so that the form is working in the background but the user can't see it until the alert is called. 

 

 

Answer 2

Try this with a DateTimePicker with the format property set to time and the UP/Down property set to true . Add a textbox for your alert message , a notifyIcon and a button to start the timer . It shows the Balloon on the NotifyIcon and also flashes the tackbar .

Imports System.Runtime.InteropServices

Public Class Form1
    <StructLayout(LayoutKind.Sequential)> _
    
Public Structure FLASHWINFO
        
Public cbSize As UInt32
        
Public hwnd As IntPtr
        
Public dwFlags As Int32
        
Public uCount As UInt32
        
Public dwTimeout As Int32
    
End Structure 'FLASHWINFO

    Public Enum FLASHWINFOFLAGS
        
' This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
        FLASHW_ALL = &H3 ' Flash both the window caption and taskbar button. 

        FLASHW_CAPTION = &H1 ' Flash the window caption. 
        FLASHW_STOP = 0 ' Stop flashing. The system restores the window to its original state. 
        FLASHW_TIMER = &H4 ' Flash continuously, until the FLASHW_STOP flag is set. 
        FLASHW_TIMERNOFG = &HC ' Flash continuously until the window comes to the foreground. 
        FLASHW_TRAY = &H2

    
End Enum 'FLASHWINFOFLAGS

    <DllImport("user32.dll")> _
    
Public Shared Function FlashWindowEx(ByRef pfwi As FLASHWINFO) As Integer
    End Function

    Private Sub SetTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetTimer.Click
        Timer1.Interval = 1000
        Timer1.Start()
        
Me.WindowState = FormWindowState.Minimized
    
End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        
If Format(DateTimePicker1.Value, "LongTime") = Format(Now, "LongTime"Then
            NotifyIcon1.ShowBalloonTip(1, "Alert!!""Click Balloon to see Alert", ToolTipIcon.Warning)
            
Dim fw As New FLASHWINFO
            fw.cbSize = Convert.ToUInt32(Marshal.SizeOf(
GetType(FLASHWINFO)))
            fw.hwnd = 
Me.Handle
            fw.dwFlags = 
CType(FLASHWINFOFLAGS.FLASHW_ALL Or FLASHWINFOFLAGS.FLASHW_TIMER Or FLASHWINFOFLAGS.FLASHW_TRAY, Int32) '
            fw.dwTimeout = 0
            FlashWindowEx(fw)
        
End If
        DateTimePicker1.Value = DateTimePicker1.Value.AddMinutes(1)
    
End Sub

    Private Sub NotifyIcon1_BalloonTipClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NotifyIcon1.BalloonTipClicked
        Timer1.Stop()
        
Me.WindowState = FormWindowState.Normal
    
End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        NotifyIcon1.Visible = 
False
    End Sub

    Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
        
If Me.WindowState = FormWindowState.Normal Then
            Dim fw As New FLASHWINFO
            fw.cbSize = Convert.ToUInt32(Marshal.SizeOf(
GetType(FLASHWINFO)))
            fw.hwnd = 
Me.Handle
            fw.dwFlags = 
CType(FLASHWINFOFLAGS.FLASHW_STOP, Int32) '
            fw.dwTimeout = 0
            FlashWindowEx(fw)
        
End If
    End Sub
End
 Class


Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
 

Answer 3

Thanks bdbodger for your friendly assistance and support!

 Now my only problem is how do I hide that form from visibility? I need it so that the form is working in the background but the user can't see it until the alert is called. 

 

Hi TusharBhatia,

 

Thanks for your post!

 

According to your description of problem, you are likely to create a tray application implementing alarm clock functionality.

If I have misunderstood anything, please feel free to let me know.

 

Then please take the following steps:

 

1.       Create a simple system tray application first.

Here an article about it, you can download the source code and do a little bit of modifications.

Create a System Tray Application in VB.NET

 

2.       Let the application implementing alarm clock functionality.

Here is my code:

 

PublicClassPopupForm

 

    PublicSubNew()

        InitializeComponent()

        Icon = My.Resources.TrayIcon

    EndSub

 

    PrivateSub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles CancelFormButton.Click

        Me.DialogResult = Windows.Forms.DialogResult.Cancel

        Me.Close()

    EndSub

 

    PrivateSub CloseAppButton_Click(ByVal sender AsObject, ByVal e As System.EventArgs) _

    Handles CloseAppButton.Click

        Me.DialogResult = Windows.Forms.DialogResult.Abort

        Me.Close()

    EndSub

 

    PrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Timer1.Interval = 1000

        Label1.Text = System.DateTime.Now

        'Label2.Text = DateTimePicker1.Value

 

        If Label2.Text = Label1.Text Then

            MessageBox.Show("Alert")

        EndIf

    EndSub

 

    PrivateSub PopupForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

        Timer1.Start()

    EndSub

 

    PrivateSub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

        If Label2.Text = Label1.Text Then

            MessageBox.Show("Alert")

        EndIf

    EndSub

 

    PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim t1 As String = DateTimePicker1.Value

        Dim t2 As String = System.DateTime.Now

 

        Label2.Text = DateTimePicker1.Value

    EndSub

EndClass

 

Here is the illustration:

 

I hope this will help resolve your problem, and I look forward to hearing from you soon.

 

Best Regards,

 

Answer 4

Hi Calvin,

Thanks for the reply everyone - it all really helped. I used the link that calvin showed me. Thank you very much everyone - I really appreciate your help. 

 

 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter