Visual Basic web application call function from multiple objects - vb.net

I have a Visual Basic web application where I will have a 5x5 grid displayed on the web page. Each grid box will contain a button. I want to be able to access the button attributes when any button is clicked. I would assume I would create a private function to handle each button click (I would create 25 functions), but I want each private function to call a public function which will receive the object and the grid coordinates.
My buttons on the web pages are called btn_column1_row1, btn_column2_row1, btn_column3_row1, btn_column4_row1, btn_column5_row1, btn_column1_row2, etc.
I want to code the private functions like this:
Private Sub btn_column1_row1_Click(sender As Object, e As EventArgs) Handles btn_column1_row1.Click
inspectButton(e,100,1,1)
End Sub
Private Sub btn_column2_row1_Click(sender as Object, e As EventArgs) Handles btn_column2_row1.Click
inspectButton(e,100,2,1)
End Sub
Private Sub btn_column5_row5_Click(sender as Object, e As EventArgs) Handles btn_column5_row5.Click
inspectButton(e,500,5,5)
End Sub
Then, the function inspectButton should look something like this:
Function inspectButton(ByVal e as EventArgs, textvalue, column, row)
Dim Question as string = Session("question")
Dim Answer as string = Session("answer")
if e.text = textvalue then
e.text = Question(column, row)
else
e.text = Answer(column, row)
end if
return
End Function
Is this the correct way to pass the button object to the inspectButton function?

Private Sub btn_Click(sender as Object, e As EventArgs) Handles btn_column1_row1.Click, btn_column1_row2.Click, btn_column3_row5.Click, btn_column5_row5.Click, btn_column5_row1.Click 'etc
inspectButton(sender)
End Sub
Function inspectButton(ByVal btn as Object)
Dim Question as string = Session("question")
Dim Answer as string = Session("answer")
if btn.Text = btn.Text then
btn.Text = Question(btn.column, btn.row)
else
btn.Text = Answer(btn.column, btn.row)
end if
return
End Function
Maybe this can be a solution... i can't understand the textvalue,column and row parameters passed...
You can handle multiple events with a single sub.

Related

How can i make a subroutine click a picture box?

I have the sub routine for when the picture box is clicked.
Public Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
If player = "p1" Then
PictureBox5.BackgroundImage = My.Resources.cross
pic5 = 1
End If
If player = "p2" Then
PictureBox5.BackgroundImage = My.Resources.nought
pic5 = 2
End If
blnFlag = True
PictureBox5.Update()
End Sub
I want to create a subroutine that clicks the picture box to change the image to a nought.
Does anyone have any idea on how i would go about doing this.
There are a couple ways you can accomplish this:
Call the click event PictureBox5_Click() inside a sub:
Private Sub AnotherClick_Click(sender as Object, e as EventArgs) handles AnotherClick.Click
~Do Stuff
PictureBox5_Click(sender, e)
End Sub
Use PerformClick():
Me.PictureBox5.PerformClick()
Edit: I updated option 1 to include the full click event sub. Should add that this is not really an "acceptable" way of writing code, you shouldn't call these event handlers directly, but rather put your logic in another sub/function and call it from there whenever you need. However, if there is some reason you need to specifically call this event handler, option 1 is the way to go.
Private Sub SetPicture(PicBox as PictureBox)\
If player = "p1" Then
PicBox.BackgroundImage = My.Resources.cross
pic5 = 1
End If
If player = "p2" Then
PickBox.BackgroundImage = My.Resources.nought
pic5 = 2
End If
blnFlag = True
PicBox.Update()
End Sub
Public Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
SetPicture(PictureBox5)
End Sub
That's how it should be done... now you can also call SetPicture from anywhere else, passing in any other picture box you want...

VB.NET - Check if webbrowser's url has changed

So basically I am asking how to check if the URL of the webbrowser has changed (is diferrent from the previous one).
Thank you.
You can check ...
Sub webbrowser1_Complete(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
//Webbrowser1.url property to get valu of url
End Sub
Ok from what I can understand from your question this is what I would do. First create a structurethis will allow you to store data that you may want to use again. Next create a Function in this case with a Boolean return that checks to see if WebBrowser1's current url is the same as the one we have stored within our structure.And once you have done that, I would create a new WebBrowser1 Event in this case WebBrowser1_DocumentCompleted to trigger the Function to compare both the WebBrowser1 url textbox and the structures stored string when a web page as completely loaded.
Public Class Form1
Dim urlSettings As urlSetting
Structure urlSetting
Public url As String
End Structure
Private Function checkURL(url As String) As Boolean
Dim changed As Boolean = True
If Not urlSettings.url = url Then
changed = False
End If
Return changed
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate(New Uri(TextBox1.Text))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If Not checkURL(TextBox1.Text) Then
urlSettings.url = TextBox1.Text
MessageBox.Show("The URL has changed")
End If
End Sub
End Class
Of course you can modify so suit your needs, however this should get you on your way. :) MSDN Information Structure: https://msdn.microsoft.com/en-us/library/4ft0z102.aspx WebBrowser Control : https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser(v=vs.110).aspx Return Statesments eg Function: https://msdn.microsoft.com/en-us/library/2e34641s.aspx

