vb. net input border in print preview - vb.net
I would like to ask on how to put a border in print preview and it will also put the label value inside the border please see attached print preview screenshot thank you. Private Sub PrintDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
print preview screenshot
here is my code:
Private Sub PrintDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString("CONOLOSCOPY", New Font("Arial", 20, FontStyle.Bold), Brushes.Red, New Point(300, 20))
e.Graphics.DrawString("ENDOSCOPY NO.: " + txt_ID.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 60))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(180, 60))
e.Graphics.DrawString("REG NO.: " + txt_REGNO.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(300, 60))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(390, 60))
e.Graphics.DrawString("HEIGHT: " + txt_HEIGHT.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(500, 60))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(580, 60))
e.Graphics.DrawString("DATE: " + txt_DATE.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 80))
e.Graphics.DrawString("______________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(70, 80))
e.Graphics.DrawString("ROOM NO.: " + txt_ROOMNO.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(280, 80))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(380, 80))
e.Graphics.DrawString("WEIGHT NO.: " + txt_WEIGHT.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(500, 80))
e.Graphics.DrawString("_________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(620, 80))
e.Graphics.DrawString("LAST NAME: " + txt_LNAME.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 130))
e.Graphics.DrawString("__________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(120, 130))
e.Graphics.DrawString("FIRST NAME: " + txt_FNAME.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(350, 130))
e.Graphics.DrawString("__________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(470, 130))
e.Graphics.DrawString("AGE: " + txt_AGE.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 160))
e.Graphics.DrawString("_____", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(55, 160))
e.Graphics.DrawString("SEX: " + txt_SEX.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(120, 160))
e.Graphics.DrawString("_____", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(170, 160))
e.Graphics.DrawString("CELL NO.: " + txt_CELLNO.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(250, 160))
e.Graphics.DrawString("_________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(340, 160))
e.Graphics.DrawString("ADDRESS: " + txt_ADDRESS.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 190))
e.Graphics.DrawString("__________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(100, 190))
e.Graphics.DrawString("EMAIL: " + txt_EMAIL.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 240))
e.Graphics.DrawString("_______________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(60, 240))
e.Graphics.DrawString("REFERRING MD: " + txt_REFERMD.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 270))
e.Graphics.DrawString("________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(160, 270))
e.Graphics.DrawString("ATTENDING MD: " + txt_MD.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(350, 270))
e.Graphics.DrawString("___________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(495, 270))
e.Graphics.DrawString("ANESTHESIOLOGIST: " + txt_ANES.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 300))
e.Graphics.DrawString("_______________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(210, 300))
e.Graphics.DrawString("PROCEDURE: " + txt_PROC.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 330))
e.Graphics.DrawString("______________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(135, 330))
e.Graphics.DrawString("INDICATION: " + txt_INDICATION.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(350, 330))
e.Graphics.DrawString("_________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(460, 330))
e.Graphics.DrawString("PREMEDICATION: " + txt_PREMED.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 360))
e.Graphics.DrawString("_______________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(170, 360))
e.Graphics.DrawString("SCOPE USED: " + txt_SCOPE.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 390))
e.Graphics.DrawString("___________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(140, 390))
e.Graphics.DrawString("PROCTIME: " + txt_PROCTIME.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(400, 390))
e.Graphics.DrawString("___________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(510, 390))
e.Graphics.DrawString("BIOPSY: " + txt_BIOPSY.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(370, 420))
e.Graphics.DrawString("___________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(450, 420))
e.Graphics.DrawString("ESOPHAGUS: " + txt_ESOPHAGUS.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 450))
e.Graphics.DrawString("CARDIA: " + txt_CARDIA.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 480))
e.Graphics.DrawString("BODY: " + txt_BODY.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 510))
e.Graphics.DrawString("ANTRUM: " + txt_ANTRUM.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 540))
e.Graphics.DrawString("DUODENAL: " + txt_DUODENAL.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 580))
e.Graphics.DrawString("ENDOSCOPIC DESCRIPTION: " + txt_ENDODESC.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 610))
e.Graphics.DrawString("_________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(270, 610))
e.Graphics.DrawString("THERAPEUTIC INTERVENTION " + txt_THERAPEUTIC.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 660))
e.Graphics.DrawString("______________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(290, 660))
e.Graphics.DrawString("DIAGNOSIS: " + txt_DIAGNOSIS.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 810))
e.Graphics.DrawString("___________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(120, 810))
e.Graphics.DrawString("HISTOPATH: " + txt_HISTOPATH.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 960))
e.Graphics.DrawString("_______________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(120, 960))
e.Graphics.DrawString("ENDOSCOPIST: " + txt_ENDOSPCOPIST.Text, New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(10, 1010))
e.Graphics.DrawString("_______________________________________________________", New Font("Arial", 13, FontStyle.Regular), Brushes.Black, New Point(150, 1010))
End Sub
Related
vb.net print multiple pages
my problem here is how to print it on multiple pages, page counts are depends on the user input on textbox9. please help me. thanks Private Sub PrintDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage PrintDocument1.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("Paper Size Name", 300, 300) Dim CT As Date = Now ' Use of Now. TextBox17.Text = (CT.ToShortDateString) Dim font1 As New Font("arial", 6, FontStyle.Bold) Dim font2 As New Font("arial", 10, FontStyle.Bold) Dim myBitmap As New System.Drawing.Bitmap(filename:="C:\Users\jtapellido\Desktop\sticker pictures\slogo1.png") Dim index As Integer = 1 Do e.Graphics.DrawImage(image:=myBitmap, point:=New Point(70, 20)) e.Graphics.DrawString("QUARANTINE", font2, Brushes.Black, 70, 40) e.Graphics.DrawString("MATERIAL NAME:_________________________________", font1, Brushes.Black, 70, 60) e.Graphics.DrawString("QC No:_____________", font1, Brushes.Black, 70, 80) e.Graphics.DrawString("LOT No:____________", font1, Brushes.Black, 70, 100) e.Graphics.DrawString("DATE RECEIVED:______________", font1, Brushes.Black, 170, 80) e.Graphics.DrawString("EXPIRATION DATE:____________", font1, Brushes.Black, 170, 100) e.Graphics.DrawString("QUANTITY:____________", font1, Brushes.Black, 70, 120) e.Graphics.DrawString("VENDOR/MANUFATURER:", font1, Brushes.Black, 70, 140) e.Graphics.DrawString("________________________________________________", font1, Brushes.Black, 70, 160) e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 70, 180) e.Graphics.DrawString("STORAGE:_______________________________________", font1, Brushes.Black, 70, 200) e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 70, 220) e.Graphics.DrawString("PREPARED BY/DATE:", font1, Brushes.Black, 70, 240) e.Graphics.DrawString("CHECKED BY/DATE:", font1, Brushes.Black, 190, 240) e.Graphics.DrawString("____________________", font1, Brushes.Black, 70, 255) e.Graphics.DrawString("____________________", font1, Brushes.Black, 190, 255) e.Graphics.DrawString(TextBox1.Text, font1, Brushes.Black, 145, 58) e.Graphics.DrawString(TextBox2.Text, font1, Brushes.Black, 110, 78) e.Graphics.DrawString(TextBox3.Text, font1, Brushes.Black, 110, 98) e.Graphics.DrawString(TextBox4.Text, font1, Brushes.Black, 255, 78) e.Graphics.DrawString(TextBox5.Text, font1, Brushes.Black, 255, 98) e.Graphics.DrawString(TextBox6.Text, font1, Brushes.Black, 120, 118) 'e.Graphics.DrawString("VENDOR/MANUFATURER:", font1, Brushes.Black, 10, 120) e.Graphics.DrawString(TextBox7.Text, font1, Brushes.Black, 70, 158) e.Graphics.DrawString(index, font1, Brushes.Black, 170, 178) e.Graphics.DrawString(TextBox9.Text, font1, Brushes.Black, 255, 178) e.Graphics.DrawString(TextBox11.Text, font1, Brushes.Black, 120, 198) e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 120, 218) 'e.Graphics.DrawString("PREPARED BY/DATE:", font1, Brushes.Black, 10, 220) 'e.Graphics.DrawString("CHECKED BY/DATE:", font1, Brushes.Black, 120, 220) e.Graphics.DrawString(TextBox15.Text + " " + TextBox17.Text, font1, Brushes.Black, 70, 253) e.Graphics.DrawString(ComboBox2.Text + " " + TextBox17.Text, font1, Brushes.Black, 190, 253) 'e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds) index += 1 Loop Until index > TextBox9.text End Sub
The logic for printing multiple pages is pretty simple. The PrintPage event handler does just what the name suggests, i.e. it prints a page. At the end of the method, you set e.HasMorePages to True if there are more pages to print and that's it. The event will be raised again and another page printed. It's up to you to provide logic at the start of the method to determine what page you're printing and at the end to determine whether there are more pages to print. Because you need to remember a state between calls to that method, you'll likely store the relevant data in one or more fields. Here is an example of printing record from a list, ten to a page: Private allData As New List(Of String) Private dataToPrint As Queue(Of String) Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Start printing. PrintDocument1.Print() End Sub Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint 'Create a new queue containing all the current data. dataToPrint = New Queue(Of String)(allData) End Sub Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 'Print a maximum of 10 records. For i = 1 To Math.Min(dataToPrint.Count, 10) e.Graphics.DrawString(dataToPrint.Dequeue(), Font, Brushes.Black, 25, i * 25) Next 'Keep printing if and only if there are more records to print. e.HasMorePages = (dataToPrint.Count > 0) End Sub Private Sub PrintDocument1_EndPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.EndPrint dataToPrint = Nothing End Sub If you wanted to print a specific number of pages based on the contents of a TextBox then you might convert that contents to an Integer and assign that to a field at the start. You could then decrement that variable at the end of the PrintPage event handler and print another page if and only if that counter is greater than zero.
How to print DataGridView data?
Here is my code to Print: Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click PrintInvoiceDoc.DefaultPageSettings.Landscape = True PrintPreviewDialog.ShowDialog() End Sub Private Sub PrintInvoiceDoc_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintInvoiceDoc.PrintPage Dim CenterAlign As New StringFormat CenterAlign.Alignment = StringAlignment.Center Dim RightAlign As New StringFormat CenterAlign.Alignment = StringAlignment.Far e.Graphics.DrawString("School Name", New Font("Arial", 16, FontStyle.Bold), Brushes.Black, New Point(25, 25)) e.Graphics.DrawString("Report", New Font("Arial", 16, FontStyle.Bold), Brushes.Black, New Point(1000, 25), RightAlign) Dim mRow As Integer = 0 Dim newpage As Boolean = True With DataGridView1 Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit) fmt.LineAlignment = StringAlignment.Center fmt.Trimming = StringTrimming.EllipsisCharacter Dim y As Single = e.MarginBounds.Top Do While mRow < .RowCount Dim row As DataGridViewRow = .Rows(mRow) Dim x As Single = e.MarginBounds.Left Dim h As Single = 0 For Each cell As DataGridViewCell In row.Cells Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height) e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) If (newpage) Then e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt) Else e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt) End If x += rc.Width h = Math.Max(h, rc.Height) Next newpage = False y += h mRow += 1 If y + h > e.MarginBounds.Bottom Then e.HasMorePages = True mRow -= 1 newpage = True Exit Sub End If Loop mRow = 0 End With End Sub So, when hit 'Print' I get the result shown in this image: Problem #1: I am missing the first row [As you can see] Problem #2: When the rows are enough that one page is not enough to store all of them, new page is created in infinite loop and same page is repeated so not all data is printed. Note: I almost don't have any idea about using a DataGridView in PrintDocument, but any advice/tips are welcome!
ur first row behind header row... make new rectanglef for ur header row like this.... For Each cell As DataGridViewCell In row.Cells Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height) Dim rc2 As RectangleF = New RectangleF(x, y - 22, cell.Size.Width, cell.Size.Height) If (newpage) Then e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New RectangleF(x, y - 22, cell.Size.Width, cell.Size.Height)) e.Graphics.DrawRectangle(Pens.Black, rc2.Left, rc2.Top, rc2.Width, rc2.Height) e.Graphics.DrawString(dgpembelian.Columns(cell.ColumnIndex).HeaderText, cellheaderFont, Brushes.Black, rc2, drawFormat) End If e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) e.Graphics.DrawString(dgpembelian.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString, .Font, Brushes.Black, rc, drawFormat) x += rc.Width h = Math.Max(h, rc.Height) Next
Print multiple pages in vb.net
How can i print multiple pages? In my form i have textboxes with their corresponding labels eg. (id, name, course, etc.) but the problem is 1 page is not enough to display all textboxes. i have to add another page to display the remaining textboxes with their labels. I tried to set e.hasmorepages to true but the textboxes appears in the second page are just the same with the first page it does not continue. Here is my code : Private Sub printSisDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printSisDoc.PrintPage Dim labelFont As New Font("Arial", 11, FontStyle.Bold) Dim textFont As New Font("Arial", 11, FontStyle.Regular) Dim headerFont As New Font("Arial", 12, FontStyle.Bold) e.Graphics.DrawString(lblGrade.Text, headerFont, Brushes.Black, 650, 660) e.Graphics.DrawString(grade11.Text, textFont, Brushes.Black, 660, 690) e.Graphics.DrawString(underline.Text, labelFont, Brushes.Black, 643, 692) e.Graphics.DrawString(grade12.Text, textFont, Brushes.Black, 660, 715) e.Graphics.DrawString(grade13.Text, textFont, Brushes.Black, 660, 740) e.Graphics.DrawString(grade14.Text, textFont, Brushes.Black, 660, 765) e.Graphics.DrawString(grade15.Text, textFont, Brushes.Black, 660, 790) e.Graphics.DrawString(grade16.Text, textFont, Brushes.Black, 660, 815) e.Graphics.DrawString(grade17.Text, textFont, Brushes.Black, 660, 840) e.Graphics.DrawString(grade18.Text, textFont, Brushes.Black, 660, 865) e.Graphics.DrawString(grade19.Text, textFont, Brushes.Black, 660, 890) e.Graphics.DrawString(grade20.Text, textFont, Brushes.Black, 0, 1500) mPageNumber += 1 e.HasMorePages = (mPageNumber <= 2) End Sub
When you have more than one page, you need to ensure the single PrintPage() method is called once for every page you need to print. Each time the method is called, it needs to know which page is current and what should be written to that page. The e.HasMorePages variable is how you make the PrintDocument object call the method again. Also remember the printSisDoc_PrintPage() method is part of a class. You can set data in the class instance the method can use to know what page is current and what to print. Private Sub printSisDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printSisDoc.PrintPage Dim labelFont As New Font("Arial", 11, FontStyle.Bold) Dim textFont As New Font("Arial", 11, FontStyle.Regular) Dim headerFont As New Font("Arial", 12, FontStyle.Bold) Select mPageNumber Case 1 e.Graphics.DrawString(lblGrade.Text, headerFont, Brushes.Black, 650, 660) e.Graphics.DrawString(grade11.Text, textFont, Brushes.Black, 660, 690) e.Graphics.DrawString(underline.Text, labelFont, Brushes.Black, 643, 692) e.Graphics.DrawString(grade12.Text, textFont, Brushes.Black, 660, 715) e.Graphics.DrawString(grade13.Text, textFont, Brushes.Black, 660, 740) e.Graphics.DrawString(grade14.Text, textFont, Brushes.Black, 660, 765) e.Graphics.DrawString(grade15.Text, textFont, Brushes.Black, 660, 790) e.Graphics.DrawString(grade16.Text, textFont, Brushes.Black, 660, 815) e.Graphics.DrawString(grade17.Text, textFont, Brushes.Black, 660, 840) e.Graphics.DrawString(grade18.Text, textFont, Brushes.Black, 660, 865) e.Graphics.DrawString(grade19.Text, textFont, Brushes.Black, 660, 890) e.HasMorePages = True Case 2 e.Graphics.DrawString(grade20.Text, textFont, Brushes.Black, 0, 400) e.HasMorePages = False End Select mPageNumber += 1 End Sub
Referencing a barcode font in graphics.drawstring VB
How can I go about referencing a barcode font in graphics.drawstring? This is my code so far. Its printing the text not the barcode: Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintDocument1.PrintPage e.Graphics.DrawString(Drugname1, New Font("Arial", 8), Brushes.Black, 10, 10) e.Graphics.DrawString("Exp: " & Expirydate_new, New Font("Arial", 8), Brushes.Black, 10, 20) e.Graphics.DrawString("Ds Remaining: " & DsRemaining1, New Font("Arial", 8), Brushes.Black, 10, 40) e.Graphics.DrawString(barCode, New Font("PrecisionID C128 04 Regular", 10), Brushes.Black, 10, 60) Dim g As Graphics 'Dim Picturebox1 As Image = Image.FromFile("barcode.bmp") 'g = e.Graphics 'g.DrawImage(Picturebox1, 0, 0) 'g.Dispose() e.HasMorePages = False End Sub Any ideas? Thanks
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