I am using this code to add a value to a combobox different then the one displayed: how to add value to combobox item
Lets suppose i have 3 values in my Combobox:
item 1
item 2
item 3
If i chose item 2 from the dropdown the code in the link works. But if i TYPE item 2 manually it doesnt work because i think that typing it only sets the combobox1.text value and not the combobox1.selecteditem. I can type a value present in the dropdown, or one not present. If i type one that is present, then the selectedItem property should also be set to the proper value. can this be done?
Thanks
solved this way:
Private Sub ComboBox1_Keyup(sender As Object,
e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
ComboBox1.SelectedIndex = ComboBox1.FindStringExact(ComboBox1.Text)
End Sub
Related
I have DataGridView1 that contain n number of column i want user to select the column that he want to save then i save it ..(done)
The problem here that i want the user to select only 3 column and on four column give user massage that he/she have to select only three.
To unselect the last one i use DataGridView1_ColumnHeaderMouseClick Event and I present the message but can not find anything like DataGridView1.SelectColumn = False
If DataGridView1.SelectedColumns.Count > 3 Then
MsgBox("You have to choose 3 columns only")
Exit Sub
End If
I'm not convinced you can count the selected columns that way. Anyway, here's a way to use the args of the event to achieve what I think you're trying to achieve:
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
If DataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Selected) > 3 Then
MsgBox("You cannot select more than 3 columns.")
DirectCast(sender, System.Windows.Forms.DataGridView).Columns(e.ColumnIndex).Selected = False
End If
End Sub
I used a DirectCast to get the sender to behave as the datagridview (which it is) because this way I could attach this event to several different dtagridviews, but if you dislike that form you can always use DataGridView1.Columns(e.ColumnIndex).Selected = False instead.
Also, I use the e.ColumnIndex to unselect the last column the user clicked on because I though that it was what you were trying to do, but you can change this comportment for something more suitable to your needs as you now know how to unselect columns.
Have fun!
I have a CheckedListBox control.I want to limit it's selection property to one means now a user can select more than one item in the control, need to limit this property to single selection only.
For example, Let's CHKListsolutions has following items
Google
Bing
Yahoo
Normally we can select 3 of them because of an obvious reason.
How to make this CHKListsolutions to select only one item in the list.
for example, I select Google and for some reason I want chnage the selection so I will select Yahoo then my last selection should unchecked and new one should be checked
I have checked in the resource for a property but in vain.
Any help would be very much appreciated
Private Sub CHKListsolutions_MouseClick(sender As Object, e As MouseEventArgs) Handles CHKListsolutions.MouseClick
Dim idx, sidx As Integer
sidx = CHKListsolutions.SelectedIndex
For idx = 0 To CHKListsolutions.Items.Count - 1
If idx <> sidx Then
CHKListsolutions.SetItemChecked(idx, False)
Else
CHKListsolutions.SetItemChecked(sidx, True)
End If
Next
End Sub
In MouseClick event you'll get the currently selected index of the item in the control(sidx) use this sidx to loop through number of items in the control and uncheck checked item that is not equal to the current index using SetItemChecked method
Use Radio Button instead of Checkedlistbox
I have two DataGridViews, DataGridView1 is holding information from database, once a user click on an item in the DataGridView1 is should be sent to the DataGridView2, my question is how to specify which information is needed from DataGridView1.
for example DataGridView1 holds thees information :
- ItemName
- ItemDescription
- ItemPrice
in DataGridView2 it has these columns
- Qty (Quantity that is inserted by user)
- ItemName
- ItemPrice
thanks in advance :)
To get the data from the clicked cell/row you can use the cellClick- or rowEnter-event. This example code adds the values 'name' and 'description' from the clicked DGV1-row to the first DGV2-row:
Private Sub DGV1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV1.CellContentClick
DGV2.rows(e.RowIndex).cells(0).value = DGV2.rows(0).cells(1).value 'itemName
DGV2.rows(e.RowIndex).cells(2).value = DGV2.rows(0).cells(2).value 'itemDescription
End Sub
If you want to constantly add a new row to DGV2 you can use the .rowCount-method of the DGV instead of an specific index for the row.
I am trying to get the primary key from a row in a DataGridView so that when a user double-clicks on any cell of the record, the value of the first cell which contains the key is stored in a variable "Key"
I have used the follwing code for this purpose.
Key= DGV1.SelectedRows(0).Cells(0).Value
The table contains columns like Key,First Name, Last Name and so on.
But when i double-click on the record i get the given exception saying that the index is negative or out-of-bounds.
Double clicking doesn't select the row, so SelectedRows doesn't contain anything and SelectedRows(0) gives you the error.
If you want to handle double clicking on the cells, you can use the CellDoubleClick event of the datagridview. You can then use this code:
Private Sub DGV1_CellDoubleClick(sender as Object, e as DataGridViewCellEventArgs) _
Handles DataGridView1.CellDoubleClick
Key= DGV1.Rows(e.RowIndex).Cells(0).Value
End Sub
Could someone please check my code? i don't seem to get the correct answer after the initial run of my code.
i want it would be like.
5-1=4 then if i press the minus button again and i want the new value of my textbox to be subtracted from the difference which in case is 4.
i'm getting the right answer from the 1st process but not when i want to subtract another value from my difference.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If C <> 0 Then
Dif = C - Val(TextBox1.Text)
C = Dif
Label1.Text = Dif
TextBox1.Clear()
End If
C = Val(TextBox1.Text)
TextBox1.Clear()
End Sub
I don't know what you're trying to accomplish exactly, but there are several problems with your code. Unless it's initialized elsewhere, the first time you click Button2 the variable 'C' is zero, so the 'If' instruction is ignored, C gets the value from the textbox and then clears the textbox.
On a second click C has value (assuming that textbox wasn't empty in the first place) and then you substract the content of Textbox1 (assuming it has something) from the value of 'C', and then display it into a label.
Wouldn't it be simpler to assign the contents of TextBox1 and TextBox2 to two variables (checking in the process to see if those contents are valid, i.e. numeric) and then display the result of substracting both variables into Label1?