Button Array - how to pass a parameter to shared handler

I have a bit of code where i have a dynamically created array or buttons with staff pictures on them, as well as the staff's name. I've added one handler to handle any button click from any of the buttons. where i am stuck is, if you look at the code below, it all works fine, and if you click any of the buttons you get the "aha" test message. but i want the name of the staff clicked on (so btnArray(i).Text) to be passed to the handler for further processing. I tried adding a ByVal parameter to the handler but that caused an error. what's the correct way to do this? As i said, the code below works for me, i just am at a loss as to how to add the extra functionality.
Dim btnArray(staffcount) As System.Windows.Forms.Button
For i As Integer = 1 To staffcount - 1
btnArray(i) = New System.Windows.Forms.Button
btnArray(i).Visible = True
btnArray(i).Width = 80
btnArray(i).Height = 101
btnArray(i).BackgroundImage = Image.FromFile(picloc(i))
btnArray(i).BackgroundImageLayout = ImageLayout.Stretch
btnArray(i).Text = staffname(i)
Dim who As String
who = btnArray(i).Text
AddHandler btnArray(i).Click, AddressOf Me.theButton_Click
btnArray(i).ForeColor = Color.White
btnArray(i).TextAlign = ContentAlignment.BottomCenter
Dim fnt As Font
fnt = btnArray(i).Font
btnArray(i).Font = New Font(fnt.Name, 10, FontStyle.Bold)
FlowLayoutPanel1.Controls.Add(btnArray(i))
Next i
End Sub
Private Sub theButton_Click()
MsgBox("aha")
End Sub
First, correct the signature of your shared handler.
Private Sub theButton_Click(sender As Object, e As EventArgs)
End Sub
Once that is done getting the text of the button clicked is a simple matter.
Private Sub theButton_Click(sender As Object, e As EventArgs)
Dim textOfButtonClicked As String = DirectCast(sender, Button).Text
MessageBox.Show(textOfButtonClicked)
End Sub
The sender is the button that was clicked. Since signatures use objects for the sender the DirectCast 'changes' it to button and you then can access the .Text property of the button.
If there are more manipulations you want to perform on the clicked button you could do it this way
Private Sub theButton_Click(sender As Object, e As EventArgs)
Dim whBtn As Button = DirectCast(sender, Button) ' get reference to button clicked
Dim textOfButtonClicked As String = whBtn.Text
MessageBox.Show(textOfButtonClicked)
'e.g. change the color
whBtn.BackColor = Color.LightYellow
End Sub

Saving textbox name when clicked in order to input text

