GetElementById gives me an exception - VB.NET - vb.net

I'm working on some code in VB.NET which will do some things automatically.
I have a couple of buttons which will do some actions manually. But I also have one button that does some actions automatically.
If I press the buttons to run it manually, it works fine.
But when I press the button to run it automatically, it gives me an Object reference not set to an instance of an object exception.
This is the code that gives the exception:
Private Sub btnStep4_Click(sender As Object, e As EventArgs) Handles btnStep4.Click
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
Dim ran As Boolean = False
For Each webpageelement As HtmlElement In allelements
WebBrowser1.Document.GetElementById("website_name").SetAttribute("value", txtWebName.Text + txtWebExt.Text) '< This one
WebBrowser1.Document.GetElementById("website_code").SetAttribute("value", txtWebName.Text) '< And this one
If webpageelement.GetAttribute("title") = "Website opslaan" And ran = False Then
webpageelement.InvokeMember("click")
ran = True
End If
Next
End Sub
So when I click the btnStep4 button, it works. But when I simulate the click like this:
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
btnStep4_Click(sender, e)
I get the exception.
Why is this? Is there something I'm missing?
This might be a stupid mistake, but I just can't find out what I'm doing wrong.
Any help or tips would be greatly appreciated!

Related

Radio buttons fire in VB net at form load

I have a simple VB net form with two radio buttons in a group box and button outside the group box that calls another form that sets up other parameters. The radio buttons both send data over the serial port.
Form1_load event has a boolean value form_loading = True. This is checked in the rbtn handler and if true should exit the subroutine. On debug, the check changed even fires one button's event that is checked at design time and at this point the form_loading value is set to false and I have no idea why. There is no form_loading = false statement. If I remove the rbtn handler, the form_loading = True persists when the other form is called and returned. The groupbox with buttons is activated as it sees a rbtn1-CheckChanged when the form loads and form_loading value get set to false. I suspect that the rbtn event is firing as the form begins to load, before the form_loading = True statement is reached, but how do I stop it firing the button event?
As its stands, when debug start, there is an IO exception error:
Serialport is closed, and the code associated with the button is in
the buffer to send to a (closed) com port
Private Sub rbtnDon_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnDon.CheckedChanged
If form_loading Then
Exit Sub
ElseIf rbtnDoff.Checked = True Then 'event fires when other button checkchanged = true, this stops it
Exit Sub
Else
data_out = (SOT + "N" + EOT)
SendtoBoard(data_out)
End If
End Sub`enter code here`
Thanks for your help Everyone. Elsewhere I found this tip: Remove the "handles rbtnDon checkchanged" from the sub:
Private Sub rbtnDon_CheckedChanged(sender As Object, e As EventArgs)handles rbtnDon checkchanged
and add this to the form1_load sub:
AddHandler rbtnDon.CheckedChanged, New EventHandler(AddressOf rbtnDon_CheckedChanged)
That seems to have solved the problem completely.
I could not add the startup code on my original post as for some reason the box will not accept two lots of separate code - or I am not doing it right:
It is here:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
ccbComPort.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
If ccbComPort.Items Is Nothing Then
lblMessage.ForeColor = Color.Red
lblMessage.Text = "No Serial Ports found"
Else
ccbComPort.Text = My.Settings.oldPort
End If
ccbBaudrate.Text = My.Settings.oldBaud
pnlComPorts.Visible = True
'form_loading = True
rbtnDon.Enabled = True
AddHandler rbtnDon.CheckedChanged, New EventHandler(AddressOf rbtnDon_CheckedChanged)
End Sub
Originally I had one form with a panel for the Com port setup and everything was fine, the issue only started when I moved the port set up to form2, after which the rbtnDon event fired before form1 started.
I did try the Sub New() approach but then I just get a small blank form on debug. Adding Form1_load to it results in "NotImplemented Exception".
The method described above seems OK and fairly simple to add but maybe it is not good practice?

Listview Checkbox - vb.net

