How to automatically input in the WebBrowser control - vb.net

I'm quite very amateur in vb.net. When I type a text on textbox, it should be able to automatically input on the webbrowsercontrol and also how to click the button signin, wherein no getelementbyid.
Also I manage to get the 1st part correct, but when I click sign in button from inside browser, there seems to be a minor error. I've made a project like this one before long long time ago and can't find the source code of it anymore, so I'm starting from scratch again.
Website: https://app.coins.ph/welcome/login
Heres my code so far:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
WebBrowser1.Document.GetElementById("username").InnerText = TextBox1.Text
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("class") 'Depending on how the source code is formatted on the tag, you may also try Element.OuterHTML, Element.InnerText and Element.OuterText in the line below
If Element.OuterText.Contains("SIGN IN") Then
Element.InvokeMember("click")
Exit For
End If
Next Element
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
WebBrowser1.Document.GetElementById("password").InnerText = TextBox2.Text
End Sub

You've got a couple of issues here. Let's start with the textbox inputs. If you look at the html source for that website's sign-in page, the inputs for username and password do not have an ID property, they only use Name. Moreover, GetElementsByTagName is searching for a html element of "username", not an "input" as it should. Given both of those issues, you should be using Document.All("[elementName]") to access those inputs. As for the sign-in part, as stated before, GetElementsByTagName is looking for html elements, so searching for the value "class" is not going to return anything you want. Instead, you should be looking for a "button" where the OuterText contains "SIGN IN". With all those changes applied, the code becomes:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("https://app.coins.ph/welcome/login")
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
WebBrowser1.Document.All("username").InnerText = TextBox1.Text
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
WebBrowser1.Document.All("password").InnerText = TextBox2.Text
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
If Element.OuterText.Contains("SIGN IN") Then
Element.InvokeMember("click")
Exit For
End If
Next Element
End Sub
End Class
There is another problem once you run this code though
If you run the above example, you will see that the form-fields are properly filled in and the sign-in button is clicked successfully, however an error appears indicating that the form fields are still blank. Even if you use WebBrowser1.Document.All("username").SetAttribute("value", TextBox1.Text) to set the input's value as well, the same error occurs. This is likely because the website's developers are using some sort of javascript that is detecting keypresses for one reason or another...it's impossible to know why, but that's how it is. So you're left with actually simulating key presses yourself. If you do that, the website will successfully log in with the username and password. You have two ways of doing this. The cleaner way is to just send all the keys at once and log in like so:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Focus()
WebBrowser1.Document.All("username").Focus()
For Each c As Char In TextBox1.Text.ToCharArray
SendKeys.SendWait(c)
Next
WebBrowser1.Document.All("password").Focus()
For Each c As Char In TextBox2.Text.ToCharArray
SendKeys.SendWait(c)
Next
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
If Element.OuterText.Contains("SIGN IN") Then
Element.InvokeMember("click")
Exit For
End If
Next Element
End Sub
However, if you still want each character to appear as you type, to mirror the functionality of the TextChanged event logic you are currently using you would have to use the KeyPress event and basically forward the keystrokes like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
If Element.OuterText.Contains("SIGN IN") Then
Element.InvokeMember("click")
Exit For
End If
Next Element
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
WebBrowser1.Focus()
WebBrowser1.Document.All("username").Focus()
SendKeys.SendWait(e.KeyChar)
TextBox1.Focus()
End Sub
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
WebBrowser1.Focus()
WebBrowser1.Document.All("password").Focus()
SendKeys.SendWait(e.KeyChar)
TextBox2.Focus()
End Sub

Related

VB.net One sub for many events

I'm working on a function in VB.Net where a user can select a supplier from a list.
The idea is that the user will filter the list until the right supplier is visible in a datagridview
the user can then either double click on the row header, the cell content or select a supplier and then click an OK button
I am wondering though, how do I avoid building one Sub for each of the above three events, Can I create one sub that catches all three events?
Private Sub supplierSearchOkButton_Click(sender As Object, e As EventArgs) Handles supplierSearchOkButton.Click
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Private Sub supplierSearchDataGridView_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles supplierSearchDataGridView.CellContentDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Private Sub supplierSearchDataGridView_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Personally, I would go with the extra method option mentioned in the comments but, if you want to, you should be able to do this:
Private Sub SetSupplier(sender As Object, e As EventArgs) Handles supplierSearchOkButton.Click,
supplierSearchDataGridView.CellContentDoubleClick,
supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
or even this:
Private Sub SetSupplier() Handles supplierSearchOkButton.Click,
supplierSearchDataGridView.CellContentDoubleClick,
supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
If you're not using any properties of the other e parameters then you can use the most general EventArgs for all three events and if you're not using the parameters at all then you can ditch them altogether. I didn't test this specifically but I'm fairly sure both will work.

