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
Related
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]
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
hey everyone can someone help me with some codes in vb.net
i created a loop that can generate labels with my OleDbDataReader
now i was wondering if i can make those labels open new form
and can i use the data in this label in my new form like the label text created !
here is my code :
Dim cnn As New OleDbCommand(query, con)
Dim cmd As New OleDbDataAdapter(cnn)
Dim dt As New DataTable()
cmd.Fill(dt)
Dim reader As OleDbDataReader
reader = cnn.ExecuteReader()
Dim number As Integer = 0
Dim location As Integer = 0
While reader.Read()
Dim sensibleFont As New Font("Segoe UI", 15)
Dim lb As New Label()
lb.Name = "labb" + number.ToString
lb.Size = New System.Drawing.Size(350, 40)
lb.Location = New System.Drawing.Point(50, 15 + location)
lb.Text = dt.Rows(number)(0).ToString()
lb.ForeColor = Color.Black
lb.Font = sensibleFont
GroupBox1.Controls.Add(lb)
Dim lb2 As New Label()
lb2.Name = "labs" + number.ToString
lb2.Size = New System.Drawing.Size(280, 40)
lb2.Location = New System.Drawing.Point(10, 5 + location)
lb2.Text = dt.Rows(number)(2).ToString()
lb2.ForeColor = Color.Black
lb2.Font = sensibleFont
GroupBox2.Controls.Add(lb2)
location += 40
number += 1
End While
con.Close()
Create a sub that does what you want.
Private Sub OpenForm()
Dim myForm As New MyForm()
myForm.Show()
End Sub
During label creation, add a click event that uses this sub as it's handler
AddHandler lb.Click, AddressOf OpenForm
As shown in the answer by A Friend, you can add a handler to a label's click event. Extending that to use the correct signature for the handler, you can get the object which raised the event and so use its properties, such as the .Text property:
Sub Label_Click(sender As Object, e As EventArgs)
Dim lbl = DirectCast(sender, Label)
Dim frm2 = New OtherForm(lbl.Text)
frm2.Show()
End Sub
To go with it, you will need a constructor (the Sub New) in the other form (which I named "OtherForm") like this:
Public Class OtherForm
Sub New()
InitializeComponent()
End Sub
Sub New(info As String)
InitializeComponent()
Label1.Text = info
End Sub
End Class
Where Label1 is just a label I put on OtherForm for testing purposes.
You might find the .Tag property of a control better for passing data as it can be any Object, e.g. an instance of a class.
I notice that you are reading the database twice: once to fill the datatable and then again as a count of the rows in the datatable, which is a bit wasteful. Also, you should dispose of a font when you have finished with it: the Using construct will take care of that for you, similarly for a connection to a database. Like this:
Sub PopulateGroupBox()
Dim connStr = "CONNECTION STRING HERE"
Dim query = "SQL QUERY HERE"
Dim dt As New DataTable()
Using con As New OleDbConnection(connStr)
Dim cnn As New OleDbCommand(query, con)
Dim cmd As New OleDbDataAdapter(cnn)
cmd.Fill(dt)
End Using
Dim location As Integer = 0
Using sensibleFont As New Font("Segoe UI", 15)
For i = 0 To dt.Rows.Count - 1
Dim lb1 As New Label()
lb1.Name = "labb" & i.ToString()
lb1.Size = New System.Drawing.Size(350, 40)
lb1.Location = New System.Drawing.Point(50, 15 + location)
lb1.Text = dt.Rows(i)(0).ToString()
lb1.ForeColor = Color.Black
lb1.Font = sensibleFont
AddHandler lb1.Click, AddressOf Label_Click
GroupBox1.Controls.Add(lb1)
Dim lb2 As New Label()
lb2.Name = "labs" & i.ToString()
lb2.Size = New System.Drawing.Size(280, 40)
lb2.Location = New System.Drawing.Point(10, 5 + location)
lb2.Text = dt.Rows(i)(2).ToString()
lb2.ForeColor = Color.Black
lb2.Font = sensibleFont
AddHandler lb2.Click, AddressOf Label_Click
GroupBox2.Controls.Add(lb2)
location += 40
Next
End Using
End Sub
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.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Panel1.Controls.Clear()
Dim patiekalai = New Button()
Dim patiekalai1 = New Button()
Dim patiekalai2 = New Button()
Dim patiekalai3 = New Button()
Dim patiekalai4 = New Button()
Panel1.Controls.Add(patiekalai)
patiekalai.Location = New Point(0, 0)
patiekalai.Size = New Size(80, 50)
Panel1.Controls.Add(patiekalai1)
patiekalai1.Location = New Point(0, 50)
patiekalai1.Size = New Size(80, 50)
Panel1.Controls.Add(patiekalai2)
patiekalai2.Location = New Point(0, 100)
patiekalai2.Size = New Size(80, 50)
Panel1.Controls.Add(patiekalai3)
patiekalai3.Location = New Point(0, 150)
patiekalai3.Size = New Size(80, 50)
Panel1.Controls.Add(patiekalai4)
patiekalai4.Location = New Point(0, 200)
patiekalai4.Size = New Size(80, 50)
patiekalai.Image = Image.FromFile("../M/Karštieji patiekalai.jpg")
AddHandler patiekalai.Click, AddressOf Patiekalai_Click
patiekalai1.Image = Image.FromFile("../M/Salotos.jpg")
patiekalai2.Image = Image.FromFile("../M/Desertai.jpg")
patiekalai3.Image = Image.FromFile("../M/Užkandžiai.jpg")
patiekalai4.Image = Image.FromFile("../M/Sriubos.jpg")
End Sub
Private Sub Patiekalai(ByVal sender As System.Object, ByVal e As System.EventArgs)
Panel1.Controls.Clear()
Dim patiekalai5 = New Button()
Dim patiekalai6 = New Button()
Dim patiekalai7 = New Button()
Panel1.Controls.Add(patiekalai5)
patiekalai5.Location = New Point(0, 0)
patiekalai5.Size = New Size(80, 50)
Panel1.Controls.Add(patiekalai6)
patiekalai6.Location = New Point(0, 50)
patiekalai6.Size = New Size(80, 50)
Panel1.Controls.Add(patiekalai7)
patiekalai7.Location = New Point(0, 100)
patiekalai7.Size = New Size(80, 50)
End Sub
Private Sub Patiekalai_Click(ByVal sender As Object, ByVal e As EventArgs)
Throw New NotImplementedException
End Sub
End Class
I get
Private Sub Patiekalai_Click(ByVal sender As Object, ByVal e As EventArgs)
Throw New NotImplementedException
End Sub
after program fails and stuck.
I am trying to add new buttons after patiekalai button is clicked.
You will have to add a handler in code manually by using AddHandler to attach the delegate.
AddHandler patiekalai6.click, addressof Patiekalai_Click
for example. you will need to add a handler like this for each of the buttons.
Panel1.Controls.Add(patiekalai7)
patiekalai7.Location = New Point(0, 100)
patiekalai7.Size = New Size(80, 50)
AddHandler patiekalai7.click, addressof Patiekalai_Click