To get the commandargument in rowdatabound event in gridview - vb.net

Following is code for buttonclick in gridview.
Protected Sub lnkChangeStatus_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim gvr As GridViewRow = TryCast(DirectCast(sender, LinkButton).Parent.Parent, GridViewRow)
Dim lngProfileId As Long = Convert.ToInt64(gvwBusinessProfiles.DataKeys(gvr.RowIndex).Value)
End Sub
I want to get commandargument of lnkChangeStatus in its click.Is there any way

I guess the syntax for the eventhandler (Command event as against Click) should be as below.
Protected Sub lnkChangeStatus_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
.....
End Sub
You can use e.CommandName or e.CommandArgument inside the code block.
Refer this

Related

Event fired twice if call a form

Can anyone help avoid this ?
At an event handler, I need cal a form but after the form is unloaded the event is fired again.
Private Sub MyHandler(sender As System.Object, e As System.EventArgs) Handles txObjName.Leave
Dim MyVar As Integer = SomeValue
dim myForm as SomeForm
MyForm.ShowDialog()
After myForm be closed, the event is fired again
A quick thought is to try make use of the FormClosing event.
So your code will look like this:
Private _Closing as boolean = False
Private Sub MyHandler(sender As System.Object, e As System.EventArgs) Handles txObjName.Leave
If Not _Closing Then
Dim MyVar As Integer = SomeValue
Dim myForm as SomeForm
MyForm.ShowDialog()
End If
End Sub
Private Sub FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
_Closing = True
End Sub
And you make use of the FormClosing event to toggle the boolean _Closing to true, so the code will not execute when the form is closed :)
Solved changing :
Handles txObjName.Leave
by
Handles txObjName.LostFocus
Thank you all

Trigger an event with one sub for multiple controls

I have 10 panels on my form, and when you hover them, their color changes. I have 10 private subs like so...
Private Sub pnl2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnl2.MouseHover
pnl2.BackColor = Color.WhiteSmoke
End Sub
This code is repeated for each panel with the only difference being it's name, how can I do this more efficiently? as it is very repetitive.
Add them at the handler statement appending each by a comma. The sender object is the panel in question so cast it to change it's properties.
Private Sub pnl2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnl2.MouseHover, pnl3.MouseHover 'etc
Dim pnl As Panel = CType(sender, Panel)
pnl.BackColor = Color.WhiteSmoke
End Sub

how to find footer's value of gridview on RowCommand event?

I want to find footer's value on the event of RowCommand in vb.net??
I'm assigning footer's value run-time on RowCreated.
e.Row.Cells(2).Text = FOOTERVALUE
e.Row.Cells(1).Text = "Total Subjects"
And this footer is for Parent Grid. I'm using nested gridview.
Protected Sub Gridview1RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview1.RowCommand
Dim Lblcolval = CType(dgrd_WWWH.FooterRow.FindControl("yourcolumn") ,Label)
End Sub
Protected Sub Gridview1_How_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
If e.Row.RowType And DataControlRowType.Footer Or (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim ddlresp As DropDownList = CType(e.Row.FindControl("ddlResp"), DropDownList)
SrchWhoBind(ddlresp)
ddlresp.SelectedValue = Convert.ToInt32(Session("Uname"))
End If
End Sub
I got the solution, just needed to write the code in rowDataBound instead of rowCreated.

Second form not showing value stored in first form when called

Hey all i am trying to figure out why my 2nd form is not displaying the value i recived in my first form.
The code for the first form is:
Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
responceBack = scannerOnCom.ReadLine
Call frm1110.clickButton(responceBack)
End Sub
The second form code is this:
Public Sub clickButton(ByRef theResponse As String)
txtNumber.Text = theResponse
'Call cmdNextFinish_Click(Nothing, Nothing)
End Sub
However, when i debug it to make sure there is something stored for theResponse, there is but for some reason it does not put it into the textbox. It's blank.
Any help would be great!
David
UPDATE
Ok so Form1:
Dim tmpForm3020 As New frm3020
Private Sub cmd3020_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3020.Click
tmpForm3020.Show()
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
responceBack = scannerOnCom.ReadLine
tmpForm3020.txtNumber.Text = responceBack
End Sub
If thats correct then i get an error on line:
xForm.txtNumber.Text = responceBack
Saying:
Cross-thread operation not valid: Control 'txtNumber' accessed from a thread other than the thread it was created on.
Are you explicitly creating an instance of your second form, or relying on the default instance? I.e. is "frm1110" the second form's class name, or an instance that you have new'd up? Make sure in either case that it is the same instance that is actually being displayed.
Dim tmpForm3020 As New frm3020
Private Sub cmd3020_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3020.Click
tmpForm3020.Show()
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub scannerOnCom_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
responceBack = scannerOnCom.ReadLine
TestData(responceBack)
End Sub
Private Sub TestData(ByVal xVal As String)
If InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf TestData))
' change Me to tmpForm3020 (if it does not work)
' tmpForm3020.Invoke(New MethodInvoker(AddressOf TestData))
Else
tmpForm3020.txtNumber.Text = xVal
End If
End Sub

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub