How do I call this vb.net function on the button click event?
Private Sub GridView_UDGReport_DataBound1(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
For rowIndex As Integer = GridView_UDGReport.Rows.Count - 2 To 0 Step -1
Dim gviewRow As GridViewRow = GridView_UDGReport.Rows(rowIndex)
Dim gviewPreviousRow As GridViewRow = GridView_UDGReport.Rows(rowIndex + 1)
For cellCount As Integer = 0 To gviewRow.Cells.Count - 1
If gviewRow.Cells(cellCount).Text = gviewPreviousRow.Cells(cellCount).Text Then
If gviewPreviousRow.Cells(cellCount).RowSpan < 2 Then
gviewRow.Cells(cellCount).RowSpan = 2
Else
gviewRow.Cells(cellCount).RowSpan = gviewPreviousRow.Cells(cellCount).RowSpan + 1
End If
gviewPreviousRow.Cells(cellCount).Visible = False
End If
Next
Next
End Sub
Since you are not using the parameters anyways, you can simply call the method with Nothing as parameter.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GridView_UDGReport_DataBound1(Nothing, Nothing)
End Sub
Append the first line so that the sub handles more than one event, as follows:
Private Sub GridView_UDGReport_DataBound1(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound, Button1.Click
Alternatively, if you need your Click event to run some other code in addition to calling this sub, do this:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'do something
GridView_UDGReport_DataBound1(sender, e)
'do something else
End Sub
Related
I'm beginner level learner in using VB and currently I am trying create two buttons that will allow me to slide through pictures in an online folder. I need some kind of a way to change the image number in the URL so that the buttons will allow me to move through the pictures.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img=1&key=xxxxx")
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img=2&key=xxxxxxx")
Try this, notice the private variable.
Private _CurrentImage as Int32 = 1
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
CurrentImage += 1
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load(string.format("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img={0}&key=xxxxx",_CurrentImage ))
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
CurrentImage += 1
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.loadstring.format("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img={0}&key=xxxxx",_CurrentImage )))
End Sub
I am assuming Button4 is PREVIOUS and Button5 is NEXT. You will want to do some work to prevent numbers out of range but you can see how easy it is to Increment/Decrement a number and then stuff it in a string.
Below I have an array and in my design I have a check list box with 10 options. For example, if boxes 1 and 2 were checked, I would only want to print Indexes 0 and 1 ONLY. I have a button that prints all of the array members (included below) and that is what I want to make print only selected items. I have tried using a switch but that file had gotten corrupted and I am lost. Thank you. (Language is VB)
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn1.Click
Dim strDecimal(9) As String
strDecimal(0) = FormatPercent(0.0146175)
strDecimal(1) = FormatPercent(0.0345324585)
strDecimal(2) = FormatPercent(0.09324543575)
strDecimal(3) = FormatPercent(0.07346475)
strDecimal(4) = FormatPercent(0.0772346615)
strDecimal(5) = FormatPercent(0.42234234654)
strDecimal(6) = FormatPercent(0.6246264664)
strDecimal(7) = FormatPercent(0.4524642234)
strDecimal(8) = FormatPercent(0.6876543534)
strDecimal(9) = FormatPercent(0.6876543534)
For num As Integer = 0 To strDecimal.Length - 1
listArrays.Items.Add(strDecimal(num))
Next
End Sub
Private Sub clearList()
listArrays.Items.Clear()
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
clearList()
End Sub
Assuming you're using a CheckedListBox and want to know which items are Checked:
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
For Each itm As String In listArrays.CheckedItems
Debug.Print(itm)
Next
End Sub
I have situation that have to know which exact row of datagridview1 (MultiSelect = False) is selected on another (inactive) datagridview1 when I am in some event handler of datagridview2 which is currently active.
I try a bunch of attempts but without correct result.
Private Sub DataGridView2_CellDoubleClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView2.CellDoubleClick
Dim myindex1 As Integer = DataGridView1.CurrentCell.RowIndex
OR
Dim myindex1 As Integer = DataGridView1.CurrentRow.Index
None of this don't work like it works when datagridview1 is active and I get index from their event handlers.
What should I do and how accuratelly get selected row of first datagridview from event handler of second datagridview?
EDIT:
Added code:
Private Sub datagridview1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
selrow1 = e.RowIndex
End Sub
Private Sub datagridview1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus
End Sub
Private Sub datagridview1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.GotFocus
DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.FromKnownColor(KnownColor.Highlight)
End Sub
Private Sub datagridview1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.LostFocus
DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.FromKnownColor(KnownColor.InactiveCaption)
End Sub
I had this code:
Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
fTrafficSurveyA.incrementCount("Car")
Call updateView()
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
fTrafficSurveyA.incrementCount("Bicycle")
Call updateView()
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
fTrafficSurveyA.incrementCount("Lorry")
Call updateView()
End Sub
I've changed it to the following:
Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
ButtonClickCode(CType(sender, Button))
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
ButtonClickCode(CType(sender, Button))
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
ButtonClickCode(CType(sender, Button))
End Sub
Private Sub ButtonClickCode(ByVal mySender As Button)
Dim incrementVehicle As String
Select Case mySender.Name
Case "carButton"
incrementVehicle = "Car"
Case "bicycleButton"
incrementVehicle = "Bicycle"
Case "lorryButton"
incrementVehicle = "Lorry"
Case Else
incrementVehicle = ""
End Select
fTrafficSurveyA.incrementCount(incrementVehicle)
Call updateView()
End Sub
The new code has more lines of code.
Is the new version better practice, or is there a third pattern that I should be following?
EDIT
Using Guffa's answer + other comments I changed the underlying TrafficSurveyA model to include an enumerated Type and altered it's function incrementCount like so....is this enumerated type is in the correct place?
Enum vehicleType
Car
Lorry
Bicycle
End Enum
Public Sub incrementCount(ByVal vehicle As vehicleType)
' Preconditions: none
' Postconditions: If vehicle is "Car", "Bicycle" or "Lorry" then 1 is added
' to the corresponding count. Otherwise nothing is done.
Select Case vehicle
Case vehicleType.Car : fCars = fCars + 1
Case vehicleType.Bicycle : fBicycles = fBicycles + 1
Case vehicleType.Lorry : fLorries = fLorries + 1
Case Else 'do nothing
End Select
End Sub
The interface code has ended up like this:
Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
ButtonClickCode(TrafficSurveyA.vehicleType.Car)
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
ButtonClickCode(TrafficSurveyA.vehicleType.Bicycle)
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
ButtonClickCode(TrafficSurveyA.vehicleType.Lorry)
End Sub
Private Sub ButtonClickCode(ByVal incrementVehicle As TrafficSurveyA.vehicleType)
fTrafficSurveyA.incrementCount(incrementVehicle)
Call updateView()
End Sub
As the code in the event handlers are identcal, you can have just one event handler for all the events:
Private Sub vehicleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click, bicycleButton.Click, lorryButton.Click
Dim mySender As Button = CType(sender, Button)
Dim incrementVehicle As String
Select Case mySender.Name
Case "carButton"
incrementVehicle = "Car"
Case "bicycleButton"
incrementVehicle = "Bicycle"
Case "lorryButton"
incrementVehicle = "Lorry"
Case Else
incrementVehicle = ""
End Select
fTrafficSurveyA.incrementCount(incrementVehicle)
Call updateView()
End Sub
Another alternative is to keep the separate event handlers, and use the fact that each event handler can send the correct text to the common method, so you don't need to check the control name to determine the text:
Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
ButtonClickCode("Car")
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
ButtonClickCode("Bicycle")
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
ButtonClickCode("Lorry")
End Sub
Private Sub ButtonClickCode(ByVal incrementVehicle As String)
fTrafficSurveyA.incrementCount(incrementVehicle)
Call updateView()
End Sub
The List Box has three candidates and a record Button. Every time the record button is hit I need it to add those button clicks for each candidate that is selected in the List Box. My code keeps counting all the clicks no matter which candidate I am selecting in the List Box. How can I differentiate between each selected item in the List Box.
Here is an image of how the application should look: http://i.imgur.com/N8zM2.jpg
Public Class Form1
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Dim candidatevotes(2) As Integer
Dim vote
Dim total
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
candListBox.Items.Add("Mark Stone")
candListBox.Items.Add("Sheima Patel")
candListBox.Items.Add("Sam Perez")
candListBox.SelectedIndex = 0
End Sub
Private Sub recordButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordButton.Click
candidatevotes(vote) = candListBox.SelectedIndex
total += candidatevotes(vote)
Dim outfile As IO.StreamWriter
outfile = IO.File.AppendText("voteinfo.txt")
outfile.WriteLine(Convert.ToString(candListBox.SelectedItem))
outfile.Close()
End Sub
Private Sub displayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayButton.Click
Dim infile As IO.StreamReader
If IO.File.Exists("voteinfo.txt") = True Then
infile = IO.File.OpenText("voteinfo.txt")
infile.Close()
End If
markLabel.Text = total.ToString
sheimaLabel.Text = total.ToString
samLabel.Text = total.ToString
End Sub
End Class
candidatevotes(vote) = candListBox.SelectedIndex
total += candidatevotes(vote)
should be
candidatevotes(candListBox.SelectedIndex) += 1
and
markLabel.Text = total.ToString
sheimaLabel.Text = total.ToString
samLabel.Text = total.ToString
should be
markLabel.Text = candidatevotes(0)
sheimaLabel.Text = candidatevotes(1)
samLabel.Text = candidatevotes(2)