Visual Basic Advanced Browser - vb.net
I am creating a browser in Visual Basic, but am having a problem saving my settings.
In advanced options, under the privacy tab i would like to have my browser retain the settings after closing the window. If i click "Never Remember password", and close the window, the settings are not stored.
Also, when I type a URL in, after searching the URL is not updated correctly.
Does anyone have any ideas as to how i can make these parts of my project work?
I am somewhat new to coding, so any tips or extra ideas to add to the Browser would be welcomed and much appreciated as i would like to see this fully functional.
Thank you! :D
Form 1
Imports System.Net
Imports System.IO
Public Class Magycka
Private Declare Sub keybd_event Lib "user32" (ByVal volumeUpOrDown As Byte, ByVal v1 As Byte, ByVal v2 As Integer, ByVal v3 As Integer)
Private Sub Magycka_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
File.Delete("C:\Magycka\history.txt")
For Each link As String In History.lstHistory.Items
File.AppendAllText("C:\Magycka\History.txt", link & vbNewLine)
Next
For Each favoriteName As String In lstName.Items
File.AppendAllText("C:\Magycka\favoriteName.txt", favoriteName & vbNewLine)
Next
For Each favoriteURL As String In lstURL.Items
File.AppendAllText("C:\Magycka\favoriteURL.txt", favoriteURL & vbNewLine)
Next
End Sub
Private Sub tmrDate_Tick(sender As Object, e As EventArgs) Handles tmrDate.Tick
lblTime.Text = DateTime.Now.ToString
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbSearchEngine.Items.Add("Google")
cmbSearchEngine.Items.Add("Google Images")
cmbSearchEngine.Items.Add("YouTube")
cmbSearchEngine.SelectedIndex = 0
addTab(TabControl1)
Try
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
txtURL.Text = browser.Url.ToString
Catch ex As Exception
End Try
If My.Settings.homePageorBlankPage = 0 Then
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(My.Settings.homePage)
Else
End If
Try
History.Visible = False
For Each link As String In File.ReadAllLines("C:\Magycka\history.txt")
History.lstHistory.Items.Add(link.ToString)
Next
Catch ex As Exception
End Try
Try
For Each url As String In File.ReadAllLines("C:\Magycka\favoriteURL.txt")
ListBox3.Items.Add(url)
Next
Catch ex As Exception
End Try
Try
ListBox3.SelectedIndex = 0
Catch ex As Exception
End Try
Try
For Each Name As String In File.ReadAllLines("C:\Magycka\favoriteName.txt")
Dim newBookmark As New ToolStripButton
newBookmark.Text = Name
newBookmark.Tag = ListBox3.SelectedItem.ToString
tsBookmarks.Items.Add(newBookmark)
ListBox3.SelectedIndex = ListBox3.SelectedIndex + 1
Next
Catch ex As Exception
End Try
createdBookClick()
End Sub
Private Sub txtURLsearchEngines_KeyUp(sender As Object, e As KeyEventArgs) Handles txtURLsearchEngines.KeyUp
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
Select Case (cmbSearchEngine.SelectedIndex)
Case 0
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.google.com/#q=" + txtURLsearchEngines.Text)
End If
Case 1
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1366&bih=661&q=" + txtURL.Text)
End If
Case 2
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.youtube.com/results?search_query=" + txtURLsearchEngines.Text)
End If
End Select
End Sub
Private Sub txtURL_KeyUp(sender As Object, e As KeyEventArgs) Handles txtURL.KeyUp
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
If e.KeyCode = Keys.Enter Then
browser.Navigate(txtURL.Text)
End If
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.GoBack()
End Sub
Private Sub btnForward_Click(sender As Object, e As EventArgs) Handles btnForward.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.GoForward()
End Sub
Private Sub brnRefresh_Click(sender As Object, e As EventArgs) Handles brnRefresh.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Refresh()
End Sub
Private Sub btnURL_Click(sender As Object, e As EventArgs) Handles btnURL.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(txtURL.Text)
End Sub
Private Sub btnVolumeUp_Click(sender As Object, e As EventArgs) Handles btnVolumeUp.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeUp, 0, 0, 0)
End Sub
Private Sub btnVolumeDown_Click(sender As Object, e As EventArgs) Handles btnVolumeDown.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeDown, 0, 0, 0)
End Sub
Private Sub btnMute_Click(sender As Object, e As EventArgs) Handles btnMute.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeMute, 0, 0, 0)
End Sub
Private Sub OptionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OptionsToolStripMenuItem.Click
AdvancedOptions.Show()
End Sub
Private Sub ViewHistoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewHistoryToolStripMenuItem.Click
History.Show()
End Sub
Private Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnHome.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(My.Settings.homePage)
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
Select Case (cmbSearchEngine.SelectedIndex)
Case 0
browser.Navigate("https://www.google.com/#q=" + txtURLsearchEngines.Text)
Case 1
browser.Navigate("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1366&bih=661&q=" = txtURLsearchEngines.Text)
Case 2
browser.Navigate("https://www.youtube.com/results?search_query=" + txtURLsearchEngines.Text)
End Select
End Sub
Private Sub NewTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewTabToolStripMenuItem.Click
addTab(TabControl1)
End Sub
Private Sub CloseTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseTabToolStripMenuItem.Click
removeTab()
End Sub
Public Sub addTab(ByRef TabControl As TabControl)
Dim browser As New webBrowserfunction
Dim tab As New TabPage
browser.Tag = tab
tab.Tag = browser
TabControl1.TabPages.Add(tab)
tab.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(My.Settings.homePage)
TabControl1.SelectedTab = tab
End Sub
Public Sub addTabfromHistory(ByRef TabControl As TabControl, ByRef link As String)
Dim browser As New webBrowserfunction
Dim tab As New TabPage
browser.Tag = tab
tab.Tag = browser
TabControl1.TabPages.Add(tab)
tab.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(link)
TabControl1.SelectedTab = tab
End Sub
Public Sub removeTab()
If TabControl1.TabPages.Count <> 0 Then
TabControl1.TabPages.Remove(TabControl1.SelectedTab)
End If
End Sub
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Try
txtURL.Text = browser.Url.ToString
Catch ex As Exception
End Try
End Sub
Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
Dim newWindow As New Magycka
newWindow.Show()
End Sub
Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click
txtURL.Undo()
End Sub
Private Sub RedoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RedoToolStripMenuItem.Click
txtURL.ClearUndo()
End Sub
Private Sub CutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CutToolStripMenuItem.Click
txtURL.Cut()
End Sub
Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click
txtURL.Copy()
End Sub
Private Sub PasteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PasteToolStripMenuItem.Click
txtURL.Paste()
End Sub
Private Sub SelectAllToolStripMenuItem_Click(sender As Object, e As EventArgs)
txtURL.SelectAll()
End Sub
Private Sub SaveSourceCodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveSourceCodeToolStripMenuItem.Click
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Dim fileName As String = InputBox("Enter FileName:", "Save HTML", ".txt")
Dim path As String = "C:\Magycka\" & fileName
Try
If File.Exists(path) Then
Dim allText As String
allText = browser.DocumentText
File.WriteAllText(path, allText)
Else
File.Create(path).Dispose()
Dim allText As String
allText = browser.DocumentText
File.WriteAllText(path, allText)
End If
Catch ex As Exception
End Try
End Sub
Private Sub addBookmarks()
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Dim newBookmark As New ToolStripButton
newBookmark.Text = TabControl1.SelectedTab.Text
newBookmark.Tag = browser.Url
tsBookmarks.Items.Add(newBookmark)
lstName.Items.Add(TabControl1.SelectedTab.Text)
lstURL.Items.Add(browser.Url.ToString)
AddHandler newBookmark.Click, AddressOf newBookmarkClick
End Sub
Private Sub newBookmarkClick(sender As Object, e As EventArgs)
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
If TypeOf sender Is ToolStripButton Then
browser.Navigate(CType(sender, ToolStripButton).Tag)
End If
End Sub
Private Sub createdBookClick()
For Each item As ToolStripButton In tsBookmarks.Items
AddHandler item.Click, AddressOf bookClick
Next
End Sub
Private Sub bookClick(sender As Object, e As EventArgs)
txtURL.Text = CType(sender, ToolStripButton).Tag
bookNav()
End Sub
Private Sub bookNav()
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
browser.Navigate(txtURL.Text)
End Sub
Private Sub BookmarkPageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BookmarkPageToolStripMenuItem.Click
addBookmarks()
End Sub
End Class
Advanced Options
Imports System.Net
Imports System.IO
Public Class Magycka
Private Declare Sub keybd_event Lib "user32" (ByVal volumeUpOrDown As Byte, ByVal v1 As Byte, ByVal v2 As Integer, ByVal v3 As Integer)
Private Sub Magycka_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
File.Delete("C:\Magycka\history.txt")
For Each link As String In History.lstHistory.Items
File.AppendAllText("C:\Magycka\History.txt", link & vbNewLine)
Next
For Each favoriteName As String In lstName.Items
File.AppendAllText("C:\Magycka\favoriteName.txt", favoriteName & vbNewLine)
Next
For Each favoriteURL As String In lstURL.Items
File.AppendAllText("C:\Magycka\favoriteURL.txt", favoriteURL & vbNewLine)
Next
End Sub
Private Sub tmrDate_Tick(sender As Object, e As EventArgs) Handles tmrDate.Tick
lblTime.Text = DateTime.Now.ToString
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbSearchEngine.Items.Add("Google")
cmbSearchEngine.Items.Add("Google Images")
cmbSearchEngine.Items.Add("YouTube")
cmbSearchEngine.SelectedIndex = 0
addTab(TabControl1)
Try
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
txtURL.Text = browser.Url.ToString
Catch ex As Exception
End Try
If My.Settings.homePageorBlankPage = 0 Then
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(My.Settings.homePage)
Else
End If
Try
History.Visible = False
For Each link As String In File.ReadAllLines("C:\Magycka\history.txt")
History.lstHistory.Items.Add(link.ToString)
Next
Catch ex As Exception
End Try
Try
For Each url As String In File.ReadAllLines("C:\Magycka\favoriteURL.txt")
ListBox3.Items.Add(url)
Next
Catch ex As Exception
End Try
Try
ListBox3.SelectedIndex = 0
Catch ex As Exception
End Try
Try
For Each Name As String In File.ReadAllLines("C:\Magycka\favoriteName.txt")
Dim newBookmark As New ToolStripButton
newBookmark.Text = Name
newBookmark.Tag = ListBox3.SelectedItem.ToString
tsBookmarks.Items.Add(newBookmark)
ListBox3.SelectedIndex = ListBox3.SelectedIndex + 1
Next
Catch ex As Exception
End Try
createdBookClick()
End Sub
Private Sub txtURLsearchEngines_KeyUp(sender As Object, e As KeyEventArgs) Handles txtURLsearchEngines.KeyUp
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
Select Case (cmbSearchEngine.SelectedIndex)
Case 0
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.google.com/#q=" + txtURLsearchEngines.Text)
End If
Case 1
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1366&bih=661&q=" + txtURL.Text)
End If
Case 2
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.youtube.com/results?search_query=" + txtURLsearchEngines.Text)
End If
End Select
End Sub
Private Sub txtURL_KeyUp(sender As Object, e As KeyEventArgs) Handles txtURL.KeyUp
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
If e.KeyCode = Keys.Enter Then
browser.Navigate(txtURL.Text)
End If
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.GoBack()
End Sub
Private Sub btnForward_Click(sender As Object, e As EventArgs) Handles btnForward.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.GoForward()
End Sub
Private Sub brnRefresh_Click(sender As Object, e As EventArgs) Handles brnRefresh.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Refresh()
End Sub
Private Sub btnURL_Click(sender As Object, e As EventArgs) Handles btnURL.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(txtURL.Text)
End Sub
Private Sub btnVolumeUp_Click(sender As Object, e As EventArgs) Handles btnVolumeUp.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeUp, 0, 0, 0)
End Sub
Private Sub btnVolumeDown_Click(sender As Object, e As EventArgs) Handles btnVolumeDown.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeDown, 0, 0, 0)
End Sub
Private Sub btnMute_Click(sender As Object, e As EventArgs) Handles btnMute.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeMute, 0, 0, 0)
End Sub
Private Sub OptionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OptionsToolStripMenuItem.Click
AdvancedOptions.Show()
End Sub
Private Sub ViewHistoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewHistoryToolStripMenuItem.Click
History.Show()
End Sub
Private Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnHome.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(My.Settings.homePage)
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
Select Case (cmbSearchEngine.SelectedIndex)
Case 0
browser.Navigate("https://www.google.com/#q=" + txtURLsearchEngines.Text)
Case 1
browser.Navigate("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1366&bih=661&q=" = txtURLsearchEngines.Text)
Case 2
browser.Navigate("https://www.youtube.com/results?search_query=" + txtURLsearchEngines.Text)
End Select
End Sub
Private Sub NewTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewTabToolStripMenuItem.Click
addTab(TabControl1)
End Sub
Private Sub CloseTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseTabToolStripMenuItem.Click
removeTab()
End Sub
Public Sub addTab(ByRef TabControl As TabControl)
Dim browser As New webBrowserfunction
Dim tab As New TabPage
browser.Tag = tab
tab.Tag = browser
TabControl1.TabPages.Add(tab)
tab.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(My.Settings.homePage)
TabControl1.SelectedTab = tab
End Sub
Public Sub addTabfromHistory(ByRef TabControl As TabControl, ByRef link As String)
Dim browser As New webBrowserfunction
Dim tab As New TabPage
browser.Tag = tab
tab.Tag = browser
TabControl1.TabPages.Add(tab)
tab.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(link)
TabControl1.SelectedTab = tab
End Sub
Public Sub removeTab()
If TabControl1.TabPages.Count <> 0 Then
TabControl1.TabPages.Remove(TabControl1.SelectedTab)
End If
End Sub
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Try
txtURL.Text = browser.Url.ToString
Catch ex As Exception
End Try
End Sub
Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
Dim newWindow As New Magycka
newWindow.Show()
End Sub
Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click
txtURL.Undo()
End Sub
Private Sub RedoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RedoToolStripMenuItem.Click
txtURL.ClearUndo()
End Sub
Private Sub CutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CutToolStripMenuItem.Click
txtURL.Cut()
End Sub
Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click
txtURL.Copy()
End Sub
Private Sub PasteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PasteToolStripMenuItem.Click
txtURL.Paste()
End Sub
Private Sub SelectAllToolStripMenuItem_Click(sender As Object, e As EventArgs)
txtURL.SelectAll()
End Sub
Private Sub SaveSourceCodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveSourceCodeToolStripMenuItem.Click
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Dim fileName As String = InputBox("Enter FileName:", "Save HTML", ".txt")
Dim path As String = "C:\Magycka\" & fileName
Try
If File.Exists(path) Then
Dim allText As String
allText = browser.DocumentText
File.WriteAllText(path, allText)
Else
File.Create(path).Dispose()
Dim allText As String
allText = browser.DocumentText
File.WriteAllText(path, allText)
End If
Catch ex As Exception
End Try
End Sub
Private Sub addBookmarks()
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Dim newBookmark As New ToolStripButton
newBookmark.Text = TabControl1.SelectedTab.Text
newBookmark.Tag = browser.Url
tsBookmarks.Items.Add(newBookmark)
lstName.Items.Add(TabControl1.SelectedTab.Text)
lstURL.Items.Add(browser.Url.ToString)
AddHandler newBookmark.Click, AddressOf newBookmarkClick
End Sub
Private Sub newBookmarkClick(sender As Object, e As EventArgs)
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
If TypeOf sender Is ToolStripButton Then
browser.Navigate(CType(sender, ToolStripButton).Tag)
End If
End Sub
Private Sub createdBookClick()
For Each item As ToolStripButton In tsBookmarks.Items
AddHandler item.Click, AddressOf bookClick
Next
End Sub
Private Sub bookClick(sender As Object, e As EventArgs)
txtURL.Text = CType(sender, ToolStripButton).Tag
bookNav()
End Sub
Private Sub bookNav()
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
browser.Navigate(txtURL.Text)
End Sub
Private Sub BookmarkPageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BookmarkPageToolStripMenuItem.Click
addBookmarks()
End Sub
End Class
History
Imports System.IO
Public Class History
Private Sub History_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Me.Visible = False
For Each link As String In lstHistory.Items
File.AppendAllText("C:\Magycka\History.txt", link & vbNewLine)
Next
End Sub
Private Sub btnClearHis_Click(sender As Object, e As EventArgs) Handles btnClearHis.Click
lstHistory.Items.Clear()
System.IO.File.Delete("C:\Magycka\History.txt")
End Sub
Private Sub btnCloseHis_Click(sender As Object, e As EventArgs) Handles btnCloseHis.Click
For Each link As String In lstHistory.Items
File.AppendAllText("C:\Magycka\History.txt", link & vbNewLine)
Next
Close()
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
lstHistory.Items.Remove(lstHistory.SelectedItem)
End Sub
Private Sub lstHistory_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstHistory.MouseDoubleClick
Magycka.addTabfromHistory(Magycka.TabControl1, lstHistory.SelectedItem.ToString)
End Sub
End Class
Related
How to use numpad as input in vb calculator
Basically I've been searching for weeks now but I cannot find a good example of using the numpad as an input for calculator. Private Sub Form1_KeyPress(ByVal KeyAscii As _MSForms.ReturnInteger) lblOutput.Text = lblOutput.Text & Chr(KeyAscii) If KeyAscii >= vbKeyNumpad0() And KeyAscii <= vbKeyNumpad9() Then lblOutput.Text = lblOutput.Text & Chr(KeyAscii) End If End Sub Option Explicit On Public Class Form1 Dim chrOperator As Char Dim varFirstVal As Integer Private Sub Form1_KeyPress(ByVal KeyAscii As _MSForms.ReturnInteger) lblOutput.Text = lblOutput.Text & Chr(KeyAscii) If KeyAscii >= vbKeyNumpad0() And KeyAscii <= vbKeyNumpad9() Then lblOutput.Text = lblOutput.Text & Chr(KeyAscii) End If End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load KeyPreview = True End Sub Private Sub ButtonAppend(number As Integer) If CInt(lblOutput.Text) = 0 Then lblOutput.Text = CType(number, String) Else lblOutput.Text = lblOutput.Text & number End If End Sub Private Sub operation(operand As String) chrOperator = CChar(operand) varFirstVal = CInt(lblOutput.Text) lblOutput.Text = CType(0, String) End Sub Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click ButtonAppend(1) End Sub Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click ButtonAppend(2) End Sub Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click ButtonAppend(3) End Sub Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click ButtonAppend(4) End Sub Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click ButtonAppend(5) End Sub Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click ButtonAppend(6) End Sub Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click ButtonAppend(7) End Sub Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click ButtonAppend(8) End Sub Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click ButtonAppend(9) End Sub Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click ButtonAppend(0) End Sub Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click lblOutput.Text = "0" varFirstVal = 0 End Sub Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click Select Case chrOperator Case CChar("A") lblOutput.Text = CType(varFirstVal + CInt(lblOutput.Text), String) Case CChar("S") lblOutput.Text = CType(varFirstVal - CInt(lblOutput.Text), String) Case CChar("M") lblOutput.Text = CType(varFirstVal * CInt(lblOutput.Text), String) Case CChar("D") lblOutput.Text = CType(varFirstVal / CInt(lblOutput.Text), String) End Select End Sub Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click operation("A") End Sub Private Sub btnSub_Click(sender As Object, e As EventArgs) Handles btnSub.Click operation("S") End Sub Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click operation("M") End Sub Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click operation("S") End Sub End Class What I want to get out of it is being able to use the numpad and never have to click anything on screen for the calculator to work.
OleDb error: "ExecuteNonQuery() requires an open and available connection" [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 7 years ago. Improve this question I've been trying to connect Access and VB 2013, but it keeps giving me this error ExecuteNonQuery() requires an open and Avaliable connection Here is my code: Imports System.Data.OleDb Imports System.IO Public Class Form3 Public sEditType As String = String.Empty Dim cn As New OleDbConnection Dim constring As String = "Provider=Microsoft.Jet.Oledb.12.0; Data Source=C:\Users\MarkTT\Documents\major.mdb; persist security info = false" Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click End Sub Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.KeyPress End Sub Private Sub Label4_Click(sender As Object, e As EventArgs) End Sub Private Sub TextBox2_TextChanged_1(sender As Object, e As EventArgs) End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) End Sub Private Sub Label5_Click(sender As Object, e As EventArgs) End Sub Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click End Sub Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click Me.WindowState = FormWindowState.Minimized End Sub Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click Me.Close() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim cm As New OleDbCommand Try Dim sqlquery As String = "INSERT INTO tablea (Name,Cost,Supplier,Quantity) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')" With cm .CommandText = sqlquery .Connection = cn .ExecuteNonQuery() End With MsgBox("Added") Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) End Sub Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click TextBox1.Enabled = True TextBox2.Enabled = True TextBox3.Enabled = True TextBox4.Enabled = True End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click TextBox1.Enabled = False TextBox2.Enabled = False TextBox3.Enabled = False TextBox4.Enabled = False End Sub Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged End Sub Private Sub TextBox2_TextChanged_2(sender As Object, e As EventArgs) Handles TextBox2.TextChanged End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click End Sub Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load cn.ConnectionString = constring cn.Open() End Sub End Class
Provider=Microsoft.Jet.Oledb.12.0 is not a valid OLEDB provider name. You either need to use Provider=Microsoft.Jet.OLEDB.4.0 or Provider=Microsoft.ACE.OLEDB.12.0
Vb.net Error then use form4.show
I have a little problem with my program. I just maked a little program. I have a problem with code "Form4.Show()". I put this code on button and then press button I receive error: An error occurred creating the form. See Exception.IneerException for details. The error is: Object reference not set to an instance of an object. I don't know what code I put wrong ... this button have only form4.show. PS: this error is from try catch. This is button code. Try Form4.Show Catch ex as Exception MessageBox.Show(ex.Message) End Try edit: all form4 code Public Class Form4 Dim txt As String = Textbox1.Text Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click txt = "450IP-Gift" Form5.Show() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click txt = "450IP" Form5.Show() End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click txt = "1150IP-Gift" Form5.Show() End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click txt = "1150IP" Form5.Show() End Sub Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click txt = "3150IP-Gift" Form5.Show() End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click txt = "3150IP" Form5.Show() End Sub Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click txt = "4800IP-Gift" Form5.Show() End Sub Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click txt = "4800IP" Form5.Show() End Sub Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click txt = "6300IP-Gift" Form5.Show() End Sub Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click txt = "6300IP" Form5.Show() End Sub Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click txt = "9600IP-Gift" Form5.Show() End Sub Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click txt = "9600IP" Form5.Show() End Sub Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click Me.Close() End Sub End Class
You need to initialize form4 Try Dim form4 = New Form4() form4.Show() Catch ex as Exception MessageBox.Show(ex.Message) End Try
open selected row in datagridview to edit in Another form
I am using vb.net designer to connect to access database . On my Form1 I have a DataGridView And Two Button For Add And Edit I Make Form2 To Add Data Into Database And Worked OK .. Imake Form3 Wiht Same form2 Content Now I need When i selcet row in DataGridView And Clic Edit Button The data of selected row show on form3 for Edit it Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.SalesTableAdapter.Fill(Me.OrdersDataSet.sales) Me.DateTimePicker1.Value = Date.Today End Sub Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged SalesBindingSource.Filter = String.Format("date = '{0}'", DateTimePicker1.Value.ToShortDateString()) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Form2.Show() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Form3.Show() End Sub Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click End Sub Private Sub SalesDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles SalesDataGridView.CellContentClick End Sub End Class
You need to approach this in a modal/dialog way. You only need one form for both add and edit. Add/Edit form Add a parameterized constructor to the form. Public Sub New(row As DataRowView) Me.InitializeComponent() 'Me.ctlAge: NumericUpDown control. 'Me.ctlBirthday: DateTimePicker control. 'Me.ctlName: TextBox control. If (row Is Nothing) Then 'Add mode, set default values: Me.ctlAge.Value = 0 Me.ctlBirthday.Value = Date.Now Me.ctlName.Text = String.Empty Else 'Edit mode, set current values: Me.ctlAge.Value = CDec(row.Item("AGE")) Me.ctlBirthday.Value = CDate(row.Item("BIRTHDAY")) Me.ctlName.Text = CStr(row.Item("NAME")) End If End Sub You also need an accept button and a cancel button. Friend Sub btnAcceptClicked(sender As Object, e As EventArgs) Handles btnAccept.Click Me.DialogResult = Windows.Forms.DialogResult.OK Me.Close() End Sub Friend Sub btnCancelClicked(sender As Object, e As EventArgs) Handles btnCancel.Click Me.DialogResult = Windows.Forms.DialogResult.Cancel Me.Close() End Sub Main form Add method: Private Sub btnAddClicked(sender As Object, e As EventArgs) Handles btnAdd.Click Try Using f As New AddOrEditForm(CType(Nothing, DataRowView)) If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then Dim view As DataView = TryCast(Me.SalesDataGridView.DataSource, DataView) If (view Is Nothing) Then Throw New InvalidCastException() End If Dim viewRow As DataRowView = view.AddNew() viewRow.EndEdit() viewRow.Item("AGE") = f.ctlAge.Value viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value viewRow.Item("NAME") = f.ctlName.Text viewRow.EndEdit() End If End Using Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Edit method: Private Sub btnEditClicked(sender As Object, e As EventArgs) Handles btnEdit.Click Try Me.SalesDataGridView.EndEdit() If (Me.SalesDataGridView.SelectedRows.Count > 0) Then Dim gridRow As DataGridViewRow = Me.SalesDataGridView.SelectedRows(0) Dim viewRow As DataRowView = TryCast(gridRow.DataBoundItem, DataRowView) If (viewRow Is Nothing) Then Throw New InvalidCastException() End If Using f As New AddOrEditForm(viewRow) If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then viewRow.BeginEdit() Try viewRow.Item("AGE") = f.ctlAge.Value viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value viewRow.Item("NAME") = f.ctlName.Text viewRow.EndEdit() Catch ex As Exception viewRow.CancelEdit() Throw ex End Try End If End Using End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
Multi Threading For getting links from Webbrowser1.Document
The problem is that multi threading is not working properly for getting links from webbrowser1.document.links. How can I solve this problem? Public Class Form1 Dim thread1 As System.Threading.Thread Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click thread1 = New System.Threading.Thread(AddressOf GetLinks) thread1.Start() End Sub Private Sub GetLinks() For i As Integer = 0 To WebBrowser1.Document.Links.Count - 1 If TextBox1.Text.Length > 0 Then TextBox1.Text += Environment.NewLine & WebBrowser1.Document.Links(i).ToString Else TextBox1.Text = WebBrowser1.Document.Links(i).ToString End If Next End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.Navigate("www.google.com") Me.CheckForIllegalCrossThreadCalls = False End Sub End Class
You can't make a call to one of the controls on the form (in this case, TextBox1 and WebBrowser1) from another thread other than the main thread of the form. You need to use a delegate. This will do the trick: Private _iLinks As Integer Dim thread1 As System.Threading.Thread Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click CheckForIllegalCrossThreadCalls = False _iLinks = WebBrowser1.Document.Links.Count thread1 = New System.Threading.Thread(AddressOf GetLinks) thread1.Start() End Sub Private Sub GetLinks() For i As Integer = 0 To _iLinks - 1 UpdateTextBoxDelegate(i) Next End Sub Private Sub UpdateTextBox(ByVal iLink As Integer) If TextBox1.Text.Length > 0 Then TextBox1.Text += Environment.NewLine & WebBrowser1.Document.Links(iLink).ToString Else TextBox1.Text += WebBrowser1.Document.Links(iLink).InnerText.ToString() End If End Sub Private Delegate Sub UpdateTextBoxCallback(ByVal iLink As Integer) Private Sub UpdateTextBoxDelegate(ByVal iLink As Integer) Try If Me.InvokeRequired Then Dim cb As New UpdateTextBoxCallback(AddressOf UpdateTextBox) Me.Invoke(cb, New Object() {iLink}) Else UpdateTextBox(iLink) End If Catch ex As Exception MessageBox.Show("There was an error " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.Navigate("http://stackoverflow.com/") End Sub