VB.NET - How to create Notification Popup window (from bottom) - vb.net

i want to show a form in the lower right of the taskbar but then when i use a dual monitor it just passes here is my code
Public Class BNotifyUI
Private Sub BNotifyUI_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Me.Visible = True
Dim x As Integer
Dim y As Integer
x = Screen.PrimaryScreen.WorkingArea.Width
y = Screen.PrimaryScreen.WorkingArea.Height = Me.Height
Me.ShowInTaskbar = False
Do Until x = Screen.PrimaryScreen.WorkingArea.Width = Me.Width
x = x - 1
Me.Location = New Point(x, y)
'text visible here
Loop
End Sub
Private Sub BNotifyUI_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
'Me.Close()
End Sub

Solved, Thanks for the tips guys ^_^
Dim x As Integer
Dim y As Integer
x = Screen.PrimaryScreen.WorkingArea.Width
y = Screen.PrimaryScreen.WorkingArea.Height - Me.Height - 10
Me.ShowInTaskbar = False
Do Until x = Screen.PrimaryScreen.WorkingArea.Width - Me.Width - 20
x = x - 1
Me.Location = New Point(x, y)
'text visible here
Loop

Related

Timer won't start in vb.net

I have started the timer with Game.Start() and by using a breakpoint I have determined that the timer is enabled, the interval is 100. However when stepping through my code, the timer sub is ignored and the paint sub is given priority. Therefore, the timer is never being run.
Here is the code which i am referring to :
Public Class Form1
Dim speed_s As Integer = 5
Dim speed_w As Integer = 5
Dim speed_d As Integer = 5
Dim speed_a As Integer = 5
Dim Enemy1 As New computerControlled(1, 1, Me)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
player1.Bounds = New Rectangle(player1.Location.X, player1.Location.Y, player1.Width, player1.Height)
'Enemy1.DrawEnemy(Me)
Game.Enabled = True
Game.Start()
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.W Then
player1.Top -= speed_w
ElseIf e.KeyCode = Keys.S Then
player1.Top += speed_s
ElseIf e.KeyCode = Keys.A Then
player1.Left -= speed_a
ElseIf e.KeyCode = Keys.D Then
player1.Left += speed_d
' Enemy1.enemypic.Left += 10 this moves enemy class well
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Game.Tick
'Enemy1.enemypic.Left += 10
Enemy1.Walk()
MsgBox("hi")
End Sub
Private MOUSE_X As Integer
Private MOUSE_Y As Integer
Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
MOUSE_X = e.X
MOUSE_Y = e.Y
End Sub
Private Sub Player_Paint(sender As Object, e As PaintEventArgs) Handles player1.Paint
Dim GFX As Graphics = e.Graphics
Dim BMP As Bitmap = Image.FromFile("c:\firaas\ball.png")
Dim center As Point = New Point(player1.Width \ 2, player1.Height \ 2)
Dim angle As Integer
Dim rad As Double
Dim CENTRE_X As Integer
Dim CENTRE_Y As Integer
'=========================
Using cyan As New Pen(Brushes.Cyan, 2)
GFX.ResetTransform()
GFX.TranslateTransform(CENTRE_X, CENTRE_Y)
GFX.RotateTransform(angle) ' angle in DEGREES!
'For Each pos As PointF In pat.Positions
' Dim r As New Rectangle(pos.X, pos.Y, 1, 1)
' r.Inflate(3, 3)
' GFX.DrawEllipse(cyan, r)
' Next
End Using
'===========================
Dim rotatematrix As New System.Drawing.Drawing2D.Matrix()
Dim srcRect As New RectangleF(64, 64, 64, 64)
Dim OffsetX As Single = Me.Width / 2 - player1.Width / 2
Dim OffsetY As Single = Me.Height / 2 - player1.Height / 2
CENTRE_X = player1.Location.X + player1.Width / 2
CENTRE_Y = player1.Location.Y + player1.Height / 2
rad = Math.Atan2(MOUSE_Y - CENTRE_Y, MOUSE_X - CENTRE_X)
angle = rad * (180 / Math.PI)
'================================================================================
BMP = New Bitmap(My.Resources.ball)
GFX.TranslateTransform(player1.Height / 2, player1.Width / 2)
GFX.RotateTransform(angle)
GFX.DrawImage(BMP, 64, 64, player1.Location.X, player1.Location.Y)
GFX.ResetTransform()
e.Graphics.TranslateTransform(player1.Height / 2, player1.Width / 2)
e.Graphics.RotateTransform(angle)
' BMP.RotateAt(angle, New PointF(player1.Location.X, player1.Location.Y))
e.Graphics.DrawImage(BMP, New Point(-player1.Width \ 2, -player1.Height \ 2))
e.Graphics.DrawImageUnscaled(BMP, New Point(0, 0))
player1.Invalidate()
End Sub
End Class
'latest project
If this is the complete code, it doesn't seem like you're setting your timer's interval.
So you're telling it, it can tick and what to do each tick but not actually setting a time for it to tick.
Game.Interval = 60000 '1 minute
Game.Enabled = True
Game.Start() 'I don't believe you need this line

