How to properly use .SelectedIndex property for listbox using arrays - vb.net

Public Module Inv
Public Item(4) as String
End Module
Private Sub Inventory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Inventory.SelectedIndexChanged
If Inventory.SelectedIndex.ToString(Item(0)) Then
MessageBox.Show("Item Selected!")
playerDMG *= 3
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs)
Item(0) = "Plasma"
for add = 0 to 0
inventory.items.add(item(add))
End Sub
What I want is that if I click that button it adds Item(0) to the listbox, and if I click on the item in the listbox it will triple playerDMG.
Problem here is that it is telling me I can't convert "Plasma Gun" to type 'Boolean'
What's going wrong here? Is there a better way to do this?

I think you would need to do something like this:
If Inventory.SelectedItem.ToString() = Item(0) Then ...
Alternatively, you could use SelectedValue I guess, but in order for that to work you'd need to use DataSource property of combobox.

Related

VB.NET: Add\remove row indexes of datagridview checkboxes rows

I am working with forms in VB.NET
There is a DatagridView table with a checkbox column.
See the picture below:
I am interested in the question: how to add the line index to the list when clicking in the checkbox (when we activate the checked status), and remove it from the list when we uncheck the checkbox?
Tried the following but this is not the correct solution:
If e.ColumnIndex = chk_column.Index Then
If e.RowIndex >= 0 Then
Try
For Each row As DataGridViewRow In dataGridNames.Rows
Dim cell As DataGridViewCheckBoxCell = TryCast(row.Cells(5), DataGridViewCheckBoxCell)
If cell.Value Is cell.FalseValue Then
bList_indexes.Add(DataGridnames.CurrentCell.RowIndex)
Exit For
Else 'If cell.Value Is cell.TrueValue Then
bList_indexes.RemoveAt(DataGridnames.CurrentCell.RowIndex)
End If
Next
Catch ex As Exception
'Show the exception's message.
'MessageBox.Show(ex.Message)
'Throw New Exception("Something happened.")
End try
End If
End If
Using DataSources allows you to take the logic out of mucking around in DataGridView events. You shouldn't perform [much] business logic on the UI anyways.
Here is the class I used to represent your data.
Public Class ClassWithSelect
Public Property [Select] As Boolean
Public Property Name As String
Public Sub New(s As Boolean, n As String)
Me.Select = s
Me.Name = n
End Sub
End Class
And all the code to set DataSources
Private myDataSource As List(Of ClassWithSelect)
Private selectedIndices As List(Of Integer)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
myDataSource = Enumerable.Range(65, 10).Select(Function(i) New ClassWithSelect(False, Chr(i).ToString())).ToList()
DataGridView1.DataSource = myDataSource
updateSelectedIndices()
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
updateSelectedIndices()
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
Private Sub updateSelectedIndices()
selectedIndices = New List(Of Integer)()
For i = 0 To myDataSource.Count - 1
If myDataSource(i).Select Then selectedIndices.Add(i)
Next
ListBox1.DataSource = selectedIndices
End Sub
And the end result
Now you don't need to access the UI to get the indices for further processing as they are in the class-level variable selectedIndices. The UI is meant for user I/O, NOT for storing state.
Note: The event handler was taken from this answer but this answer is also linked as an improvement to the check change handler, but I felt the complexity would distract from my answer. If you find you need to click fast, look into the latter.
Also Note: The method updateSelectedIndices() should have inside it an InvokeRequired check if you plan to perform work off the UI thread

How to make Datagridview Editable and Change it to Number Format?

Good Morning.
I have a Datagridview in a Form and it is connected in Database and it looks like this.
I have no problem with this part.
My question here is like this. How can I make the Fourth Column Editable? I mean I can edit it by clicking this property
and now the output will be like this.
Now here is the real question, I will ask a question based on the flow that my system will do.
1.The 4th Column are the column that will become editable and the rest will be locked or uneditable
2.Lets say I will put 48 how can I make it 48.00 when i leave the cell? That kind of format with .00 at the end.
3.Unable to input Letters on the 4th column.
TYSM for future help
Set the ReadOnly property to True for any column that you don't
want the user to be able to edit.
Set the DefaultCellStyle.Format property of the column to "n2" or "f2".
I'd probably advise against that because the fact that you want to include a decimal point makes it more complex. If you're determined to go ahead then you should research how to allow only numeric input in a regular TextBox, because this will work exactly the same way. You simply need to access the TextBox control used for editing via the appropriate event handlers of the grid.
In the example below, because the editingTextBox field is declared WithEvents, the last method will handle the TextChanged event for the editing control while it's assigned to that field for the duration of the editing session.
Private WithEvents editingTextBox As TextBox
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Me.editingTextBox = DirectCast(e.Control, TextBox)
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Me.editingTextBox = Nothing
End Sub
Private Sub editingTextBox_TextChanged(sender As Object, e As EventArgs) Handles editingTextBox.TextChanged
'...
End Sub
Set the other columns to readonly = true and your numeric column = false and set the defaultcellstyle.format of your numeric column to "###,##0.00" and in your datagridview's cellvalidating event, do the ff:
Private Sub DatagridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DatagridView1.CellValidating
Try
If DatagridView1.IsCurrentCellDirty Then
Select Case DatagridView1.Columns(e.ColumnIndex).Name.ToUpper
Case "<NAME OF YOUR NUMERIC COLUMN>"
If Not IsNumeric(e.FormattedValue) Then
MsgBox("Invalid value.")
e.Cancel = True
Exit Sub
End If
If CType(e.FormattedValue, Integer) < 0 Then
MsgBox("Invalid value.")
e.Cancel = True
Exit Sub
End If
End Select
End If
Catch ex As Exception
ErrMsg(ex)
End Try
End Sub
Private Sub dgvwithdraw_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvwithdraw.CellClick
Select Case dgvwithdraw.Columns(e.ColumnIndex).Name
Case "Select", "Alloted"
dgvwithdraw.ReadOnly = False
Case Else
dgvwithdraw.ReadOnly = True
End Select
End Sub