Hey all i am in need of some help getting my code working correctly like i am needing it to. Below is my code that when the user click on the textbox, it pops up a keyboard where they can click on any letter and it will type that letter into the textbox. Problem being is i can not seem to get the name of the text box to return so that it knows where to send the letters to.
Order in firing is:
TextBox1_MouseDown
keyboardOrPad.runKeyboardOrPad
kbOrPad.keyboardPadType
ClickLetters
Form1.putIntoTextBox
Form1
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
Call keyboardOrPad.runKeyboardOrPad("SHOW") 'Just shows the keyboard
Call kbOrPad.keyboardPadType("PAD", TextBox1)
End Sub
Public Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
whatBox.Text = what2Put '<-- has error Object reference not set to an instance of an object. for the whatBox.text
End Sub
kbOrPad class
Dim theBoxName As TextBox = Nothing
Public Sub keyboardPadType(ByRef whatType As String, ByRef boxName As TextBox)
theBoxName = boxName '<-- shows nothing here
Dim intX As Short = 1
If whatType = "PAD" Then
Do Until intX = 30
Dim theButton() As Control = Controls.Find("Button" & intX, True)
theButton(0).Enabled = False
intX += 1
Loop
ElseIf whatType = "KEYB" Then
End If
End Sub
Private Sub ClickLetters(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = CType(sender, Button)
If btn.Text = "Backspace" Then
Else
Call Form1.putIntoTextBox(btn.Text, theBoxName) 'theBoxName taken from keyboardPadType
End If
End Sub
Some visuals for you:
Pastebin code: http://pastebin.com/4ReEnJB0
make sure that theBoxName is a Module scoped variable, then I would populate it like this giving you the flexibility of implementing a shared TextBox MouseDown Handler:
Private Sub TextBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
Dim tb As TextBox = CType(sender, TextBox)
Call keyboardPadType("PAD", tb)
End Sub
Try something like this
Public Class Form1
Dim myKborPad As New kbOrPad
Private Sub TextBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
Dim tb As TextBox = CType(sender, TextBox)
Call myKborPad.keyboardPadType("PAD", tb)
End Sub
Edit Based on your PasteBin code.
I noticed you already have an instance of your keyboardPadType declared in your Module, use that instead of what I said earlier. That code should look like:
remove:
Dim myKborPad As New kbOrPad
and use the theKbOrPad that you created in your module like this:
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
Dim tb As TextBox = CType(sender, TextBox)
Call keyboardOrPad.runKeyboardOrPad("SHOW")
Call theKbOrPad.keyboardPadType("PAD", tb)
'Call kbOrPad.keyboardPadType("PAD", tb)
End Sub
Also about the current error your are getting, you are trying to use the default instance of your Form1 , it isn't the actual Form that you are running, you can code around this by making the method you are trying to use as shared. Like this:
Public Shared Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
whatBox.Text = what2Put
End Sub
But however I would actually prefer to put it into your Module like this
Public Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
whatBox.Text = what2Put
End Sub
and call it like this
Call putIntoTextBox(btn.Text, theBoxName)
after making above changes your code worked.
First, you should replace the ByRef with ByVal (anytime you don't know whether you should use one or the other, use ByVal).
Secondly, I believe you don't need the method, putIntoTextBox, I think you should be able to do that directly (might be threading problems that prevent it, but I don't think that's likely based on your description). You don't show where Form1 is set (or even if it is), and that's another potential problem.
Finally, the better way to call back into the other class is to use a delegate/lambada.
(I know, no code, but you don't provide enough context for a working response, so I'm just giving text).

Search ListBox elements in VB.Net

I'm migrating an application from VB6 to VB.Net and I found a change in the behavior of the ListBox and I'm not sure of how to make it equal to VB6.
The problem is this:
In the VB6 app, when the ListBox is focused and I type into it, the list selects the element that matches what I type. e.g. If the list contains a list of countries and I type "ita", "Italy" will be selected in the listbox.
The problem is that with the .Net version of the control if I type "ita" it will select the first element that starts with i, then the first element that starts with "t" and finally the first element that starts with "a".
So, any idea on how to get the original behavior? (I'm thinking in some property that I'm not seeing by some reason or something like that)
I really don't want to write an event handler for this (which btw, wouldn't be trivial).
Thanks a lot!
I shared willw's frustration. This is what I came up with. Add a class called ListBoxTypeAhead to your project and include this code. Then use this class as a control on your form. It traps keyboard input and moves the selected item they way the old VB6 listbox did. You can take out the timer if you wish. It mimics the behavior of keyboard input in Windows explorer.
Public Class ListBoxTypeAhead
Inherits ListBox
Dim Buffer As String
Dim WithEvents Timer1 As New Timer
Private Sub ListBoxTypeAhead_KeyDown(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.A To Keys.Z, Keys.NumPad0 To Keys.NumPad9
e.SuppressKeyPress = True
Buffer &= Chr(e.KeyValue)
Me.SelectedIndex = Me.FindString(Buffer)
Timer1.Start()
Case Else
Timer1.Stop()
Buffer = ""
End Select
End Sub
Private Sub ListBoxTypeAhead_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.LostFocus
Timer1.Stop()
Buffer = ""
End Sub
Public Sub New()
Timer1.Interval = 2000
End Sub
Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Buffer = ""
End Sub
End Class
As you probably know, this feature is called 'type ahead,' and it's not built into the Winform ListBox (so you're not missing a property).
You can get the type-ahead functionality on the ListView control if you set its View property to List.
Public Function CheckIfExistInCombo(ByVal objCombo As Object, ByVal TextToFind As String) As Boolean
Dim NumOfItems As Object 'The Number Of Items In ComboBox
Dim IndexNum As Integer 'Index
NumOfItems = objCombo.ListCount
For IndexNum = 0 To NumOfItems - 1
If objCombo.List(IndexNum) = TextToFind Then
CheckIfExistInCombo = True
Exit Function
End If
Next IndexNum
CheckIfExistInCombo = False
End Function