Show DateTimePicker in Textbox

I've set DatetimePicker.Visible=False, and when I click on Textbox I want It to be displayed inside of Textbox. My code works for 1 Textbox, but not the other one:
Private Sub Show_DTP(Ctl As Control)
Dim x As Integer = 0
Dim y As Integer = 0
Dim Width As Integer = 0
Dim height As Integer = 0
Dim rect As Rectangle
Select Case Ctl.Name
Case "TxtTest"
rect = TxtTest.DisplayRectangle()
x = rect.X + TxtTest.Left
y = rect.Y + TxtTest.Top
Width = rect.Width + 4
height = rect.Height
With My_DTP
.SetBounds(x, y, Width, height)
.Visible = True
.Focus()
End With
Case "Txt2"
rect = Txt2.DisplayRectangle()
x = rect.X + Txt2.Left
y = rect.Y + Txt2.Top
Width = rect.Width + 4
height = rect.Height
With My_DTP
.SetBounds(x, y, Width, height)
.Visible = True
.Focus()
End With
End Select
End Sub
Private Sub Text_Click(sender As Object, e As EventArgs) Handles TxtTest.Click, Txt2.Click
Dim Txt As System.Windows.Forms.TextBox = DirectCast(sender, TextBox)
My_DTP.Visible = False
Show_DTP(Txt)
End Sub
What is wrong here ?
There is no need for the case statement: you do the same things regardless of the actual textbox.
I put two textboxes named "TxtTest" and "Txt2" on a form and added a DateTimePicker named "MyDTP". With the following code it behaved as you appear to want:
Option Infer On
Option Strict On
Public Class Form1
Private Sub Show_DTP(target As TextBox)
Dim rect As Rectangle = target.DisplayRectangle()
Dim x As Integer = rect.X + target.Left
Dim y As Integer = rect.Y + target.Top
Dim width = rect.Width + 4
Dim height = rect.Height
With MyDTP
.SetBounds(x, y, Width, height)
.Visible = True
.Focus()
End With
End Sub
Private Sub Text_Click(sender As Object, e As EventArgs) Handles TxtTest.Click, Txt2.Click
Dim Txt As System.Windows.Forms.TextBox = DirectCast(sender, TextBox)
MyDTP.Visible = False
Show_DTP(Txt)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MyDTP.Visible = False
End Sub
End Class

Zoom to mouse position after Graphics transform, in VB.Net

How to zoom to the cursor position after the graphics component has been transformed?
I want to be able to zoom to any of the test rectangles.
The panning is done with the Middle mouse button.
MouseWheel Event handles the zooming.
Ignore the DrawGrid method, it's just to get some visual reference.
Public Class Diagram
Dim renderOrigin As New Point
Dim zoom As Single = 1.0F
Dim startPoint As New Point
Dim isDragging As Boolean = False
Dim gridSpacing As Integer = 50
Dim testRects() As Rectangle = New Rectangle() {New Rectangle(-150, -150, 70, 25), _
New Rectangle(-10, -5, 70, 25), _
New Rectangle(100, 8, 70, 25), _
New Rectangle(300, 80, 70, 25)}
Sub New()
SetStyle(ControlStyles.ResizeRedraw, True)
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
g.TranslateTransform(renderOrigin.X, renderOrigin.Y)
g.ScaleTransform(zoom, zoom)
drawGrid(g)
g.FillRectangles(Brushes.Green, testRects.ToArray)
End Sub
Private Sub drawGrid(ByRef g As Graphics)
If zoom < 0.6 Then
Exit Sub
End If
Dim oX As Integer = renderOrigin.X
Dim oY As Integer = renderOrigin.Y
Dim maxStepX As Integer = Math.Ceiling((Width - oX) / gridSpacing) / zoom
Dim maxStepY As Integer = Math.Ceiling((Height - oY) / gridSpacing) / zoom
Dim minStepX As Integer = Math.Floor((oX * -1) / gridSpacing) / zoom
Dim minStepY As Integer = Math.Floor((oY * -1) / gridSpacing) / zoom
For x = minStepX To maxStepX
For y = minStepY To maxStepY
g.DrawLine(Pens.Black, x * gridSpacing, gridSpacing * y, (x + 1) * gridSpacing, gridSpacing * y)
g.DrawLine(Pens.Black, x * gridSpacing, gridSpacing * y, x * gridSpacing, (y + 1) * gridSpacing)
Next
Next
End Sub
Private Sub Diagram_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Middle Then
startPoint = New Point(e.X - renderOrigin.X, e.Y - renderOrigin.Y)
isDragging = True
End If
End Sub
Private Sub Diagram_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If isDragging Then
renderOrigin = New Point(e.X - startPoint.X, e.Y - startPoint.Y)
Invalidate()
End If
End Sub
Private Sub Diagram_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
isDragging = False
End Sub
Private Sub Diagram_MouseWheel(sender As Object, e As MouseEventArgs) Handles Me.MouseWheel
Dim i As Single = (e.Delta / Math.Abs(e.Delta)) / 100
zoom += i
If zoom < 0.1 Then zoom = 0.1
If zoom > 1.0 Then zoom = 1.0
renderOrigin = New Point(e.X - e.X * zoom, e.Y - e.Y * zoom)
Invalidate()
End Sub
End Class
1st. thing to know is what your mouse location is in control coordinates. It is part of the EventArguments in the scroll event. Save that location to a field.
2nd is you have to apply a TranslateTransform with the negativ saved location to move the zoom location to 0/0 then apply the ScaleTransform.
3rd move the picture back to it's original location by another TranslateTransform. now with the saved mouse location (not negated).
Thomas

Refreshing picturebox image in visual basic

I am coding a program that edits pictures, I have a picture box where I load a bitmap, the first image loads fine, but if I want to load a second image, the second image won't load and the first one stays, it won't refresh. How can I do to change the image in the picture box using 2 different bitmaps?
I have PictureBox3.Image = bm where bm is a bitmap variable, it loads fine, but then if I press the other button to load another bitmap in PictureBox3 (PictureBox3.Image = bm2), it won't load.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim clr As Integer
Dim ymax As Integer
Dim xmax As Integer
Dim x As Integer
Dim y As Integer
Dim bm As Bitmap = PictureBox1.Image
xmax = bm.Width - 1
ymax = bm.Height - 1
For y = 0 To ymax
For x = 0 To xmax
With bm.GetPixel(x, y)
clr = 0.21 * .R + 0.72 * .G + 0.07 * .B
End With
bm.SetPixel(x, y, _
Color.FromArgb(255, clr, clr, clr))
Next x
Next y
PictureBox3.Image = bm
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim clr As Integer
Dim ymax As Integer
Dim xmax As Integer
Dim x As Integer
Dim y As Integer
Dim bm2 As Bitmap = PictureBox1.Image
xmax = bm2.Width - 1
ymax = bm2.Height - 1
For y = 0 To ymax
For x = 0 To xmax
With bm2.GetPixel(x, y)
clr = (1 * .R + 1 * .G + 1 * .B) / 3
End With
bm2.SetPixel(x, y, _
Color.FromArgb(255, clr, clr, clr))
Next x
Next y
PictureBox3.Image = bm2
End Sub
End Class
Dim bm2 As Bitmap = new Bitmap(PictureBox1.Image)

