how to call event to another event (controls are dynamically created) - vb.net

I want to call the event of button even when I click on PictureBox or label.
Or you can give me alternative method to create just the image I include. (Maybe they call it OjectListView i guess - note: I don't know bout OLV).
I don't have other choice so I use button just like in this image
.
I'm not an expert to help me please. Thanks in advance.
Imports MySql.Data.MySqlClient
Public Class Form2<br>
Dim Sql As String<br>
Public conn As MySqlConnection<br>
'Public cmd As New MySqlCommand<br>
'Public reader As MySqlDataReader<br>
Public adapter As MySqlDataAdapter<br>
Public table As New DataTable
Dim x As Integer = 5
Dim y As Integer = 5
Public Sub HandleButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim str As String = btn.Tag
MessageBox.Show(str)
End Sub
Private Sub HandleLabelClick(ByVal sender As Object, ByVal e As EventArgs)
Dim lbl As Label = DirectCast(sender, Label)
Dim str As String = lbl.Text
MessageBox.Show(str)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Size = New Size(1197, 738)
conn = New MySqlConnection("server=localhost; user=root; pass=; port=3306; database=db_sample_animelist")
Try
conn.Open()
Catch ex As Exception
MessageBox.Show("Error Connecting to Database.", "Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End Try
' This line of code is to get all data to database
Sql = "SELECT * FROM tbl_animelist"
adapter = New MySqlDataAdapter(Sql, conn)
adapter.Fill(table)
Dim i As Integer = 1
' This line of code is to display data from database
For Each row In table.Rows
Dim list_button As New Button
list_button.Name = "list_button" & i
list_button.Size = New Size(472, 184)
list_button.Location = New Point(x, y)
'list_button.FlatStyle = FlatStyle.Flat
list_button.Tag = row.item("id")
AddHandler list_button.Click, AddressOf Me.HandleButtonClick
Dim picbox As New PictureBox
picbox.Name = "picbox" & i
picbox.Size = New Size(121, 162)
picbox.Location = New Point(10, 10)
picbox.BackColor = Color.Transparent
Try
Dim arrImage() As Byte
arrImage = row.item("image")
Dim mstream As New System.IO.MemoryStream(arrImage)
picbox.Image = Image.FromStream(mstream)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
list_button.Controls.Add(picbox)
'AddHandler picbox.Click, AddressOf HandlePicClick
Dim lbl_title As New Label
lbl_title.Name = "lbl_title" & i
lbl_title.AutoSize = False
lbl_title.Size = New Size(330, 20)
lbl_title.AutoEllipsis = True
lbl_title.BackColor = Color.DodgerBlue
lbl_title.Location = New Point(136, 10)
lbl_title.Text = row.item("name")
'label.Font = New Font(label.Font, FontStyle.Bold)
lbl_title.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
lbl_title.Tag = i
list_button.Controls.Add(lbl_title)
AddHandler lbl_title.Click, AddressOf HandleLabelClick
Dim lbl_rating As New Label
lbl_rating.Name = "lbl_rating" & i
lbl_title.AutoSize = False
lbl_rating.Size = New Size(76, 20)
lbl_rating.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
lbl_rating.Location = New Point(136, 35)
lbl_rating.BackColor = Color.Transparent
lbl_rating.Text = "Rating :"
list_button.Controls.Add(lbl_rating)
AddHandler lbl_rating.Click, AddressOf HandleLabelClick
Dim lbl_rate As New Label
lbl_rate.Name = "lbl_rate" & i
lbl_rate.AutoSize = False
lbl_rate.Size = New Size(49, 20)
lbl_rate.Location = New Point(211, 35)
lbl_rate.Text = row.item("rating")
lbl_rate.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
lbl_rate.BackColor = Color.Transparent
list_button.Controls.Add(lbl_rate)
AddHandler lbl_rate.Click, AddressOf HandleLabelClick
Dim lbl_ep As New Label
lbl_ep.Name = "lbl_ep" & i
lbl_ep.AutoSize = False
lbl_ep.Size = New Size(76, 20)
lbl_ep.Location = New Point(136, 55)
lbl_ep.Text = "Episodes :"
lbl_ep.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
lbl_ep.BackColor = Color.Transparent
list_button.Controls.Add(lbl_ep)
AddHandler lbl_ep.Click, AddressOf HandleLabelClick
Dim lbl_ep_count As New Label
lbl_ep_count.Name = "lbl_ep_count" & i
lbl_ep_count.AutoSize = False
lbl_ep_count.Size = New Size(142, 20)
lbl_ep_count.Location = New Point(211, 55)
lbl_ep_count.Text = row.item("episodes")
lbl_ep_count.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
lbl_ep_count.BackColor = Color.Transparent
list_button.Controls.Add(lbl_ep_count)
AddHandler lbl_ep_count.Click, AddressOf HandleLabelClick
Dim lbl_aired As New Label
lbl_aired.Name = "lbl_aired" & i
lbl_aired.AutoSize = False
lbl_aired.Size = New Size(76, 20)
lbl_aired.Location = New Point(136, 75)
lbl_aired.Text = "Aired :"
lbl_aired.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
lbl_aired.BackColor = Color.Transparent
list_button.Controls.Add(lbl_aired)
AddHandler lbl_aired.Click, AddressOf HandleLabelClick
Dim lbl_aired_date As New Label
lbl_aired_date.Name = "lbl_aired_date" & i
lbl_aired_date.AutoSize = False
lbl_aired_date.AutoEllipsis = True
lbl_aired_date.Size = New Size(255, 20)
lbl_aired_date.Location = New Point(211, 75)
lbl_aired_date.Text = row.item("aired")
lbl_aired_date.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
lbl_aired_date.BackColor = Color.Transparent
list_button.Controls.Add(lbl_aired_date)
AddHandler lbl_aired_date.Click, AddressOf HandleLabelClick
Dim lbl_genres As New Label
lbl_genres.Name = "lbl_genres" & i
lbl_genres.AutoSize = False
lbl_genres.Size = New Size(76, 20)
lbl_genres.Location = New Point(136, 95)
lbl_genres.Text = "Genres :"
lbl_genres.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
lbl_genres.BackColor = Color.Transparent
list_button.Controls.Add(lbl_genres)
AddHandler lbl_genres.Click, AddressOf HandleLabelClick
Dim lbl_genre_list As New Label
lbl_genre_list.Name = "lbl_genre_list" & i
lbl_genre_list.AutoSize = False
lbl_genre_list.AutoEllipsis = True
lbl_genre_list.Size = New Size(255, 20)
lbl_genre_list.Location = New Point(211, 95)
lbl_genre_list.Text = row.item("genres")
lbl_genre_list.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
lbl_genre_list.BackColor = Color.Transparent
list_button.Controls.Add(lbl_genre_list)
AddHandler lbl_genre_list.Click, AddressOf HandleLabelClick
Dim lbl_synopsis As New Label
lbl_synopsis.Name = "lbl_synopsis" & i
lbl_synopsis.AutoSize = False
lbl_synopsis.AutoEllipsis = True
lbl_synopsis.Size = New Size(330, 68)
lbl_synopsis.Location = New Point(136, 115)
lbl_synopsis.Text = row.item("synopsis")
lbl_synopsis.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
lbl_synopsis.BackColor = Color.Transparent
lbl_synopsis.MaximumSize = New Size(330, 68)
list_button.Controls.Add(lbl_synopsis)
AddHandler lbl_synopsis.Click, AddressOf HandleLabelClick
Panel1.Controls.Add(list_button)
' This line of code is to set location for every button
x += 480
If i Mod 2 = 0 Then
x = 5
y += 184 + 5
End If
' Every loop it will add one for the purpose of naming a control
i += 1
Next
End Sub
End Class

You have to assign an eventhandler to the picturebox event handler at runtime like described here :
https://msdn.microsoft.com/en-us/library/dfty2w4e(v=vs.110).aspx
I am not used to vb but it should be something like following :
AddHandler picbox.Click, AddressOf HandleButtonClick
Of course your logic in the event handler needs to be modified as well because it is casting the sender to a button which in the case of a click on the image would be an image of course.
This should get you on the right track.

Related

ListBox Scroll Turns Black / Glitches On Vertical Scroll - Redraw Issue?

https://imgur.com/a/gTuKRnf
Video example in link above. (Short, 30 seconds)
listBox works fine until scrolling is needed.
When scrolling, listBox changes background, turns black, glitches out.
Have attempted OwnerDrawFixed mode but then listBox shows empty.
Has anyone else seen this before?
After Scroll:
After drag select/click:
Code Snippets:
[Form1 Designer.vb]
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
Me.PictureBox2 = New System.Windows.Forms.PictureBox()
Me.PictureBox3 = New System.Windows.Forms.PictureBox()
Me.PictureBox4 = New System.Windows.Forms.PictureBox()
Me.LinkLabel1 = New System.Windows.Forms.LinkLabel()
Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
Me.Button2 = New System.Windows.Forms.Button()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.PictureBox3, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.PictureBox4, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 200
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Courier New", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.TextBox1.Location = New System.Drawing.Point(12, 77)
Me.TextBox1.Margin = New System.Windows.Forms.Padding(6, 5, 6, 5)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(293, 31)
Me.TextBox1.TabIndex = 0
'
'Button1
'
Me.Button1.Font = New System.Drawing.Font("Courier New", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.Button1.Location = New System.Drawing.Point(153, 118)
Me.Button1.Margin = New System.Windows.Forms.Padding(6, 5, 6, 5)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(192, 35)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'PictureBox1
'
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(12, 12)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(44, 44)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox1.TabIndex = 2
Me.PictureBox1.TabStop = False
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Courier New", 15.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
Me.Label1.Location = New System.Drawing.Point(62, 23)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(283, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "My App"
'
'ListBox1
'
Me.ListBox1.Font = New System.Drawing.Font("Courier New", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.ListBox1.FormattingEnabled = True
Me.ListBox1.ItemHeight = 16
Me.ListBox1.Location = New System.Drawing.Point(12, 161)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(333, 180)
Me.ListBox1.TabIndex = 4
'
'OpenFileDialog1
'
Me.OpenFileDialog1.AddExtension = False
Me.OpenFileDialog1.FileName = "OpenFileDialog1"
Me.OpenFileDialog1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
Me.OpenFileDialog1.FilterIndex = 2
Me.OpenFileDialog1.RestoreDirectory = True
'
'PictureBox2
'
Me.PictureBox2.Image = CType(resources.GetObject("PictureBox2.Image"), System.Drawing.Image)
Me.PictureBox2.Location = New System.Drawing.Point(314, 77)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(31, 31)
Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox2.TabIndex = 5
Me.PictureBox2.TabStop = False
'
'PictureBox3
'
Me.PictureBox3.Image = CType(resources.GetObject("PictureBox3.Image"), System.Drawing.Image)
Me.PictureBox3.Location = New System.Drawing.Point(12, 118)
Me.PictureBox3.Name = "PictureBox3"
Me.PictureBox3.Size = New System.Drawing.Size(31, 31)
Me.PictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox3.TabIndex = 6
Me.PictureBox3.TabStop = False
'
'PictureBox4
'
Me.PictureBox4.Image = CType(resources.GetObject("PictureBox4.Image"), System.Drawing.Image)
Me.PictureBox4.Location = New System.Drawing.Point(314, 355)
Me.PictureBox4.Name = "PictureBox4"
Me.PictureBox4.Size = New System.Drawing.Size(31, 31)
Me.PictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.PictureBox4.TabIndex = 7
Me.PictureBox4.TabStop = False
'
'LinkLabel1
'
Me.LinkLabel1.AutoSize = True
Me.LinkLabel1.Font = New System.Drawing.Font("Courier New", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.LinkLabel1.LinkColor = System.Drawing.Color.Black
Me.LinkLabel1.Location = New System.Drawing.Point(12, 373)
Me.LinkLabel1.Name = "LinkLabel1"
Me.LinkLabel1.Size = New System.Drawing.Size(126, 15)
Me.LinkLabel1.TabIndex = 8
Me.LinkLabel1.TabStop = True
Me.LinkLabel1.Text = "label"
Me.LinkLabel1.VisitedLinkColor = System.Drawing.Color.Black
'
'NotifyIcon1
'
Me.NotifyIcon1.Icon = CType(resources.GetObject("NotifyIcon1.Icon"), System.Drawing.Icon)
Me.NotifyIcon1.Text = "My App"
Me.NotifyIcon1.Visible = True
'
'Button2
'
Me.Button2.Font = New System.Drawing.Font("Courier New", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.Button2.Location = New System.Drawing.Point(153, 355)
Me.Button2.Margin = New System.Windows.Forms.Padding(6, 5, 6, 5)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(152, 31)
Me.Button2.TabIndex = 9
Me.Button2.Text = "Button"
Me.Button2.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(13.0!, 23.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(357, 397)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.LinkLabel1)
Me.Controls.Add(Me.PictureBox4)
Me.Controls.Add(Me.PictureBox3)
Me.Controls.Add(Me.PictureBox2)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Font = New System.Drawing.Font("Courier New", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "My App"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.PictureBox2, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.PictureBox3, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.PictureBox4, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Timer1 As Timer
Friend WithEvents TextBox1 As TextBox
Friend WithEvents Button1 As Button
Friend WithEvents PictureBox1 As PictureBox
Friend WithEvents Label1 As Label
Friend WithEvents ListBox1 As ListBox
Friend WithEvents OpenFileDialog1 As OpenFileDialog
Friend WithEvents PictureBox2 As PictureBox
Friend WithEvents PictureBox3 As PictureBox
Friend WithEvents PictureBox4 As PictureBox
Friend WithEvents LinkLabel1 As LinkLabel
Friend WithEvents NotifyIcon1 As NotifyIcon
Friend WithEvents Button2 As Button
End Class
[Form1.vb]
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If System.IO.File.Exists("myfile.txt") Then
Dim lines = File.ReadAllLines("myfile.txt")
ListBox1.Items.Clear()
ListBox1.Items.AddRange(lines)
End If
End Sub
Example of 'myfile.txt' contents:
iexplore1
iexplore2
iexplore3
iexplore4
... [And so on]

Get PixelColor at Top/Left Pos to Change TIme String On Screensaver

I was told to edit the program up here, so here is small program that shows what I am trying to accomplish (learn)
I am using the screen.Bounds as you suggested, but the problem is still that the mouse gets it's location only when it is within the form. Outside of the form the mouse has no clue where it is, and therefore cant tell me the color at its mouse position.
I feel I'm so close to getting this to work...
Public Class Form10
'Sample Code
'Needs 2 Textboxes, names txtStatic, txtMouse
'Needs 2 Pictureboxes, names PBStatic, PBMouse
'Needs Timer1 set to 1000
'Goal is to show pixelcolor of screen under
'form's corner, top/left at Point(450,74) "named STATIC"
'and show color of Screen Pixel when mouse is clicked
'anywhere on Screen "named MOUSE"
Dim ScreenX, ScreenY As Integer
Dim SColor, MColor As Color
Dim MouseLoc, StaticLoc As Point
Private Sub Form10_Load(sender As Object, e As EventArgs) _
Handles Me.Load
Me.Location = New Point(450, 75)
StaticLoc = Me.Location
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ScreenX = My.Computer.Screen.Bounds.Width
ScreenY = My.Computer.Screen.Bounds.Height
Dim bm As New Bitmap(ScreenX, ScreenY)
SColor = GetPixColor(StaticLoc)
MColor = GetPixColor(MouseLoc)
TxtStatic.Text = StaticLoc.ToString
TxtMouse.Text = MouseLoc.ToString
PBStatic.BackColor = SColor
PBMouse.BackColor = MColor
End Sub
Function GetPixColor(ByVal Location As Point) As Color
Using b As New Bitmap(1, 1)
Using g As Graphics = Graphics.FromImage(b)
g.CopyFromScreen(Location, Point.Empty, New Drawing.Size(1, 1))
End Using
Return b.GetPixel(0, 0)
End Using
End Function
Private Sub Form10_MouseMove(sender As Object,
e As MouseEventArgs) _
Handles Me.MouseMove
If e.Button = MouseButtons.Left Then
Dim x1, y1 As Integer
x1 = e.Location.X - mouseloc.X
y1 = e.Location.Y - mouseloc.Y
Me.Location = New Point(Me.Location.X + x1,
Me.Location.Y + y1)
End If
End Sub
Private Sub Form10_MouseClick(sender As Object,
e As MouseEventArgs) _
Handles Me.MouseClick
MouseLoc = e.Location
End Sub
End Class
HERE IS THE DESIGNER IN CASE SOMETHING IS WRONG THERE TOO !!
_
Partial Class Form10
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.PBMouse = New System.Windows.Forms.PictureBox()
Me.TxtMouse = New System.Windows.Forms.TextBox()
Me.TxtStatic = New System.Windows.Forms.TextBox()
Me.PBStatic = New System.Windows.Forms.PictureBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
CType(Me.PBMouse, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.PBStatic, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Timer1
'
Me.Timer1.Interval = 1000
'
'PBMouse
'
Me.PBMouse.Location = New System.Drawing.Point(35, 341)
Me.PBMouse.Name = "PBMouse"
Me.PBMouse.Size = New System.Drawing.Size(337, 50)
Me.PBMouse.TabIndex = 8
Me.PBMouse.TabStop = False
'
'TxtMouse
'
Me.TxtMouse.Font = New System.Drawing.Font("Segoe UI", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TxtMouse.Location = New System.Drawing.Point(35, 260)
Me.TxtMouse.Name = "TxtMouse"
Me.TxtMouse.Size = New System.Drawing.Size(337, 64)
Me.TxtMouse.TabIndex = 7
'
'TxtStatic
'
Me.TxtStatic.Font = New System.Drawing.Font("Segoe UI", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TxtStatic.Location = New System.Drawing.Point(35, 109)
Me.TxtStatic.Name = "TxtStatic"
Me.TxtStatic.Size = New System.Drawing.Size(337, 64)
Me.TxtStatic.TabIndex = 6
'
'PBStatic
'
Me.PBStatic.Location = New System.Drawing.Point(35, 43)
Me.PBStatic.Name = "PBStatic"
Me.PBStatic.Size = New System.Drawing.Size(337, 50)
Me.PBStatic.TabIndex = 5
Me.PBStatic.TabStop = False
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(39, 14)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 29)
Me.Label1.TabIndex = 9
Me.Label1.Text = "Static"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(39, 224)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(128, 29)
Me.Label3.TabIndex = 11
Me.Label3.Text = "MousePos"
'
'Form10
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(12.0!, 25.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(412, 450)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.PBMouse)
Me.Controls.Add(Me.TxtMouse)
Me.Controls.Add(Me.TxtStatic)
Me.Controls.Add(Me.PBStatic)
Me.Name = "Form10"
Me.Text = "Form10"
Me.TopMost = True
CType(Me.PBMouse, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.PBStatic, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Timer1 As Timer
Friend WithEvents PBMouse As PictureBox
Friend WithEvents TxtMouse As TextBox
Friend WithEvents TxtStatic As TextBox
Friend WithEvents PBStatic As PictureBox
Friend WithEvents Label1 As Label
Friend WithEvents Label3 As Label
End Class

Create controls dinamically in a form got by a string value

In my current project I intend to do something like a file explorer. When I click with the right mouse button over the form it opens another form that simulates a MsgBox with 2 buttons, one to create a folder and another to create a text file. The program stores the name of the form that was open when clicked with the mouse and should create the desired option. The problem is that it does not create and does not give errors.
prevForm is a string which contains the previous form name.
Sorry for bad English
Private Sub pasta_Click(sender As Object, e As EventArgs) Handles pasta.Click
Dim nomePasta = InputBox("Nome da Pasta: ", "Nova Pasta", "Nova pasta")
Dim novoForm As New Form With {
.Name = nomePasta,
.Text = nomePasta,
.Size = New Point(816, 489),
.BackColor = Color.FromName("ActiveCaption")
}
Dim novaPicBox As PictureBox
novaPicBox = New PictureBox With {
.Size = New Point(60, 60),
.Location = New Point(720, 21)
}
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaPicBox)
Dim novaLbl As Label
novaLbl = New Label With {
.Text = nomePasta,
.Location = New Point(720, 85)
}
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaLbl)
Me.Close()
End Sub
Private Sub fichtexto_Click(sender As Object, e As EventArgs) Handles fichtexto.Click
Dim nomeFichTexto = InputBox("Nome do Ficheiro de Texto: ", "Novo Ficheiro de Texto", "Novo Ficheiro de Texto")
Dim novaPicBox As PictureBox
novaPicBox = New PictureBox With {
.Size = New Point(60, 60),
.Location = New Point(720, 111),
.Image = Image.FromFile(".\txt.png"),
.SizeMode = PictureBoxSizeMode.StretchImage
}
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaPicBox)
Dim novaLbl As Label
novaLbl = New Label With {
.Text = nomeFichTexto,
.Location = New Point(720, 174)
}
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaLbl)
Me.Close()
End Sub
You keep calling
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm)
which keeps creating instances. You don't want to create an instance since there is surely already one. You need to find it, or, better yet, just set it when the message is shown.
Add a property to your class which can be set before it's shown, which is the actual previous Form.
Public Class MsgForm
Public Property PreviousForm As Form
Private Sub pasta_Click(sender As Object, e As EventArgs) Handles pasta.Click
Dim nomePasta = InputBox("Nome da Pasta: ", "Nova Pasta", "Nova pasta")
Dim novoForm As New Form With {
.Name = nomePasta,
.Text = nomePasta,
.Size = New Point(816, 489),
.BackColor = Color.FromName("ActiveCaption")
}
Dim novaPicBox = New PictureBox With {
.Size = New Point(60, 60),
.Location = New Point(720, 21)
}
PreviousForm.Controls.Add(novaPicBox)
Dim novaLbl = New Label With {
.Text = nomePasta,
.Location = New Point(720, 85)
}
PreviousForm.Controls.Add(novaLbl)
Me.Close()
End Sub
Private Sub fichtexto_Click(sender As Object, e As EventArgs) Handles fichtexto.Click
Dim nomeFichTexto = InputBox("Nome do Ficheiro de Texto: ", "Novo Ficheiro de Texto", "Novo Ficheiro de Texto")
Dim novaPicBox = New PictureBox With {
.Size = New Point(60, 60),
.Location = New Point(720, 111),
.Image = Image.FromFile(".\txt.png"),
.SizeMode = PictureBoxSizeMode.StretchImage
}
PreviousForm.Controls.Add(novaPicBox)
Dim novaLbl = New Label With {
.Text = nomeFichTexto,
.Location = New Point(720, 174)
}
PreviousForm.Controls.Add(novaLbl)
Me.Close()
End Sub
End Class
And when you call the message box form, set the property
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim m = New MsgForm With {.PreviousForm = Me}
m.Show()
End Sub
End Class

How to update a Chart

I've just finished developing a piece of code where I am able to create a Chart and query from my database. Here I insert values from the database and it will show to the user.
I will post here the code that I've done to create a chart:
Public Sub BuildChart()
Try
SQLCon = New SqlConnection
SQLCon.ConnectionString = "........."
Dim sqlStatis As String = "SELECT Top 5 Filename, Filesize FROM infofile"
Dim Chart1 As New Chart()
Dim da As New SqlDataAdapter(sqlStatis, SQLCon)
Dim ds As New DataSet()
da.Fill(ds, "infofile")
Dim ChartArea1 As ChartArea = New ChartArea()
Dim Legend1 As Legend = New Legend()
Dim Series1 As Series = New Series()
Me.Controls.Add(Chart1)
ChartArea1.Name = "ChartArea1"
Chart1.ChartAreas.Add(ChartArea1)
Legend1.Name = "Legend1"
Chart1.Legends.Add(Legend1)
Chart1.Location = New System.Drawing.Point(12, 12)
Chart1.Name = "Chart1"
Series1.ChartArea = "ChartArea1"
Series1.Legend = "Legend1"
Series1.Name = "Tamanho do ficheiro"
Chart1.Series.Add(Series1)
Chart1.Size = New System.Drawing.Size(600, 300)
Chart1.TabIndex = 0
Chart1.Text = "Chart1"
Chart1.Series("Tamanho do ficheiro").XValueMember = "Filename"
Chart1.Series("Tamanho do ficheiro").YValueMembers = "Filesize"
Chart1.DataSource = ds.Tables("infofile")
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
SQLCon.Dispose()
End Try
End Sub
As you can see I've created a method which will be called in the form and the info will be shown there. Outside of everything I declared a variable Dim Chart1 As New Chart(). Now I want to create a method which allows me with a timer to update automatically the chart. So I should create another method called UpdateChart where I could insert there this:
Timer1.Interval = 3000
Timer1.Start()
But now I have no idea what should I use to update it every 3 secs or 3000 milliseconds.
On Load you want to call the BuildChart method and start the timer:
Private Sub frmTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BuildChart()
With Timer1
.Enabled = True
.Interval = 3000
.Start()
End With
End Sub
For BuildChart you only need to create the chart itself and not bind the data.
Public Sub BuildChart()
Try
Dim Chart1 As New Chart()
Dim ChartArea1 As ChartArea = New ChartArea()
Dim Legend1 As Legend = New Legend()
Dim Series1 As Series = New Series()
Me.Controls.Add(Chart1)
ChartArea1.Name = "ChartArea1"
Chart1.ChartAreas.Add(ChartArea1)
Legend1.Name = "Legend1"
Chart1.Legends.Add(Legend1)
Chart1.Location = New System.Drawing.Point(12, 12)
Chart1.Name = "Chart1"
Series1.ChartArea = "ChartArea1"
Series1.Legend = "Legend1"
Series1.Name = "Tamanho do ficheiro"
Chart1.Series.Add(Series1)
Chart1.Size = New System.Drawing.Size(600, 300)
Chart1.TabIndex = 0
Chart1.Text = "Chart1"
Chart1.Series("Tamanho do ficheiro").XValueMember = "Filename"
Chart1.Series("Tamanho do ficheiro").YValueMembers = "Filesize"
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
SQLCon.Dispose()
End Try
End Sub
We then want UpdateChart to be called from the Timer.Tick event.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
UpdateChart()
End Sub
Private Sub UpdateChart()
Chart1.Series(0).Points.Clear()
Chart1.DataSource = ""
SQLCon = New SqlConnection
SQLCon.ConnectionString = "............."
Dim sqlStatis As String = "SELECT Top 5 Filename, Filesize FROM infofile"
Dim da As New SqlDataAdapter(sqlStatis, SQLCon)
Dim ds As New DataSet()
da.Fill(ds, "infofile")
Chart1.DataSource = ds.Tables("infofile")
End Sub
Be aware though that this will create a lot of hits to your database.

Design-time Coding

I'm a newbie in visual basic dot net. I tried to create a design in run-time. Here's my simple code.
Dim frmLogin As New Form
Dim lblUserName, lblPassword As New Label
Dim txtUserName, txtPassword As New TextBox
Dim btnContinue, btnCancel As New Button
'set frmLogin properties
frmLogin.StartPosition = FormStartPosition.CenterScreen
frmLogin.FormBorderStyle = Windows.Forms.FormBorderStyle.None
frmLogin.BackColor = Color.LightGray
frmLogin.Size = New Size(400, 200)
'set lblUserName properties
lblUserName.Text = "User Name: "
lblUserName.Font = New Font("Calibri", 12, FontStyle.Regular)
lblUserName.Location = New Point(20, 30)
'set lblPassword properties
lblPassword.Text = "Password: "
lblPassword.Font = New Font("Calibri", 12, FontStyle.Regular)
lblPassword.Location = New Point(20, 70)
'set txtUserName properties
txtUserName.Font = New Font("Calibri", 12, FontStyle.Regular)
txtUserName.Location = New Point(120, 20)
txtUserName.Size = New Size(250, 20)
txtPassword.Font = New Font("Calibri", 12, FontStyle.Regular)
txtPassword.Location = New Point(120, 60)
txtPassword.Size = New Size(250, 20)
txtPassword.PasswordChar = "*"
btnCancel.Text = "Cancel"
btnCancel.Location = New Point(270, 120)
btnCancel.Size = New Size(100, 28)
btnCancel.BackColor = Color.White
btnCancel.Font = New Font("Calibri", 12, FontStyle.Regular)
frmLogin.Controls.Add(lblPassword)
frmLogin.Controls.Add(lblUserName)
frmLogin.Controls.Add(txtUserName)
frmLogin.Controls.Add(txtPassword)
frmLogin.Controls.Add(btnCancel)
frmLogin.ShowDialog()
How could I start creating a code here? I'd like to start in btnCancel, how could I insert this code in btnCancel
dim myAns as string = msgbox("This will exit the program. Are you sure?")
if ans = vbyes then application.exit
you have to add an event handler to the controls:
'register the event handler after initializing of the control
AddHandler btnCancel.Click, AddressOf CancelClick
'create a method with same signature as the delegate of the event
Private Sub CancelClick(ByVal sender As Object, ByVal e As EventArgs)
'do your stuff here
End Sub