I have four textbox for an IP Address. Each textbox is for each of the octate.
When the control loses focus or when user presses ENTER, it will check if the value is greater than 255.
Private Sub txtParametersIpFirst_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtParametersIpFirst.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
'Check if Value is more than 255
End If
End Sub
Private Sub txtParametersIpFirst_LostFocus(sender As Object, e As EventArgs) Handles txtParametersIpFirst.LostFocus
'Check if Value is more than 255
End Sub
Is there something that these two events be combined?
The best way to combine two events is by calling a method..
Private Sub Logic()
'Check if Value is more than 255
End Sub
Private Sub txtParametersIpFirst_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtParametersIpFirst.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
Logic()
End If
End Sub
Private Sub txtParametersIpFirst_LostFocus(sender As Object, e As EventArgs) Handles txtParametersIpFirst.LostFocus
Logic()
End Sub
However, if you're to combine two same events, for example two Lostfocus event of two different textboxes, then you can just add handles;
Private Sub txtText1_LostFocus(sender As Object, e As EventArgs) Handles txtText1.LostFocus, txtText2.LostFocus,
' Code similar for both textbox
' You can use the Sender param to verify who's triggering the event
End Sub
Why dont you go for masking like this ###.###.###.### and use the Validation event to confirm that the value entered is a valid IP address.By this way there is no need for four different text boxes
There is some good material on This link.and MSDN
Related
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.
Recently I've been working on a program that has a few TextBoxes, CheckBoxes, ComboBoxes, etc., and I found that making one function handle multiple events is pretty simple, you just separate the events with a comma and the code recognizes the inidvidual events.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click, Button2.Click
MsgBox("Hello World!")
End Sub
However, when you start to have a large number of events that you want handled by the same function, it gets a bit messy.
Private Sub Checks_CheckedChanged(sender As Object, e As EventArgs) Handles chkInput1.CheckedChanged, chkInput2.CheckedChanged, chkInput3.CheckedChanged, chkInput4.CheckedChanged, checkInput5.CheckedChanged, chkOutput.CheckedChanged
MsgBox("Checks Changed!")
End Sub
You can use the line continuation character _ to make it look a little better.
Private Sub Checks_CheckedChanged(sender As Object, e As EventArgs) Handles _
chkInput1.CheckedChanged, chkInput2.CheckedChanged, chkInput3.CheckedChanged, _
chkInput4.CheckedChanged, checkInput5.CheckedChanged, chkOutput.CheckedChanged
MsgBox("Checks Changed!")
End Sub
But you still end up with a nasty block of text. Is there a more clean/concise way of doing this? What I have in mind is that it would be really nice to give an array of object events as an argument but I don't think that's possible.
You could do this by using the
AddHandler ObjectName.EventName, AddressOf EventHandlerName
syntax
It's simple enough to write a Sub that takes an array of object and loops over them to add the handler for each event.
For checkboxes:
Public Sub AddHandlerSub(PassedArray As CheckBox())
For Each item As CheckBox in PassedArray
AddHandler Item.CheckedChanged, AddressOf EventHandlerName
next
End Sub
You can simply iterate the controls in the controls collection and not fuss with an array at all. You can also do further conditions if you want to exclude/add any given control, such as in the example of the TextBox Case in the following example.
Private Sub DataTables_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each Ctrl As Control In Panel1.Controls
Select Case Ctrl.GetType
Case GetType(CheckBox)
AddHandler DirectCast(Ctrl, CheckBox).CheckedChanged, Sub(S As Object, EA As EventArgs)
Dim ChkBox As CheckBox = DirectCast(S, CheckBox)
'do something with ChkBox
End Sub
Case GetType(TextBox)
Select Case Ctrl.Name
Case "TextBox1", "TextBox2" 'Add handle only to these contrls
'Or you could add Case Else and put the below handle within it
'Then this becomes an exclusion case
AddHandler DirectCast(Ctrl, TextBox).TextChanged, Sub(S As Object, EA As EventArgs)
Dim TxtBox As TextBox = DirectCast(S, TextBox)
'do something with TxtBox
End Sub
End Select
End Select
Next
End Sub
Additional Information: You can select procedures as Event Handlers by selecting the control. Then in the Properties window click the lightning bolt to display Events. Selected the event you wish to assign a handler and then the drop down arrow to the right. The resulting list will display all the Subs that match the signature of that event. Select the one you want and the designer will write or append the control to the Handles clause.
Add a procedure to the Form with a signature that matches the event.
Private Sub MultipleButtons(sender As Object, e As EventArgs)
End Sub
In the dropdown the list contains all Subs that match the signature of the event.
The designer writes the Handles clause
Private Sub MultipleButtons(sender As Object, e As EventArgs) Handles Button5.Click
End Sub
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
I'm a beginner to VB and I'm looking to optimize my code a little. I have 9 textboxes, and every time one of them changes I run a Subroutine based on it's content. Is there a way to make it, perhaps with a for loop, go through all of the 9 textboxes and register when any of them changes. Here's ..my code looks like at the moment...
Private Sub tbBox1_TextChanged(sender As Object, e As EventArgs) Handles tbBox1.TextChanged
checkInput(tbBox1, 0, 0)
End Sub
Private Sub tbBox2_TextChanged(sender As Object, e As EventArgs) Handles tbBox2.TextChanged
checkInput(tbBox2, 0, 1)
End Sub
Private Sub tbBox3_TextChanged(sender As Object, e As EventArgs) Handles tbBox3.TextChanged
checkInput(tbBox3, 0, 2)
End Sub
..etc
You don't need a separate handler for each text box. A single handler method can handle all of them. If you need a value for each text box to pass into your checkInput method, just use the Tag property of each text box.
Private Sub TextBoxChanged(sender As Object, e As EventArgs) Handles tbBox1.TextChanged, tbBox2.TextChanged, tbBox3.TextChanged 'etc.
checkInput(sender, 0, sender.Tag)
End Sub
Try multiple Handles:
Private Sub tbBox_TextChanged(sender As Object, e As EventArgs) Handles _
tbBox1.TextChanged, _
tbBox2.TextChanged, _
tbBox3.TextChanged
checkInput(sender, 0, 0)
End Sub
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