Plotting sampling graph on VB.NET

I am collecting data from an Arduino and transmit on Visual Basic through the serial port. Now I wanted to plot a graph with time vs electrical energy (unit kWh) - time along the x-axis and electrical energy along the y-axis. Usually I am getting data of the current from Arduino.
Now I wanted to learn how to start plotting the graph. I need a simple example explaining plotting a graph for sample. I tried some example code. It seems they are not working.
How do I plot a graph time vs current read from serial? Once Visual Basic starts running, it should save data w.r.t system time and date.
Example available here
Current code
Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading
Imports System.Drawing
Public Class Form1
Dim myPort As Array
Dim Distance As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
PortComboBox.Items.AddRange(myPort)
BaudComboBox.Items.Add(9600)
BaudComboBox.Items.Add(19200)
BaudComboBox.Items.Add(38400)
BaudComboBox.Items.Add(57600)
BaudComboBox.Items.Add(115200)
ConnectButton.Enabled = True
DisconnectButton.Enabled = False
End Sub
Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
SerialPort1.PortName = PortComboBox.Text
SerialPort1.BaudRate = BaudComboBox.Text
SerialPort1.Open()
Timer1.Start()
'lblMessage.Text = PortComboBox.Text & " Connected."
ConnectButton.Enabled = False
DisconnectButton.Enabled = True
End Sub
Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
SerialPort1.Close()
DisconnectButton.Enabled = False
ConnectButton.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim k As Double
Dim distance As String = SerialPort1.ReadLine()
k = CDbl(distance)
ListBoxSensor.Text = k
Catch ex As Exception
End Try
End Sub
Private Sub Relay_ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_ON.Click
SerialPort1.Write("1")
End Sub
Private Sub Relay_Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_Off.Click
SerialPort1.Write("0")
End Sub
End Class
Add a picturebox, a timer a button and a textbox:
PictureBox1 size = 768, 279
timer interval to 500
Private img As Bitmap
Private imgClone As Bitmap
Private widthInterval As Integer
'the distance from the left side of picturebox where x axis starts
Private leftPad As Integer = 50
'the distance from the down side of picturebox where x axis is
Private downPad As Integer = 30
'the distance from the up side of picturebox where y axis ends
Private upPad As Integer = 50
'the distance from the right side of picturebox where x axis ends
Private rightPad As Integer = 80
Private rn As New Random
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
drawBack()
Timer1.Enabled = True
End Sub
Private Sub drawBack()
Static count As Boolean = False
Dim g As Graphics
'number of values in x axis e.g 1, 2, 3, ... representing time
Dim numX As Integer = 23
'number of values in y axis representing KW/h
Dim numY As Integer = 5
Dim stringFormat As New StringFormat()
'arreys to hold the text for both axies
Dim arrayTextX(numX), arrayTextY(numY - 1) As String
Dim i As Integer
'the distance from the right side of picturebox where x axis stops
Dim rightPad As Integer = 80
Dim brush As Brush = New SolidBrush(Color.FromArgb(245, 255, 255))
Dim pen As Pen = New Pen(Color.FromArgb(212, 212, 212), 1)
Dim height, x, y As Integer
'Run once
If count = True Then
Return
End If
count = True
stringFormat.Alignment = StringAlignment.Center
img = New Bitmap(PictureBox1.Width, PictureBox1.Height)
imgClone = New Bitmap(PictureBox1.Width, PictureBox1.Height)
g = Graphics.FromImage(img)
g.SmoothingMode = SmoothingMode.AntiAlias
g.Clear(Color.White)
'the distance in x axis between each value
widthInterval = CInt((PictureBox1.Width - leftPad - rightPad) / (numX + 1))
'the distance in y axis between each value
height = CInt((PictureBox1.Height - upPad - downPad) / (numY + 1))
'fill arrays with text
For i = 0 To numX - 1
arrayTextX(i) = (i + 1).ToString
Next
For i = 0 To numY - 1
arrayTextY(i) = ((i + 1) * height).ToString
Next
'fill background of graph with color
g.FillRectangle(brush, New Rectangle(leftPad, upPad, PictureBox1.Width - leftPad - rightPad + 1, _
PictureBox1.Height - downPad - upPad))
'vertical lines
x = leftPad
For i = 0 To numX - 1
x += widthInterval
g.DrawLine(pen, x, PictureBox1.Height - downPad, x, upPad)
g.DrawString(arrayTextX(i), New Font("Arial", 8), Brushes.Black, _
New Rectangle(x - 10, PictureBox1.Height - downPad + 3, 20, 20), stringFormat)
Next
'horizontal lines
stringFormat.Alignment = StringAlignment.Far
y = PictureBox1.Height - downPad
For i = 0 To numY - 1
y -= height
g.DrawLine(pen, leftPad, y, PictureBox1.Width - rightPad, y)
g.DrawString(arrayTextY(i), New Font("Arial", 8), Brushes.Black, _
New Rectangle(0, y - 6, leftPad - 5, 20), stringFormat)
Next
g.DrawString("KW/Hour", New Font("Arial", 8, FontStyle.Bold), Brushes.Black, _
New PointF(5, 5))
g.DrawString("Time", New Font("Arial", 8, FontStyle.Bold), Brushes.Black, _
New PointF(PictureBox1.Width - 50, PictureBox1.Height - 20))
'draws x axis
g.DrawLine(Pens.Black, New Point(leftPad, PictureBox1.Height - downPad), _
New Point(PictureBox1.Width - rightPad, PictureBox1.Height - downPad))
'draws y axis
g.DrawLine(Pens.Black, New Point(leftPad, PictureBox1.Height - downPad), _
New Point(leftPad, upPad))
g.Dispose()
PictureBox1.Image = img
imgClone = CType(img.Clone, Bitmap)
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim value, x As Integer
Dim g As Graphics
Dim pntEnd As Point
Static pnt As Point = New Point(-1, -1)
Static staticX As Integer = -1
Dim hour As Integer = DateTime.Now.Hour
Dim minute As Integer = DateTime.Now.Minute
Dim second As Integer = DateTime.Now.Second
second = minute * 60 + second
x = leftPad + hour * widthInterval + CInt(CDbl(widthInterval - 1) * CDbl(second) / 3600.0R)
If pnt.X >= 0 Then
'checks if the new points x coordinate is the same as the previous and returns
If x <= staticX Then
Return
End If
End If
GetValue(value)
pntEnd = New Point(x, PictureBox1.Height - value - downPad)
g = Graphics.FromImage(img)
g.SmoothingMode = SmoothingMode.AntiAlias
If pnt.X < 0 Then
g.DrawLine(Pens.Red, pntEnd, pntEnd)
Else
g.DrawLine(Pens.Red, pnt, pntEnd)
End If
g.Dispose()
pnt = pntEnd
staticX = x
PictureBox1.Invalidate()
End Sub
Private Sub GetValue(ByRef value As Integer)
'here you can take the value from arduino.
value = rn.Next(0, PictureBox1.Height - downPad - upPad) 'random value
End Sub
When you press Button1 the graph starts.
Calculate width of graph (x axis)
width = PictureBox1.Width - leftPad - rightPad
this width is equivelant with 24h or 86400sec. So you should set the timer interval to
Timer1.Interval = CInt((86400 / width ) * 1000) 'in milliseconds
There is no need to be that because the tick function checks if the
new point is the same as the previous. So set timer interval to 1 second.