Visual Basic 2010 - Dynamic Tabs, Controls & Recording Data - database

I'm posting this after losing my time by searching & got no proper answer.
My requirement is to create a permit system.
There is a tabcontrol on the form & when I click on "New Permit" button (located top of the form above tabcontrol), a new tab should open (named "NEW Permit") with text boxes on it.
Each time "New Permit" button is clicked, same type of tabs to be opened parallely.
When we select a one tab & fill in the text fields & click on "Issue Permit" button (located top of the form above tabcontrol), tab should be closed while saving the data in text fields to a access database with a dedicated permit number.
I have created the form & the "New Permit","Issue Permit" buttons & tabcontrol are on a usercontrol "ucPermit".
I'm struck in creating those dynamic tabs & getting the data on those into a database.
'''''''''' Annex ''''''''''''''''''''''''''''''''''''''
#DonA
I have two panels on the form.
Panel 01 (on left) has buttons "Summary, Permit, LOTO, HotWork".
When each button is clicked respective usercontrol (loaded onto panel 02 on right at runtime) comes to front.
Public Class Home
Dim ucSummary As New ucSummary
Dim ucPermitMain As New ucPermitMain
Dim ucLotoMain As New ucLotoMain
Dim ucHotWorkMain As New ucHotWorkMain
Private Sub Home_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblDate.Text = Format(Now, "MMMM dd, yyyy")
lblTime.Text = Format(Now, "h:mm tt")
btnSummary.BackColor = Color.LightGreen
Panel2.Controls.Add(ucSummary)
ucSummary.Dock = DockStyle.Fill
Panel2.Controls.Add(ucPermitMain)
ucPermitMain.Dock = DockStyle.Fill
Panel2.Controls.Add(ucLotoMain)
ucLotoMain.Dock = DockStyle.Fill
Panel2.Controls.Add(ucHotWorkMain)
ucHotWorkMain.Dock = DockStyle.Fill
End Sub
Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click
btnSummary.BackColor = Color.LightGreen
btnPermit.BackColor = Color.LightBlue
btnLoto.BackColor = Color.LightBlue
btnHotWork.BackColor = Color.LightBlue
ucSummary.BringToFront()
End Sub
Private Sub btnPermit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPermit.Click
btnSummary.BackColor = Color.LightBlue
btnPermit.BackColor = Color.LightGreen
btnLoto.BackColor = Color.LightBlue
btnHotWork.BackColor = Color.LightBlue
ucPermitMain.BringToFront()
End Sub
Private Sub btnLoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoto.Click
btnSummary.BackColor = Color.LightBlue
btnPermit.BackColor = Color.LightBlue
btnLoto.BackColor = Color.LightGreen
btnHotWork.BackColor = Color.LightBlue
ucLotoMain.BringToFront()
End Sub
Private Sub btnHotWork_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHotWork.Click
btnSummary.BackColor = Color.LightBlue
btnPermit.BackColor = Color.LightBlue
btnLoto.BackColor = Color.LightBlue
btnHotWork.BackColor = Color.LightGreen
ucHotWorkMain.BringToFront()
End Sub
End Class
I have just started & currently working on Permit section which works with ucPermitMain usercontrol.
It has a toolstrip (tsPermit) on top & an empty tabcontrol (tcPermit) below.
Toolstrip has buttons "NewPermit, Issue, Close" (for now..more to come with design)
Each time NewPermit is clicked new tabs open parallely with text boxes on it.
When selected a particular tab & "Issue" is clicked, the data on the selected tab's text boxes to be put into database "PTW.accdb", & tab to be closed.
Public Class ucPermitMain
Dim PTW As New OleDb.OleDbConnection
Dim ucPermit As New ucPermit
Dim txtbox As New TextBox
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
PTW = New OleDb.OleDbConnection
PTW.ConnectionString = "Provider=Microsoft.ACE.Oledb.12.0; Data Source=" & Application.StartupPath & "\PTW.accdb"
End Sub
Public Sub tsbtnNewPermit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnNewPermit.Click
tsbtnClose.Visible = True
tsbtnClose.Enabled = True
Dim CTP As New CustomTabPage("New PTW", New Object)
Me.tcPermit.TabPages.Add(CTP)
tcPermit.SelectTab(tcPermit.TabPages.Count - 1)
End Sub
Public Sub tsbtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnClose.Click
tcPermit.TabPages.RemoveAt(tcPermit.SelectedIndex)
'This bottom code is telling it to remove the selected tab and countdown minus (-) 1 (one)
If tcPermit.TabPages.Count > 0 Then
tcPermit.SelectTab(tcPermit.SelectedIndex)
Else
tsbtnClose.Visible = False
tsbtnClose.Enabled = False
End If
End Sub
Public Sub tsbtnIssue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnIssue.Click
Dim cmd As New OleDb.OleDbCommand
Dim CTB As CustomTextBox
If Not PTW.State = ConnectionState.Open Then
'open connection if it is not yet open
PTW.Open()
End If
cmd.Connection = PTW
cmd.CommandText = "INSERT INTO tblPermit(ptwNo)" & _
"VALUES('" & ?????????????? & "')"
cmd.ExecuteNonQuery()
End Sub
End Class
CustomTextBox & CustomTabPage are two seperate classes to create these custom controls.
Imports System.Windows.Forms
Public Class CustomTabPage
Inherits TabPage
Public Sub New(ByVal Name As String, ByVal NewConstruct As Object)
InitTextBox(1)
Me.Text = Name
End Sub
Private Sub InitTextBox(ByVal Num As Integer)
Dim LocX As Integer = 10
Dim LocY As Integer = 10
Dim CTB As New CustomTextBox("")
CTB.Location = New System.Drawing.Point(LocX, LocY)
Me.Controls.Add(CTB)
End Sub
End Class
Imports System.Windows.Forms
Public Class CustomTextBox
Inherits TextBox
Public Sub New(ByVal Name As String)
Me.Text = Name
End Sub
End Class
This is only code I have done upto now & they may be not optimized.
Hope you will spend your time to go through.

The lesson here is to have a Data Layer, this layer has all the methods and logic for saving data to the database. When the new tab is built you have a property that allows it to access the Data Layer. It might even need some encapsulation(UserControl) with having a group of control that the same logic for saving/validating data.
You also should show some code - an effort on your part before asking for help.

Related

Script Errors in Visual Studio 2012

I'm currently operating Visual Studio 2012. I have a "URL Scanner" form where once a url is entered into a text box, four web browsers opens up and inputs the url into the site and displays.
My problem is this, I get multiple script error pop ups with a error box that states
"Object doesn't support property or method 'getElementsByClassName'".
I tried looking up related resolutions but can't get it to work. I'm quite new to VB and hope someone could correct my mistake.
Class MainWindow
Dim WithEvents ScriptWindow As WebBrowser
Private Sub Enter_Click(sender As Object, e As RoutedEventArgs) Handles Enter1.Click
If TextBox1.Text = "" Then
MessageBox.Show("Please type a target URL in the space provided!")
Else
'Dim file As System.IO.StreamWriter
Dim theDate As Date
theDate = Format(Now(), "short date")
Textboxdate.Text = theDate
' file = My.Computer.FileSystem.OpenTextFileWriter("URLlogs.txt", True)
'file.WriteLine(TextBox1.Text & " | Date of search: " & Textboxdate.Text)
'file.Close()
Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\URLlogs.txt"
Dim writer As New System.IO.StreamWriter(path, True)
writer.WriteLine(TextBox1.Text & " | Date of search: " & Textboxdate.Text)
writer.Close()
WebBrowser1.Navigate("https://www.talosintelligence.com/reputation_center/lookup?search=" & TextBox1.Text)
WebBrowser2.Navigate("https://www.ssllabs.com/ssltest/analyze.html?d=" & TextBox1.Text)
WebBrowser3.Navigate("https://sitecheck.sucuri.net/results/" & TextBox1.Text)
WebBrowser4.Navigate("https://quttera.com/detailed_report/" & TextBox1.Text)
End If
End Sub
Private Sub Reset_Click(sender As Object, e As RoutedEventArgs) Handles Reset.Click
TextBox1.Text = ""
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As TextChangedEventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub SuppressScriptErrors()
If (WebBrowser1.Document IsNot Nothing) Then
ScriptWindow = WebBrowser1.Document.Window
End If
End Sub
Private Sub SuppressScriptErrors2()
If (WebBrowser2.Document IsNot Nothing) Then
ScriptWindow = WebBrowser2.Document.Window
End If
End Sub
Private Sub SuppressScriptErrors3()
If (WebBrowser3.Document IsNot Nothing) Then
ScriptWindow = WebBrowser3.Document.Window
End If
End Sub
Private Sub SuppressScriptErrors4()
If (WebBrowser4.Document IsNot Nothing) Then
ScriptWindow = WebBrowser4.Document.Window
End If
End Sub
Private Sub Startpage1_Click(sender As Object, e As RoutedEventArgs) Handles Startpage1.Click
Me.Hide()
Dim startwindow As New StartWindow
startwindow.Show()
End Sub
Private Sub SearchHistory1_Click(sender As Object, e As RoutedEventArgs) Handles SearchHistory1.Click
Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\URLlogs.txt"
If System.IO.File.Exists(path) = True Then
Process.Start(path)
Else
MessageBox.Show("Please generate a search before proceeding")
End If
End Sub
End Class

How match system time with access database time?

I am creating an app in VB.NET that will play a song on user given time. User store song location and play time in ms-access database. when system time match with database time the song will play automatically. I've tried this with given below code but this is not working.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Call Play(Format(Now, "Long Time"))
End Sub
Sub Play(ByVal tm As String)
Dim ConnectionString As String
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=" & Application.StartupPath & "\VideoPlay.accdb;"
Dim AccessConnection As New
System.Data.OleDb.OleDbConnection(ConnectionString)
AccessConnection.Open()
Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT *
FROM VideoPlay", AccessConnection)
Dim ds As New DataSet
da.Fill(ds, "VideoPlay")
Dim dt As DataTable
dt = ds.Tables("VideoPlay")
Dim x As Integer
If dt.Rows.Count > 0 Then
For x = 0 To dt.Rows.Count - 1
If tm = dt.Rows(x).Item("TM") Then
MsgBox("yes")
End If
Next
End If
AccessConnection.Close()
da.Dispose()
ds.Dispose()
dt.Dispose()
AccessConnection.Close()
End Sub
End Class
Can any one tell me what is wrong in my code or any other way to compare system time with database time.
thanks in advance.

Using listbox to access 2D array VB

So I am trying to use a listbox to access variables in my 2D array. I am unsure what is the best way to do this. Right now I am using the selectedindex of the listbox to access it but I am only seeing the second dimension being show in my message box. Any help would be appreciated.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
lstInventory.Items.Add("Hand Grenade")
lstInventory.Items.Add("9mm Ammo Box")
lstInventory.Items.Add(".40 Ammo Box")
lstInventory.SelectedIndex = 0
End Sub
Dim dblInventoryItem(,) As Double = {{10.99, 5},
{5.99, 10},
{8.99, 8}}
Private Sub btnCheck_Click(sender As System.Object, e As System.EventArgs) Handles btnCheck.Click
Dim intRow As Integer = lstInventory.SelectedIndex
MessageBox.Show(dblInventoryItem(intRow, 1).ToString)
End Sub
End Class
You don't want to use a multi-dimensional array here.
The OOP way to do this would be to define inventory item as its own class or structure, and use instances to both populate your list box and store the inventory item price and quantity.
Something like:
Public Class Form1
Structure InventoryItem
Public Sub New(ByVal itmName As String, ByVal itmPrice As Double, ByVal itmQty As Integer)
Name = itmName : Price = itmName : Quantity = itmQty
End Sub
Dim Name As String
Dim Price As Double
Dim Quantity As Integer
End Structure
Dim invItems As New List(Of InventoryItem)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
invItems.Add(New InventoryItem("Hand Grenade", 10.99, 5))
'' ... Add your additional items here
For Each i As InventoryItem In invItems
lstInventory.Items.Add(i.Name)
Next
End Sub
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
Dim invItem As InventoryItem = invItems(lstInventoryItems.SelectedIndex)
MessageBox.Show(invItem.Name & "," & invItem.Price & "," & invItem.Quantity)
End Sub
End Class

change default value of textbox to new value and store new value after reopen

I have a textbox(textbox1) which when the WPF Window is loaded, it used the UserProfile variable to show the current user directory in textbox1.text
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim defaultpath As String = Environment.CurrentDirectory
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("UserProfile")
TextBox1.Text = defaultpath
End Sub
I also have a button that when clicked, uses FolderBrowserDialog to browse for folder then shows the new folder path in textbox1.text.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim fldDialog As New FolderBrowserDialog()
fldDialog.RootFolder = Environment.SpecialFolder.Desktop
fldDialog.ShowDialog()
Dim filepathstore As String = fldDialog.SelectedPath
TextBox1.Text = filepathstore
End Sub
The value now shows the path that was selected with FolderBrowserDialog.
How would I store this new value and when WPF Window is closed/reopen, displays this new value instead of the default value. (replace not delete default value)
This new value of the folderpath can change as many times as needed. However when a reset button is clicked, the WPF window goes back to default value.
It sounds like the easiest option is to save the value to a file that can be recalled at any time. The below code assume the value you want to save is in textbox1.text when the FormClosing event is called, and then loads it back into textbox1.text when the form is opened.
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim s As String
s = TextBox1.Text
Dim loc As String
loc = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "/testfile.txt"
My.Computer.FileSystem.WriteAllText(loc, TextBox1.Text, False)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim loc As String
loc = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "/testfile.txt"
Dim s As String
s = My.Computer.FileSystem.ReadAllText(loc)
TextBox1.Text = s
End Sub
Let me know if you have a problem with this :)

I want to change text font using font dialog in vb.net

Here is my code and I am able to add the text by defining some font properties but I want to add this using Font dialog.Can anyone help me regarding this issue.
Public Class Form1
Dim pic_font As New Font("Arial Black", 40, FontStyle.Regular, GraphicsUnit.Pixel)
Dim bm As Bitmap = New Bitmap(100, 100)
Dim strText As String = "Diver Dude"
Dim szText As New SizeF
Dim ptText As New Point(125, 125)
Dim ptsText() As PointF
Dim MovingOffset As PointF
Dim ptsTextPen As Pen = New Pen(Color.LightSteelBlue, 1)
Dim MouseMoving As Boolean
Dim MouseOver As Boolean
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PictureBox1.Hide()
bm = Image.FromFile(Application.StartupPath & "\DivePic.bmp")
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
ptsTextPen.DashStyle = DashStyle.Dot
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
'Check if the pointer is over the Text
If IsMouseOverText(e.X - 10, e.Y - 10) Then
MouseMoving = True
'Determine the upper left corner point from where the mouse was clicked
MovingOffset.X = e.X - ptText.X
MovingOffset.Y = e.Y - ptText.Y
Else
MouseMoving = False
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
'Check if the pointer is over the Text
If IsMouseOverText(e.X - 10, e.Y - 10) Then
If Not MouseOver Then
MouseOver = True
Me.Refresh()
End If
Else
If MouseOver Then
MouseOver = False
Me.Refresh()
End If
End If
If e.Button = Windows.Forms.MouseButtons.Left And MouseMoving Then
ptText.X = CInt(e.X - MovingOffset.X)
ptText.Y = CInt(e.Y - MovingOffset.Y)
Me.Refresh()
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
MouseMoving = False
Me.Refresh()
End Sub
Public Function IsMouseOverText(ByVal X As Integer, ByVal Y As Integer) As Boolean
'Make a Graphics Path from the rotated ptsText.
Using gp As New GraphicsPath()
gp.AddPolygon(ptsText)
'Convert to Region.
Using TextRegion As New Region(gp)
'Is the point inside the region.
Return TextRegion.IsVisible(X, Y)
End Using
End Using
End Function
Dim tbm As Bitmap
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
tbm = CType(bm.Clone, Bitmap)
Dim g As Graphics = Graphics.FromImage(tbm)
Dim mx As Matrix = New Matrix
Dim gpathText As New GraphicsPath
Dim br As SolidBrush = New SolidBrush(Color.FromArgb(tbarTrans.Value, _
KryptonColorButton1.SelectedColor))
SetptsText()
'Smooth the Text
g.SmoothingMode = SmoothingMode.AntiAlias
'Make the GraphicsPath for the Text
Dim emsize As Single = Me.CreateGraphics.DpiY * pic_font.SizeInPoints / 72
gpathText.AddString(strText, pic_font.FontFamily, CInt(pic_font.Style), _
emsize, New RectangleF(ptText.X, ptText.Y, szText.Width, szText.Height), _
StringFormat.GenericDefault)
'Draw a copy of the image to the Graphics Object canvas
g.DrawImage(CType(bm.Clone, Bitmap), 0, 0)
'Rotate the Matrix at the center point
mx.RotateAt(tbarRotate.Value, _
New Point(ptText.X + (szText.Width / 2), ptText.Y + (szText.Height / 2)))
'Get the points for the rotated text bounds
mx.TransformPoints(ptsText)
'Transform the Graphics Object with the Matrix
g.Transform = mx
'Draw the Rotated Text
If chkAddOutline.Checked Then
Using pn As Pen = New Pen(Color.FromArgb(tbarTrans.Value, KryptonColorButton2.SelectedColor), 1)
g.DrawPath(pn, gpathText)
End Using
Else
g.FillPath(br, gpathText)
End If
If CheckBox2.Checked = True Then
Dim p As New Pen(Color.FromArgb(tbarTrans.Value, KryptonColorButton2.SelectedColor), 1)
'draw te hollow outlined text
g.DrawPath(p, gpathText)
'clear the path
gpathText.Reset()
Else
g.FillPath(br, gpathText)
End If
'Draw the box if the mouse is over the Text
If MouseOver Then
g.ResetTransform()
g.DrawPolygon(ptsTextPen, ptsText)
End If
'Draw the whole thing to the form
e.Graphics.DrawImage(tbm, 10, 10)
'tbm.Dispose()
g.Dispose()
mx.Dispose()
br.Dispose()
gpathText.Dispose()
End Sub
Private Sub TrackBar_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tbarRotate.Scroll, tbarTrans.Scroll
lblRotate.Text = tbarRotate.Value
lblOpacity.Text = tbarTrans.Value
Me.Refresh()
End Sub
Sub SetptsText()
'Create a point array of the Text Rectangle
ptsText = New PointF() { _
ptText, _
New Point(CInt(ptText.X + szText.Width), ptText.Y), _
New Point(CInt(ptText.X + szText.Width), CInt(ptText.Y + szText.Height)), _
New Point(ptText.X, CInt(ptText.Y + szText.Height)) _
}
End Sub
Private Sub chkAddOutline_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAddOutline.CheckedChanged
Me.Refresh()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
bm = Image.FromFile(OpenFileDialog1.FileName)
szText = Me.CreateGraphics.MeasureString(strText, pic_font)
SetptsText()
ptsTextPen.DashStyle = DashStyle.Dot
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
tbm.Save(SaveFileDialog1.FileName)
End If
End Sub
End Class
What do you mean.If you mean open a font dialog and select a font from it,here is the code.
' You need Import System.Drawing before your class
' In your class vars section
Dim fd As New FontDialog
'later in your code
' This should be in the code where you call the font dialog
If(fd.ShowDialog() == DialogResults.Ok)
pic_font = fd.Font
End If

Resources