Run-time Design - vb.net

I'm newbie in vb.net and I just have something to ask you.
I want to create a simple program and I'm trying to do this with run-time design.
with this form, when you click on the button1 with the caption(Text) "Show Another Form", another form will be created with this code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim NewForm As New Form
Dim btnCancel As New Button
NewForm.StartPosition = FormStartPosition.CenterScreen
NewForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
NewForm.BackColor = Color.WhiteSmoke
NewForm.Size = New Size(400, 200)
NewForm.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
btnCancel.Text = "Cancel"
btnCancel.Size = New Size(150, 50)
btnCancel.Location = New Point(50, 50)
NewForm.Controls.Add(btnCancel)
NewForm.ShowDialog()
AddHandler btnCancel.Click, AddressOf CancelClick
End Sub
Public Sub cancelclick(ByVal sender As Object, ByVal e As EventArgs)
Dim x As String = MessageBox.Show("Exit", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If x = vbYes Then End
End Sub
End Class
How can I exit the form I've created when you click on the Cancel Button. The code I've provided doesn't work. Pls help me out. Thanks

Try attach handler code before show dialog
NewForm.Controls.Add(btnCancel)
AddHandler btnCancel.Click, AddressOf CancelClick
NewForm.ShowDialog()

In your code change the AddressOf CancelClick to AddressOf cancelclick
EDIT:
Change the NewForm.ShowDialog() to NewForm.Show() and also change the code as below code then definitely it will work.
Public NewForm As Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
NewForm = New Form
Dim btnCancel As New Button
NewForm.StartPosition = FormStartPosition.CenterScreen
NewForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
NewForm.BackColor = Color.WhiteSmoke
NewForm.Size = New Size(400, 200)
NewForm.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
btnCancel.Text = "Cancel"
btnCancel.Size = New Size(150, 50)
btnCancel.Location = New Point(50, 50)
NewForm.Controls.Add(btnCancel)
AddHandler btnCancel.Click, AddressOf cancelclick
NewForm.Show()
End Sub
Public Sub cancelclick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim x As String = MessageBox.Show("Exit", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If x = vbYes Then End
End Sub

Related

Parameter Field current value exception was Unhandled

I'm having a problem in my program that when i click the print button it always show up this error Sample error and i don't know what to do now.
this is the code, this is my parameters sample parameters
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ShowPrintButton = False
For Each ctrl As Control In CrystalReportViewer1.Controls
If TypeOf ctrl Is Windows.Forms.ToolStrip Then
Dim btnNew As New ToolStripButton
btnNew.Text = "PRINT"
btnNew.ToolTipText = "Print Reciept"
CType(ctrl, ToolStrip).Items.Insert(0, btnNew)
AddHandler btnNew.Click, AddressOf printClick
End If
Next
End Sub
Private Sub printClick(sender As Object, e As EventArgs)
Dim PrintDialog As New PrintDialog
If PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
CrystalReport11.PrintOptions.PrinterName = PrintDialog.PrinterSettings.PrinterName
CrystalReport11.PrintToPrinter(1, False, 0, 0) 'this is where the error coming up'
Transact()
End If
End Sub
Private Sub btnSlip_Click(sender As Object, e As EventArgs) Handles btnSlip.Click
Dim reportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument
reportDocument = New CrystalReport1
reportDocument.Refresh()
reportDocument.SetDataSource(dt)
reportDocument.SetParameterValue(0, ComboBox2.Text)
reportDocument.SetParameterValue(1, TextBox1.Text)
Form2.CrystalReportViewer1.ReportSource = reportDocument
Form2.ShowDialog()
Form2.Dispose()
Form2.WindowState = FormWindowState.Maximized
End Sub

How to handle a variable amount of buttons clicked?

I have a pretty basic problem with button clicks in VB.Net I cannot seem to figure out.
First, I am creating a variable amount of buttons and adding them to the parent form.
Private Sub CreateUIObjects()
For i As Integer = 1 To NumberOfButtons
Dim button As Button = New Button()
Me.Controls.Add(button)
Next
End Sub
I know it is possible to handle a fixed amount of buttons clicked with the following code
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click, Button2.Click, Button3.Click '... And so on
Dim b As Button = CType(sender, Button)
End Sub
But what do I do with not 3, but a variable amount of buttons?
Some code to experiment with
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CreateUIObjects(3)
End Sub
Dim myButtonNames As String = "foobar"
Private Sub myButtonsClick(sender As Object, e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
Debug.WriteLine(b.Name)
End Sub
Private Sub CreateUIObjects(NumberOfButtons As Integer)
Static ct As Integer = 0
For i As Integer = 1 To NumberOfButtons
ct += 1
Dim btn As Button = New Button()
btn.Name = myButtonNames & ct.ToString
btn.Text = btn.Name
btn.Location = New Point(ct * 20, ct * 20)
AddHandler btn.Click, AddressOf myButtonsClick
Me.Controls.Add(btn)
Next
End Sub
You can use something like this
AddHandler Button1.Click, AddressOf Button_Click
take a look http://msdn.microsoft.com/en-us/library/ms172877.aspx

vb.net passing variables between controls

here is my code
Public Sub Personalize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each fi As FileInfo In New DirectoryInfo(Application.StartupPath + "\web").GetFiles
Dim pictooltip As New ToolTip
Dim pbx As New Button
AddHandler pbx.Click, AddressOf pbx_click
pbx.Width = 150
pbx.Height = 150
pbx.BackgroundImage = Image.FromFile(fi.FullName)
wallpapers.Controls.Add(pbx)
pbx.Cursor = Cursors.Hand
pictooltip.SetToolTip(pbx, fi.Name)
pbx.BackgroundImageLayout = ImageLayout.Stretch
Next
End Sub
Private Sub pbx_click()
main.BackgroundImage = Image.FromFile(fi.FullName)
End Sub
i can't figure how to use "fi" in pbx_click()
any hint for that ??
Just put the FullName() into the Tag() property of the Button and pull it back out when it gets clicked:
Dim pbx As New Button
pbx.Tag = fi.FullName
Pulling it back out:
Private Sub pbx_click()
main.BackgroundImage = Image.FromFile(DirectCast(sender, control).Tag.ToString())
End Sub
Try this,
Public Class Form1
Dim fi as FileInfo
Public Sub Personalize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each Me.fi In New DirectoryInfo(Application.StartupPath + "\web").GetFiles
Dim pictooltip As New ToolTip
Dim pbx As New Button
AddHandler pbx.Click, AddressOf pbx_click
pbx.Width = 150
pbx.Height = 150
pbx.BackgroundImage = Image.FromFile(fi.FullName)
wallpapers.Controls.Add(pbx)
pbx.Cursor = Cursors.Hand
pictooltip.SetToolTip(pbx, fi.Name)
pbx.BackgroundImageLayout = ImageLayout.Stretch
Next
End Sub
Private Sub pbx_click()
main.BackgroundImage = Image.FromFile(fi.FullName)
End Sub
End Class
Your Fi is declared locally in the first subroutine (or sub).
Declare Fi outside of the Personalize_Load sub, then pass data to it.
Dim fi as String = ""
Public Sub Personalize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each fii As FileInfo In New DirectoryInfo(Application.StartupPath + "\web").GetFiles
Dim pictooltip As New ToolTip
Dim pbx As New Button
AddHandler pbx.Click, AddressOf pbx_click
pbx.Width = 150
pbx.Height = 150
pbx.BackgroundImage = Image.FromFile(fii.FullName)
fi = fii.FullName
wallpapers.Controls.Add(pbx)
pbx.Cursor = Cursors.Hand
pictooltip.SetToolTip(pbx, fi.Name)
pbx.BackgroundImageLayout = ImageLayout.Stretch
Next
End Sub
Private Sub pbx_click()
main.BackgroundImage = Image.FromFile(fi)
End Sub
You can then use pbx_click() by simply calling it on any sub, or trying it to a handler.
Change your event to this:
Private Sub pbx_click(sender As Object, e As System.EventArgs)
main.BackgroundImage = Image.FromFile(DirectCast(sender, Button).BackgroundImage)
End Sub
Or, you can use the Tag property and store some serialized data. See here: Add parameter to Button click event

Is it possible to print or save the picturebox with other picture box within it

I am a vb.net beginner. I got a project that I need to do function that enabled user to add new picture(which is a new picturebox) and move it in a picture box. I have made these two happened but I don`t know how to make the picturebox(that allow new picturebox move inside) save as bitmap/jpg into database. Is that possible to do that.If yes, how?
Public Class Form1
Private btn As Button ' this is a reference object
Private pic As PictureBox
Private ptX, ptY As Integer
Private drag As Boolean
Private Sub nodepic_MouseDown(ByVal senderPic As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then
drag = True
pic = CType(senderPic, PictureBox)
ptX = e.X : ptY = e.Y
End If
If pic.Focused Then
clearButton.Enabled = True
End If
End Sub
Private Sub nodepic_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
If pic.Location.X >= 1 AndAlso pic.Location.Y >= 1 AndAlso
(pic.Location.X + pic.Width) <= panelPictureBox.Width - 5 AndAlso
(pic.Location.Y + pic.Height) <= panelPictureBox.Height - 5 Then
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
Me.Refresh()
Else
drag = False
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
End If
End If
End Sub
Private Sub node_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = False
End Sub
Private Sub deleteButton(senderPic As Object, e As EventArgs)
Dim delete As DialogResult
delete = MessageBox.Show("Are you sure to delete this icon?", "Delete Icon", MessageBoxButtons.YesNo)
If delete = Windows.Forms.DialogResult.Yes Then
senderPic.Dispose()
locationLabel.Text = String.Empty
End If
End Sub
Private Sub locationButton(senderPic As Object, e As System.Windows.Forms.MouseEventArgs)
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
locationLabel.Text = pic.Location.ToString()
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim picBox As New PictureBox
If e.Node.Name.Equals("red") Then
picBox.Image = ImageList1.Images(0)
End If
If e.Node.Name.Equals("orange") Then
picBox.Image = ImageList1.Images(1)
End If
picBox.Location = New Point(10, 10)
panelPictureBox.Controls.Add(picBox)
action(picBox)
End Sub
Private Sub action(sender As PictureBox)
AddHandler sender.MouseDown, AddressOf nodepic_MouseDown
AddHandler sender.MouseMove, AddressOf nodepic_MouseMove
AddHandler sender.MouseUp, AddressOf node_MouseUp
AddHandler sender.MouseDoubleClick, AddressOf deleteButton
AddHandler sender.MouseClick, AddressOf locationButton
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
panelPictureBox.Enabled = True
panelPictureBox.BackColor = Color.White
End Sub
Private Sub clearButton_Click(senderPic As Object, e As EventArgs) Handles clearButton.Click
pic.Dispose()
End Sub**strong text**
End Class
You can save the PictureBox, along with its "child" PictureBoxes, to a Bitmap using code like this:
Dim bmp As New Bitmap(panelPictureBox.Width, panelPictureBox.Height)
panelPictureBox.DrawToBitmap(bmp, panelPictureBox.ClientRectangle)
Next you save it to memory by writing it out to a MemoryStream. Note that you can specify the format in the second parameter:
Dim ms As New System.IO.MemoryStream()
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Finally, you can obtain a byte array from the resulting MemoryStream:
Dim bytes() As Byte = ms.ToArray
' ... save "bytes" to your database ...
if you need to print the image inside the picturebox then you should insert printdialog ,printdocument in side the design of the form then copy the code below
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
PrintDialog1 = New PrintDialog
PrintDialog1.Document = PrintDocument1 'pbxLogo.Image
Dim r As DialogResult = PrintDialog1.ShowDialog
If r = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawImage(PictureBox1.Image, 0, 0)
End Sub

vb2010 getting name values from multiple buttons

At the moment, I have a button that sends a value to another form and displays result in a label. The problem is, I have 20 buttons that are labeled a to w that need to be coded and I am stumped as to how I can pass values from multiple buttons. Would it be a case statement in the form being passed to? I am a new user to VB.Net and still finding my way so any help would be gratefully received. I have included code sample for the first button 'A'. Thanks
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = MouseButtons.Right) Then
'Dim curButton As Button = DirectCast(sender, Button)
'frmRacks.buttonName = curButton.Name 'Dynamic alternative to frmRacks.buttonName = "A"
frmRacks.buttonName = "A"
frmRacks.Show()
ElseIf (e.Button = MouseButtons.Left) Then
MessageBox.Show("To be coded")
End If
End Sub
frmRacks
Public Class frmRacks
Public buttonName As String
Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
EDIT: New project
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim button1 As Button = New Button
Dim button2 As Button = New Button
Dim button3 As Button = New Button
With button1
.Name = "button1"
.Left = 0
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button1
End With
With button2
.Name = "button2"
.Left = 100
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button2
End With
With button3
.Name = "button3"
.Left = 200
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button3
End With
Me.Controls.Add(button1)
Me.Controls.Add(button2)
Me.Controls.Add(button3)
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim curButton As Button = DirectCast(sender, Button)
Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
Form2.buttonName = curButtonName
Form2.Show()
'MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub
End Class
Public Class Form2
Public buttonName As String
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
End Class
Here you have a sample code which hopefully will help you to get clearer ideas:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim button1 As Button = New Button
Dim button2 As Button = New Button
Dim button3 As Button = New Button
With button1
.Name = "button1"
.Left = 0
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button1
End With
With button2
.Name = "button2"
.Left = 100
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button2
End With
With button3
.Name = "button3"
.Left = 200
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button3
End With
Me.Controls.Add(button1)
Me.Controls.Add(button2)
Me.Controls.Add(button3)
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim curButton As Button = DirectCast(sender, Button)
Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub