VB.NET Webbrowser to textbox - vb.net

I need help getting all of the text from WebBrowser1 to my textbox1.text
I tried
WebBrowser1.Navigate(TextBox3.Text)
TextBox1.Text = WebBrowser1.DocumentText
textbox3 being my website
and textbox1 being were i want all the text.

You have to handle the DocumentCompleted event of WebBrowser control.
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object,
e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
Handles WebBrowser1.DocumentCompleted
TextBox1.Text = WebBrowser1.DocumentText
End Sub

My solution:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' WebBrowser1
' TextBox1
' TextBox2
'
WebBrowser1.ScriptErrorsSuppressed = True ' we would like to suppress scripts error message
WebBrowser1.Navigate("http://codeguru.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' 1) Get entire html code and save as .html file
TextBox1.Text = WebBrowser1.DocumentText
' HOWTO retry while error UNTIL ok
' this needs to be done because Body.InnerText returns error when called too soon
' 2) Get Body text and save as .txt file
Dim retry As Boolean = True
Dim body As String = ""
While retry
Try
body = WebBrowser1.Document.Body.InnerText
retry = False
Catch ex As System.IO.IOException
retry = True
Finally
TextBox2.Text = body
End Try
End While
End Sub
End Class

Related

get file name without files extension using drag and drop vb.net

I want to drag and drop a file on a button and store the files Name without extension to a text box. Help to get out of this problem. I am getting some errors on those codes.
Private Sub Button5_DragDrop(sender As Object, e As DragEventArgs)
_Handles Button5.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
Dim file_name As String = Path.GetFileName(files(0))
For Each path In files
TextBox1.Text = (path)
Next
TextBox2.Text = files(0)
End Sub
Private Sub Button5_DragEnter(sender As Object, e As DragEventArgs)
_Handles Button5.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
I feel you missed setting AllowDrop feature to true
and also there is a function to get filename without extension "GetFileNameWithoutExtension"
Check the code Below
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Button5.AllowDrop = True
End Sub
Private Sub Button5_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Button5.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each indpath In files
TextBox1.Text = Path.GetFileNameWithoutExtension(indpath) & vbNewLine & TextBox1.Text
Next
End Sub
Private Sub Button5_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Button5.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
End Class

Using RFID in multiple windows forms

Hie there.
I am trying to use RFID tags on different forms. The code I have works fine if in one from. As soon you add it to another form it stops. I have tried using event handlers to no success. Does anyone know how I coud do this.
Here is my code:
Public Class Form1
Dim WithEvents phidgetRFID As Phidgets.RFID
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'To reduce code complexity we assume that there is one PhidgetRFID
'attached to the PC before the program is run.
phidgetRFID = New Phidgets.RFID()
phidgetRFID.Open()
'Defaults for text fields
txtStatus.Text = "Not Connected"
End Sub
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
lvPhidgetInfo.MultiColumn = False
lvPhidgetInfo.Items.Insert(0, "TagID Count")
End Sub
Private Sub phidgetRFID_Attach(ByVal sender As Object, ByVal e As Phidgets.Events.AttachEventArgs) Handles phidgetRFID.Attach
'When the Phidget RFID attaches update the form text boxes
Label1.Text = "Phidget RFID Reader has Attached"
txtStatus.Text = "Connected"
txtNumOutputs.Text = phidgetRFID.outputs.Count
txtSerialNumber.Text = (Str(phidgetRFID.SerialNumber))
phidgetRFID.Antenna = True
CheckBox3.Checked = True
phidgetRFID.LED = True
CheckBox2.Checked = True
End Sub
Private Sub phidgetRFID_Detach(ByVal sender As Object, ByVal e As Phidgets.Events.DetachEventArgs) Handles phidgetRFID.Detach
'If the Phidget RFID detaches close the form
Me.Close()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'If the form closes and the Phidget RFID is attached Close it.
If phidgetRFID.Attached = True Then
phidgetRFID.close()
End If
End Sub
Private Sub phidgetRFID_Error(ByVal sender As Object, ByVal e As Phidgets.Events.ErrorEventArgs) Handles phidgetRFID.Error
'If the Phidget RFID is not attached when the form opens show
'message box and close the form
MessageBox.Show(e.Description)
Me.Close()
End Sub
Private Sub phidgetRFID_RFIDTag(ByVal sender As Object, ByVal e As Phidgets.Events.TagEventArgs) Handles phidgetRFID.Tag
Static count As Integer
count = count + 1
lvPhidgetInfo.BeginUpdate()
lvPhidgetInfo.Items.Insert(1, e.Tag & " " & count)
lvPhidgetInfo.EndUpdate()
End Sub
Private Sub phidgetRFID_RFIDTagLost(ByVal sender As Object, ByVal e As Phidgets.Events.TagEventArgs) Handles phidgetRFID.TagLost
lvPhidgetInfo.BeginUpdate()
lvPhidgetInfo.Items.Insert(1, e.Tag & " Lost")
lvPhidgetInfo.EndUpdate()
End Sub
I have also tried this and I crushes without reporting an error.
Dim PhidgetRFID As New Phidgets.RFID()
AddHandler PhidgetRFID.Attach, AddressOf rfid_Attach
AddHandler PhidgetRFID.Detach, AddressOf rfid_Detach
AddHandler PhidgetRFID.Tag, AddressOf rfid_Tag
AddHandler PhidgetRFID.TagLost, AddressOf rfid_TagLost
AddHandler PhidgetRFID.Error, AddressOf rfid_Error
PhidgetRFID.open()
PhidgetRFID.waitForAttachment(3000)
Private Sub rfid_Attach(sender As Object, e As AttachEventArgs)
Label3.Text = "RFID reader {0} attached!" & e.Device.SerialNumber.ToString()
'Throw New NotImplementedException
End Sub
Private Sub rfid_Detach(sender As Object, e As DetachEventArgs)
Label4.Text = "RFID reader {0} ditached!" & e.Device.SerialNumber.ToString()
End Sub
Private Sub rfid_Tag(sender As Object, e As TagEventArgs)
Try
txtCardNumber.Text = e.Tag
Dim lastRFIDTag As String = txtCardNumber.Text
phidgetRFID.LED = True
' Throw New NotImplementedException
Catch ex As Exception
MsgBox("" & ex.Message, , "")
End Try
End Sub
Private Sub rfid_TagLost(sender As Object, e As TagEventArgs)
'Throw New NotImplementedException
phidgetRFID.LED = False
End Sub
Plaese help..

VB.Net multiple process with checkbox

I want to know the code to the following illustration.
i have one form with some checkboxs and one button,
screen is here
i've try with this code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = True And CheckBox4.Checked = True Then
'when the button is clicked will be the process for moving images
'Like
System.IO.File.Copy(Application.StartupPath + "\File\Pic1.jpg", "D:\File\Pic1.jpg")
End If
End Sub
I was tired with that code, is there a shorter coding ?
for example, if the checkbox1.checked = true and another checkbox not checked then only moving one pict
If I understand the question, you want to copy pictures 1 to 4 if the checkboxes 1 to 4 are checked.
Try this:
Dim SourcePath As string = Application.StartupPath + "\File\"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
CopyFile(CheckBox1, "Pic1.jpg")
CopyFile(CheckBox2, "Pic2.jpg")
CopyFile(CheckBox3, "Pic3.jpg")
CopyFile(CheckBox4, "Pic4.jpg")
End Sub
Private Sub CopyFile(CB As CheckBox, FileName As String)
If CB.Checked Then
System.IO.File.Copy(SourcePath + FileName, "D:\File\" + FileName)
End If
End Sub

Using open file dialog and save file dialog with a list box in VB

I need to save things someone adds to a list and open a txt file putting it into a list box. When I open a txt file I only get one line of code and my attempts to save only produce empty txt files. Any help will be greatly appreciated. Here is my code:
Imports System.IO
Public Class Form1
Public Listed As String
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim AllText As String = "", LineOfText As String = ""
Dim StreamToDisplay As StreamReader
OpenFileDialog1.Filter = "Text files (*.txt)}|*.txt"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Try
StreamToDisplay = My.Computer.FileSystem.OpenTextFileReader(OpenFileDialog1.FileName)
Label1.Text = OpenFileDialog1.FileName
Do Until StreamToDisplay.EndOfStream
LineOfText = StreamToDisplay.ReadLine()
'AllText = AllText & LineOfText & vbCrLf
lstBox.Items.Add(Listed)
Loop
lstBox.Items.Add(AllText).ToString()
StreamToDisplay.Close()
CloseToolStripMenuItem.Enabled = True
OpenToolStripMenuItem.Enabled = False
Catch ex As Exception
MsgBox("An error occurred." & vbCrLf & ex.Message)
End Try
End If
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, lstBox.Items.ToString(), False)
End If
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
lstBox.Items.Clear()
Label1.Text = ""
CloseToolStripMenuItem.Enabled = False
OpenToolStripMenuItem.Enabled = True
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim Prompt As String = "Enter Items To Add Here"
Listed = InputBox(Prompt)
lstBox.Items.Add(Listed).ToString()
End Sub
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
With lstBox
.Items.Remove(.SelectedItem)
End With
End Sub
End Class
Here's a simple example that:
adds items to a ListBox
saves them to a file
loads them from a file and populates the ListBox with them
Code:
Imports System.IO
Public Class Form1
Private Sub ButtonAddItem_Click(sender As Object, e As EventArgs) Handles ButtonAddItem.Click
ListBox1.Items.Add(DateTime.Now.Ticks)
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Using writer = New StreamWriter(SaveFileDialog1.FileName)
For Each o As Object In ListBox1.Items
writer.WriteLine(o)
Next
End Using
End If
End Sub
Private Sub ButtonLoad_Click(sender As Object, e As EventArgs) Handles ButtonLoad.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim lines = File.ReadAllLines(OpenFileDialog1.FileName)
ListBox1.Items.Clear()
ListBox1.Items.AddRange(lines)
End If
End Sub
End Class

How to recieve and read data through UART from microcontroller using VB

I will be posting my code for whatever I have done till now.
I'm sending data to microcontroller, but now I've to check other way and want to receive data.
I have written code which itself acts as UART. I am using readexisting and able to read some characters which is unrelevent, but I'm not able to read required data.
Imports System.IO.Ports
Public Class Settings
Dim myComPort As New SerialPort
' Call a routine to write a command to turn on an LED and read the response.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SendCommand("7")
End Sub
' Call a routint write a command to turn on an LED and read the response.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendCommand("A")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SendCommand("C")
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
TextBox4.Text = myComPort.BaudRate
End Sub
' If myComPort is open, finish transmitting.
' Exiting the Using block closes the port and releases its resources.
Sub CloseComPort()
Try
Using myComPort
If (Not (myComPort Is Nothing)) Then
' The COM port exists.
If myComPort.IsOpen Then
' Wait for the transmit buffer to empty.
Do While (myComPort.BytesToWrite > 0)
Loop
End If
End If
End Using
Catch ex As UnauthorizedAccessException
' The port may have been removed. Ignore.
End Try
End Sub
' Set the BaudRate property of myComPort to match the bit rate selected in the combo box.
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBitRate.SelectedIndexChanged
myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
'TextBox2.Text = ToString(myComPort.BaudRate)
End Sub
' If the previously selected COM port is open, close it.
' Set the PortName property of myComPort to match the port selected in the combo box.
' Call a routine to open the port.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPorts.SelectedIndexChanged
CloseComPort()
myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
' myComPort.BaudRate = 500
'TextBox2.Text = myComPort.BaudRate
OpenComPort()
End Sub
' Call a routine to close the COM port.
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
CloseComPort()
End Sub
' Call routines to initalize the form and open the selected COM port.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
InitializeForm()
OpenComPort()
AddHandler myComPort.DataReceived, AddressOf DataReceivedeventHandler
End Sub
' Set up the form and select a default port and bit rate.
Sub InitializeForm()
Dim bitRates(9) As Integer
Dim nameArray() As String
' Find the COM ports on the system.
nameArray = SerialPort.GetPortNames
Array.Sort(nameArray)
' Fill a combo box with the port names.
cmbPorts.DataSource = nameArray
cmbPorts.DropDownStyle = ComboBoxStyle.DropDownList
' Select a default port.
'cmbPorts.SelectedIndex = 1
'Bit rates to select from.
bitRates(0) = 300
bitRates(1) = 600
bitRates(2) = 1200
bitRates(3) = 2400
bitRates(4) = 9600
bitRates(5) = 14400
bitRates(6) = 19200
bitRates(7) = 38400
bitRates(8) = 57600
bitRates(9) = 115200
'Place the bit rates in a combo box.
cmbBitRate.DataSource = bitRates
cmbBitRate.DropDownStyle = ComboBoxStyle.DropDownList
' Select a default bit rate.
' If (Not (cmbBitRate Is Nothing)) Then
'CloseComPort()
'End If
End Sub
' Set port parameters and open the COM port
' associated with the SerialPort object myComPort.
Sub OpenComPort()
Try
' Get the selected COM port's name from the combo box.
If Not myComPort.IsOpen Then
myComPort.PortName = cmbPorts.SelectedItem.ToString
' Get the selected bit rate from the combo box.
If cmbBitRate.SelectedIndex > 0 Then
myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
TextBox4.Text = myComPort.BaudRate
End If
'myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
' Set other port parameters.
myComPort.Parity = Parity.None
myComPort.DataBits = 8
myComPort.StopBits = StopBits.One
myComPort.Handshake = Handshake.None
myComPort.ReadTimeout = 3000
myComPort.WriteTimeout = 5000
' Open the port.
myComPort.Open()
End If
Catch ex As InvalidOperationException
MessageBox.Show(ex.Message)
Catch ex As UnauthorizedAccessException
MessageBox.Show(ex.Message)
Catch ex As System.IO.IOException
MessageBox.Show(ex.Message)
End Try
End Sub
' Write a command to the SerialPort object and read the response.
''' <param name= "command"> The command to send. </param>
Private Sub SendCommand(ByVal command As String)
' Dim response As String
Try
'TextBox1.BackColor = Color.MediumPurple
myComPort.Write(command)
'myComPort.ReadTimeout = 1000
' While myComPort.ReadLine = 0
' End While
'If myComPort.IsOpen Then
'myComPort.ReadLine()
'End If
'TextBox6.Text = myComPort.ReadLine
Select Case command
Case "A"
' TextBox6.Text = myComPort.
TextBox1.Text = "Green LED is on"
TextBox1.BackColor = Color.LightGreen
Case "7"
TextBox1.Text = " Red LED is ON"
TextBox1.BackColor = Color.Red
'TextBox6.Text = myComPort.ReadLine
Case "C"
TextBox1.Text = " Both LED's are ON"
TextBox1.BackColor = Color.RoyalBlue
'TextBox6.Text = myComPort.ReadLine
Case Else
End Select
Catch ex As TimeoutException
MessageBox.Show(ex.Message)
Catch ex As InvalidOperationException
MessageBox.Show(ex.Message)
Catch ex As UnauthorizedAccessException
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub DataReceivedeventHandler(
ByVal sender As Object,
ByVal e As SerialDataReceivedEventArgs)
Dim sp As SerialPort = CType(sender, SerialPort)
Dim indata As String = sp.ReadExisting()
' Dim jndata As String = sp.ReadChar
MsgBox(indata)
'TextBox6.Text = CStr(vbMsgBoxRtlReading)
' MsgBox(jndata)
' Console.WriteLine("Data Received:")
Console.Write(indata)
' ArgumentException.Equals(indata)
End Sub
Public Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
End Sub
'Private Sub DataReceivedEventHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
' Throw New NotImplementedException
'End Sub
End Class