Ok, i have a listview with checkboxes and a button, how it works is that i have to check the items i want to change the values, then press the button to change the value of those checked item, here's my code on the button.
Try
Dim I As Integer
If lv_id.CheckedItems.Count = 0 Then
For I = 0 To lv_id.Items.Count - 1
lv_id.Items(I).SubItems(1).Text = "Pending"
Next
Else
For I = 0 To lv_id.CheckedItems.Count - 1
lv_id.CheckedItems(I).SubItems(1).Text = "Submitted"
Next
End If
Proc_Items.BackColor = Color.Green
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Now, what i want to do is, to remove the button and then when i'll check the item i want to the code above will do the process without pressing the button, i tried "ItemCheck, ItemChecked" event, but with no luck.
You should be able to use the ItemChecked event for this. With the ItemCheckedEventArgs giving you everything you need.
This example would set up to toggle the 3rd column depending on the checkbox state
Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
If e.Item.Checked Then
e.Item.SubItems(2).Text = "Submitted"
Else
e.Item.SubItems(2).Text = "Pending"
End If
End Sub
If I understood you right, you want to run your code whenever a ckecbox in your ListView gets checked or unchecked. This can be done using
Public Class Form1
'The ListView_SelectedIndexChanged event triggers when a checkbox of the listview gets checked or unchecked
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
'Put your code here, access all checked items with "ListBox1.CheckedItems"
End Sub
End Class
This worked for me, and I think it works for you as well. If not, please tell me in the comments what went wrong.

VB: Make webbrowser1 click a button on a webpage

Well, I guess title says most of it. I already know how to click a button within a browser but the code doesn't work on this pacific button. I hope this code is allowed on here, sorry if it's not...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button3.Enabled = True
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
'AMF Email
WebBrowser1.Document.GetElementById("Email").InnerText textbox1.text
'AMF Password
WebBrowser1.Document.GetElementById("password").InnerText = textbox2.text
'AMF Login Button
If webpageelement.GetAttribute("value") = "Login" Then
webpageelement.InvokeMember("click")
End If
Next
'Navigates to FB likes to begin the process
WebBrowser1.Navigate("http://addmefast.com/free_points/facebook_likes")
Button2.PerformClick()
End Sub
Okay so all this code works perfectly, but now I want it to click the "Like" button on this link: http://addmefast.com/free_points/facebook_likes
However, I can not find the correct value. Can anyone help get the correct value for the "Like" button on that page?
VB2012
When I try to "Inspect Element" on the button, this is all it shows;
<div class="btn3">
Like
</div>
You can use the following code to do this:
For Each elem as HtmlElement in WebBrowser1.Document.All
If elem.GetAttribute("class") = "btn3" Then
elem.InvokeMember("click")
End If
Next
Sorry if there is any mistakes here. I am use to using an IDE for my code...
I hope this helps,
Rodit

How to make a KeyDown work twice

I am working on a little story type thing in visual basic. Nothing too complicated but i have run into a problem. I am trying to make a pause feature. I have it all working to when i press escape, the form goes into a menu type thing. The only problem is that it only works once. The first press works, but if i hit continue, it continues from where i left off. But if i press it again, nothing happens. Here is the code i am using:
Private Sub Story_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Text.Hide()
Background.Hide
ContinueButton.Show
OptionsButton.Show
End If
End Sub
Is there any way for me to make this work more than just once? I am using a KeyDown event in a private sub.
Hope this made sense, but thanks for any help guys!
You're using Form.KeyPreview = true to capture the form keystrokes. I think Text.Hide() or one of Show operations could be the problem such as getting the TabStop's out of order or leaving a gap in the tab sequence.
Try resetting the controls or using SendKeys to reset the tab sequence index (to the approriate a control to have focus), For example, Tab forward and Alt Tab back:
SendKeys.Send("{TAB}");
or
SendKeys.Send("%{TAB}");
Ref:
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx
Using a class level boolean as toggle should work:
Dim Hide As Boolean = False
Private Sub Story_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Hide = Not Hide
If Hide Then
Text.Hide()
Background.Hide
ContinueButton.Show
OptionsButton.Show
Else
Text.Show()
Background.Show
ContinueButton.Hide
OptionsButton.Hide
End If
End Sub
If nothing else helps, you can always have a private class-level variable like _processingKeyDown, set to True at handler start, and then to False when done. The first line in the handler would be:
If _processingKeyDown Then Return

Hide a row in DataGridView

I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I have googled this but have yet to find a way to do it.
At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
'Get the text of the item that was clicked on.
'Dim text As String = txtCustomerActive.Text
Try
'txtCustomerActive.Visible = False
pnlContextMenuStrip1.Visible = False
MessageBox.Show(txtCustomerActive.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You could use Rows.Item() to hide specific DataGridViewRow, like:
If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If
I am assuming you are using FullRowSelect here.
If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:
If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If
To Unhide everything let's say from a Button Click you could have this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub
As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).
However, I believe that such feature could be done with some client-side javascript progamming.
Hope this helps!