Enable/Disable a form while in an another form

I'm trying to figure out how to disable my form(Form1.vb) without hiding it then enabling it after I'm done with the other form(Form2.vb).
I've searched on youtube but it says C#. I've tried it but somehow it was indicated as an error in VS 2015. I tried messing around with the syntax because I really can't figure it out. The syntax that I have tried is "LandingForm.ActiveForm.Owner.Enabled = True".
Indicated below are the codes of my system. The first one is form1.vb/LandingForm.vb and the second one is form2.vb/AcctSettings.vb.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Enabled = False
AcctSettings.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
LandingForm.ActiveForm.Owner.Enabled = True
Me.Hide()
End Sub
Am I missing something? Can somebody help?
On your form1 just have a simple line like
Form2.Show()
wherever/however you wish to open the other form (button etc.)
then in the code for that form2 in the formload handeler have
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Enabled = False
End Sub
This will keep form1 open but basically grey it out.
Then have a simple button click like so to have access to form 1.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form1.Enabled = True
End Sub
And you are done! And if you wish just add another button or a IF statement to the button2 sub and say form1.enabled = false if you want to be able to enable/disable etc.
To enable/disable your userform :
To disable your userform you will need yo enable it before :
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Enabled = True
Form1.Enabled = False
End Sub
To disable it will be easier you just got to set your userform.enabled as False
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form2.Enabled = True
End Sub
If you want to close your userform useroform.Unload might be the solution.
In your code it would be like this :
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Enabled = False
AcctSettings.Show()
form1.Unload
End Sub
and
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
LandingForm.ActiveForm.Owner.Enabled = True
Me.Hide()
form2.Unload
End Sub
an other option is to Hide your user form : it will just hide the userform and will not release the objects and variables from the memory. Where as Unload method will release the objects and variables from the memory.
UserForm1.Hide
To conclude : Hide method will be used when we want to hide the form temorarly and to show after sometime to the user. Where as unload will be used when it completes the task.
Note that this is YourUserForm_Name.Unload

Trying to enter a name in a textbox, hit ok, and have that form close and pass the info to another forms label

***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub

Windows Forms: Keeping Check Beside Checked Box list item and displaying separate message for each

When I run my program currently, Within any checked box list I am unable to actually select an option (i.e. the box beside it, to tick).
I would also like each individual option to display a messagebox when ticked (i.e. "User story one added"), But currently my program only displays a general messagebox ("User Story selected") once the checked box list is clicked on. Any help would be much appreciated!
Current Code:
Public Class Form2
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
MessageBox.Show("User Story Selected")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
You are responding to the wrong event and might be likely to examine the wrong property.
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ItemCheckEventArgs)
MessageBox.Show("User Story Selected")
End Sub
Checks fire the ItemCheck event, and checked items are in the CheckedItems Collection. The SelectedItems collection is literally those selected (highlighted) which might not also be checked. It is not really a list of checkboxes, but a list of items drawn as checks - thats why they look different than regular checks. To see which item:
For n as Integer = 0 to CheckedListBox1.CheckedItems.Count-1
userWants = CheckedListBox1.CheckedItems(n)
Next n
Like a ListBox you can put anything in there, not just strings, so it was a list of stories, you might be able to do:
userWants = CheckedListBox1.CheckedItems(n).StoryName

TextBox Highlighted Text Visual Basic

I have a problem with my TextBox. I'm trying to make a small program like memo. When you write something in the textbox it saves it for next time for you to open it again. But the problem is when you open the program once again, the old text is highlighted. So how I remove that?
Public Class Form1
Private Sub
Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.memo = TextBox1.Text My.Settings.Save()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.memo + Environment.NewLine
End Sub
End Class
See the TextBox.SelectionStart property