Dynamically add Items to Combobox VB.NET

I have 2 combobox in windows application. I want to programmatically add Items to second combobox based on what is selected in the first combobox.
This is what I do in the selected index change of the combobox:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
If ComboBox1.SelectedIndex = 0 Then
ComboBox2.Items.Add("MM")
ComboBox2.Items.Add("PP")
ElseIf ComboBox1.SelectedIndex = 1 Then
ComboBox2.Items.Add("SMS")
ElseIf ComboBox1.SelectedIndex = 2 Then
ComboBox2.Items.Add("MMS")
ComboBox2.Items.Add("SSSS")
End If
Catch ex As Exception
End Try
End Sub
It works fine, however, if I keep selecting different items it's keep adding the value over and over. I would like to add those values only once.
Also, when I add an item I would prefer to add an ID with the item description. I tried:
ComboBox2.Items.Add("SSSS", "1")
It seems that it's not working.
Any suggestions?
try this
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
If ComboBox1.SelectedIndex = 0 Then
If Not (ComboBox2.Items.Contains("MM")) And Not (ComboBox2.Items.Contains("PP")) Then
ComboBox2.Items.Add("MM")
ComboBox2.Items.Add("PP")
End If
ElseIf ComboBox1.SelectedIndex = 1 Then
If Not (ComboBox2.Items.Contains("SMS")) Then
ComboBox2.Items.Add("SMS")
End If
ElseIf ComboBox1.SelectedIndex = 2 Then
If Not (ComboBox2.Items.Contains("MMS")) And Not (ComboBox2.Items.Contains("SSSS")) Then
ComboBox2.Items.Add("MMS")
ComboBox2.Items.Add("SSSS")
End If
End If
Catch ex As Exception
End Try
Regarding:
"Also, when I add an item I would prefer to add an ID with the item
description"
You can use the AddRange function of the ComboBox or make a list as I show here and use it as a datasource - as #Plutonix mentioned in his comment.
With it you can add an array of objects of a type that holds a value and a description, given that you override the ToString base function (of Object class).
Here is such a class:
Public Class CodeAndDescription
Public code As String
Public description As String
Public Overrides Function ToString() As String
Return Me.code + " - " + description
End Function
End Class
Now make a list of that upper class and add it to the combobox. Something like:
Dim lstItems As List(Of CodeAndDescription) = GetList()
yourComboBox.Items.Clear()
yourComboBox.Items.AddRange(lstItems.ToArray)
Don't forget to Cast the retrieved object when you take it out of the combo:
Dim codeDesc As CodeAndDescription = TryCast(yourComboBox.Items(0), CodeAndDescription)
I've done this on a check list, but I think the principle is the same for a ComboBox.

Creating arrays of controls in visual basic

Ok so here's the relevant code:
Public Shared compSelect(9) As ComboBox
Public Shared compPercent(9) As TextBox
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Data.LoadComponents("C:/Users/Jon/Documents/Visual Studio 2013/Projects/QuickBlend/QuickBlend/QuickBlend/Resources/databaseText.txt")
MsgBox("finished loading")
MainForm.compSelect = {CompSelect1, CompSelect2, CompSelect3, CompSelect4, CompSelect5, CompSelect6, CompSelect7, CompSelect8, CompSelect9, CompSelect10}
MainForm.compPercent = {CompPercent1, CompPercent2, CompPercent3, CompPercent4, CompPercent5, CompPercent6, CompPercent7, CompPercent8, CompPercent9, CompPercent10}
For Each box As ComboBox In MainForm.compSelect
box.DataSource = Data.Components
box.DisplayMember = "Name"
For Each comp As String In Data.ComponentNames
box.Items.Add(comp)
Next
MsgBox("looped")
Next
MsgBox("finished loop")
End Sub
As you can see, I've placed various MsgBoxes to see exactly whats going on. It never displays the "looped" message box. Can anybody explain to me why it's completely skipping the for loop? Been working on this for a while and got fed up with it. Thanks in advance for the help! =)
MainForm.compSelect should be Me.compSelect since this is the instance(has been filled with comboboxes) and not just the fully qualified name of the object that has not been filled.
Your problem is that you are setting the datasource for the comboBox, and then trying to add items to it. .NET does not like this, and will just exit the Sub that tries to do this, without warning (unless you have exception handling added in). Your code should be...
Public Shared compSelect(9) As ComboBox
Public Shared compPercent(9) As TextBox
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Data.LoadComponents("C:/Users/Jon/Documents/Visual Studio 2013/Projects/QuickBlend/QuickBlend/QuickBlend/Resources/databaseText.txt")
MsgBox("finished loading")
MainForm.compSelect = {CompSelect1, CompSelect2, CompSelect3, CompSelect4, CompSelect5, CompSelect6, CompSelect7, CompSelect8, CompSelect9, CompSelect10}
MainForm.compPercent = {CompPercent1, CompPercent2, CompPercent3, CompPercent4, CompPercent5, CompPercent6, CompPercent7, CompPercent8, CompPercent9, CompPercent10}
For Each box As ComboBox In MainForm.compSelect
box.DataSource = Data.Components
box.DisplayMember = "Name"
'take this stuff out, it is not needed
'For Each comp As String In Data.ComponentNames
'box.Items.Add(comp)
'Next
MsgBox("looped")
Next
MsgBox("finished loop")
End Sub

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