Get selected row of inactive datagridview - vb.net

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

Related

Open different forms on different button click in VB.NET

A noob query I have, is there any way to use a single command to open different forms on different button click events. I have 24 buttons in one form and will use these buttons to open 24 different forms.
So instead of doing it for 24 times as:
Private Sub BtnCh1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh1.Click
FormCh1.Show()
End Sub
Private Sub BtnCh2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh2.Click
FormCh2.Show()
End Sub
Private Sub BtnCh3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh3.Click
FormCh3.Show()
End Sub
Private Sub BtnCh4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh4.Click
FormCh4.Show()
End Sub
Can it be done with a single command?
In your form's load event add the forms in a List(Of Form)
Private list As List(Of Form)
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
list = New List(Of Form)
list.Add(New Form1())
'
'
'
list.Add(New Form24())
End Sub
Set your button's Tag property with the form's index and set them all to use the same click event:
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
list(CType(sender, Button).Tag).Show()
End Sub
Attach all the handlers to your method and then branch behaviour based on the Select Case:
Private Sub Button_Click_Handler(sender As Object, e As EventArgs) Handles Button66.Click, Button67.Click, Button68.Click
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case Button66.Name
Dim f1 As New Form1
f1.Show()
Case Button67.Name
Dim f2 As New Form2
f2.Show()
Case Button68.Name
Dim f3 As New Form3
f3.Show()
End Select
End Sub

Assign a value?

I have this
Public Shared Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim c As Client
Dim Armor As Item = c.Inventory.GetItemInSlot(SlotNumber.Armor)
End Sub
And when I click the button, is crashes and "System.NullReferenceException" pops up , I've searched around and found out that it happens because c is used before it is assigned a value so I'd like to know, what would be the proper way to assign it a value?
try this:
instead of:
Public Shared Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim c As New Client
Dim Armor As Item = c.Inventory.GetItemInSlot(SlotNumber.Armor)
End Sub
try:
Public Shared Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim c As New Client.Inventory
Dim Armor As Item = c.Inventory.GetItemInSlot(SlotNumber.Armor)
End Sub

How to add items from a MenuStrip event to a ListBox using a For Next Loop

That might have sounded weird so let me explain.
I have a school assignment that has me pulling my hair out. I have to get a collection of 5 facts and have them display to a ListBox using a For Next Loop. The user would use an InputBox to input the facts.
I dont know what to put in the For Next to fetch the string from the InputBox. I'm at my wits end and am falling behind.
Here is what I have so far
Public Class frmWWIIFacts
Private Property RemoveAt As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub AddFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFactToolStripMenuItem.Click
Dim intFact As Integer
Dim strInputFact As String
strInputFact = InputBox("Do you want to add a fact?", "Add a fact")
For
Next
strInputFact = InputBox("Do you want to add a fact?", "Add a fact")
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub ClearListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearListToolStripMenuItem.Click
lstFacts.Items.Clear()
End Sub
Private Sub RemoveFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveFactToolStripMenuItem.Click
End Sub
I've submitted a reddit post requesting some assistance but its gotten me nowhere. https://www.reddit.com/r/learnprogramming/comments/3t614u/vb2015_using_menustrip_to_addremove_items_in_a/
I would love some help on this. Please ask questions if your confused on my method or if you need to know more.
Sounds like you're trying to do this:
Private Sub AddFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFactToolStripMenuItem.Click
Dim intFact As Integer
Dim strInputFact As String
lstFacts.Items.Clear()
For intFact = 1 To 5
strInputFact = InputBox("Please enter a fact:", "Add a fact")
If Not strInputFact = "" Then
lstFacts.Items.Add(strInputFact)
End If
Next
End Sub

Call function on button click event

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

Adding Multiple Items From One Listbox to Another VB.Net

I am able to only move single items from one listbox to another with this code. I tried with both MultiSimple & MultiExtended SelectionMode.
How do I select multiple items and then move them?
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
Dim i As Integer = Listbox1.SelectedIndex
If i = -1 Then
Exit Sub 'skip if no item is selected
End If
Listbox2.Items.Add(Listbox1.Items(i))
Listbox1.Items.RemoveAt(i)
End Sub
You need to use SelectedIndices or SelectedItems.
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()
For Each selectedItem In selectedItems
Listbox2.Items.Add(selectedItem)
Listbox1.Items.Remove(selectedItem)
Next
End Sub
Note the use of a Linq query to get list of selected items as an array. Using an array is required to prevent "Collection changed" exceptions. You may need to add a reference to System.Linq.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("SanDiego")
ComboBox1.Items.Add("BeverlyHills")
ComboBox1.Items.Add("Florida")
ComboBox1.Items.Add("NewYork")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String
s = ComboBox1.SelectedItem
ListBox1.Items.Add(s)
ComboBox1.Items.Remove(s)
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
For Each selectedItem In (From i In ListBox1.SelectedItems).Tolist()
Listbox2.Items.Add(selectedItem)
Listbox1.Items.Remove(selectedItem)
Next
End Sub