Listview Checkbox - vb.net - vb.net

Ok, i have a listview with checkboxes and a button, how it works is that i have to check the items i want to change the values, then press the button to change the value of those checked item, here's my code on the button.
Try
Dim I As Integer
If lv_id.CheckedItems.Count = 0 Then
For I = 0 To lv_id.Items.Count - 1
lv_id.Items(I).SubItems(1).Text = "Pending"
Next
Else
For I = 0 To lv_id.CheckedItems.Count - 1
lv_id.CheckedItems(I).SubItems(1).Text = "Submitted"
Next
End If
Proc_Items.BackColor = Color.Green
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Now, what i want to do is, to remove the button and then when i'll check the item i want to the code above will do the process without pressing the button, i tried "ItemCheck, ItemChecked" event, but with no luck.

You should be able to use the ItemChecked event for this. With the ItemCheckedEventArgs giving you everything you need.
This example would set up to toggle the 3rd column depending on the checkbox state
Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
If e.Item.Checked Then
e.Item.SubItems(2).Text = "Submitted"
Else
e.Item.SubItems(2).Text = "Pending"
End If
End Sub

If I understood you right, you want to run your code whenever a ckecbox in your ListView gets checked or unchecked. This can be done using
Public Class Form1
'The ListView_SelectedIndexChanged event triggers when a checkbox of the listview gets checked or unchecked
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
'Put your code here, access all checked items with "ListBox1.CheckedItems"
End Sub
End Class
This worked for me, and I think it works for you as well. If not, please tell me in the comments what went wrong.

Related

Cancel EnterCell event of spread farpoint

My program use spread farpoint as a 3rd-party control, it has a TextBox control and a Spread control for showing data. When user change active cell in spread, I want to validate that the TextBox must not empty. If the TextBox is empty, EnterCell event must be cancel and the TextBox must got focus, also active cell of spread must not change. I'm stuck here.
Currently I performed validation in LeaveCell event but it doesn't work. EnterCell event fired and spread still changed the active cell :(
If TextBox1.Text = String.Empty Then
MsgBox ("TextBox1 cannot blank")
TextBox1.Select()
'TextBox1.Focus() 'I have tried this function but still not working
End If
Please support me!
Thanks all.
As far as my knowledge, there's nothing we can do to "cancel" EnterCell event. However, I found out that we could do a little trick to achieve it.
Private Sub spread_LeaveCell(sender as Object, e as LeaveCellEventArgs) Handles sprSheet.LeaveCell
If TextBox1.Text = String.Empty Then
MsgBox ("TextBox1 cannot blank")
'This is the trick
e.NewColumn = e.Column
e.NewRow = e.Row
Exit Sub
End If
'Other leave cell proceses..
End Sub
...
Private Sub spread_EnterCell(sender as Object, e as EnterCellEventArgs) Handles sprSheet.EnterCell
If TextBox1.Text = String.Empty Then
TextBox1.Select()
End If
Exit Sub

Disable Button If Combox Input Deleted

In my project, I have a few textbox inputs, and some combo boxes with maybe 2 indexed items on a form. There's a button I'm disabling on load if no input is supplied to both textbox inputs, and it works great even if I delete out any text. However, I'm having issues with forcing the combo box to behave in the same manner. This work however:
Private Sub cboPickShirts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPickShirts.SelectedIndexChanged
InputCheck_3 = True
If cboPickShirts.SelectedIndex < 0 Then
InputCheck_3 = False
End If
If InputCheck_3 = False Then
btnInputResult.Enabled = False
ElseIf InputCheck_3 = True Then
btnInputResult.Enabled = True
End If
End Sub
I have InputCheck_3 set up as a global variable in a Public Module. On form load, I'm disabling my button and it doesn't enable until I select one of the indexed items. My struggle to get the button disable again if any combo box text is entered and deleted out, leaving it null or empty. Any thoughts on what I'm missing or what I can add to get results? I guess I need a variable or event to notice the change (entering & deletion of text).
The problem you are having is that your SelectedIndexChanged event is not being triggered when you remove the selected item from your ComboBox. I would use the TextChanged event of your TextBox's and ComboBox and give it a common handler and check it that way. Something like this
Private Sub TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, cboPickShirts.TextChanged
EnableCheck()
End Sub
Private Sub EnableCheck()
btnInputResult.Enabled = (String.IsNullOrEmpty(TextBox1.Text) And String.IsNullOrEmpty(TextBox2.Text) And ComboBox1.SelectedIndex = -1)
End Sub
You can also check that the comboBox is NullorEmpty the same way as the textbox's. As it stands right now the combobox will be enabled when the text no longer matches a selection.
One line code
btnInputResult.Enabled = If((cboPickShirts.SelectedIndex<0),False, True)

DataGridView.rows.Cells.Style.BackColor doesn't work with MdiParent in form load event

It really drives me crazy, I have a form, I am calling a public sub called "timerss" in the form load event, when i run my form the sub "timerss" works perfectly, but when i add "Me.MdiParent = MDIParent1" in load event the sub "timerss" doesn't work!! i am really confused here!! any idea please.
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MdiParent = MDIParent1
timerss()
End Sub
update1:\ check the print screen of the result when running my form with and without setting MdiParent!
update2: I managed to fix part of the problem which is i got the data but what i want now is to set the color of the time cells with red, as i said the sub don't want to work, the timerss sub is:
For m As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(m).Cells(4).Value > DataGridView1.Rows(m).Cells(9).Value Then
DataGridView1.Rows(m).Cells(4).Style.BackColor = Color.Red
MsgBox("red")
End If
Next
as u c in the above code i put a msgbox just to make sure that the code is working, so when i run my form the msgbox appears, but the backcolor function doesn't work when i set the MdiParent.
I don't know why the behavior is different without MDIParent, but you could try calling the sub from the DataBindingComplete event.
I had a similar problem and solved it using the event.

Hide a row in DataGridView

I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I have googled this but have yet to find a way to do it.
At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
'Get the text of the item that was clicked on.
'Dim text As String = txtCustomerActive.Text
Try
'txtCustomerActive.Visible = False
pnlContextMenuStrip1.Visible = False
MessageBox.Show(txtCustomerActive.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You could use Rows.Item() to hide specific DataGridViewRow, like:
If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If
I am assuming you are using FullRowSelect here.
If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:
If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If
To Unhide everything let's say from a Button Click you could have this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub
As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).
However, I believe that such feature could be done with some client-side javascript progamming.
Hope this helps!

Edit Update DatagridView VB.Net (No Database)

Good day everyone.
I need your help in this project I am into (a Visual Basic program with no database.) It just contains a Datagridview, a Textbox, and three buttons (an "Add" Button, a "Edit" and an "Update" Button).
1 . Is there any way (like using "for loop") to automatically assign DataGridView1.Item("item location") to the one edited and be updated?
2 . Or is it possible to just click an item in the Datagridview then it will be edited at that without passing it to a Textbox, and to be updated at that.
The DataGridViewCellEventArgs variable (e in the method stub the designer will generate for you) of the double click event of the cell has RowIndex and ColumnIndex properties which refer to the position of the cell you clicked.
Save those (in a class variable possibly or a local one if that's all you need) and then refer to them when you update the cell in your DataGridView, possibly like this MyDataGridView.Item(e.ColumnIndex, e.RowIndex) or MyDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex) where e is the variable from the double click event handler.
For you cell double click event you could have something like this:
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Using myEditor As New frmCellEditor(Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value)
If myEditor.ShowDialog() = DialogResult.OK Then
Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = myEditor.NewCellValue
End If
End Using
End Sub
This will call a new instance of your editor and get a value from you. For the purpose of this demo I have made a form like this:
Public Class frmCellEditor
Public NewCellValue As Integer
Public Sub New(ByVal CurrentCellValue As Object)
InitializeComponent()
Me.TextBox1.Text = CStr(CurrentCellValue)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.NewCellValue = CInt(Me.TextBox1.Text)
Me.DialogResult = DialogResult.OK
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Call Me.Close()
End Sub
End Class
Which just has two buttons (Button1 = OK, Button2 = Cancel). When you click OK, it just returns the value 1 which then gets set as the value of the cell.
This is a VERY simplistic example, but it should provide you the basics of what you are trying to do.
UPDATE:
I updated the code for the editor interface so it will include handling for passing the value back and forth from the form with your datagridview.
In your project, make a new form called frmCellEditor. This forms has to have two buttons and a textbox (Make sure that the programmatic names match!). Replace the code with the code listed above. You will have to add Imports System.Windows.Forms above the class as well.
Amend the event handler for the cell double click event of your datagrid to pass the cell value when frmCellEditor is constructed (the line going ... New frmCellEditor(...).
How many columns does your DataGridView has?
Based on how you populate your DataGridView, I'll assume only 1.
Declare this on top of your form
Dim i as Integer
On your btnUpdate_Click Event (Just combine your Edit and Update button into One)
SELECT CASE btnUpdate.Text
Case "Update"
With DataGridView1
'Check if there is a selected row
If .SelectedRows.Count = 0 Then
Msgbox "No Row Selected for Update"
Exit Sub
End If
i = .CurrentRow.Index 'Remember the Row Position
Textbox1.Text = .item(0 ,i).value 'Pass the Value to the textbox
.Enabled = False 'Disable DataGridView to prevent users from clicking other row while updating.
btnUpdate.Text = "Save"
End With
Case Else 'Save
DatagridView1.Item(0,i).Value = Textbox1.Text
btnUpdate.Text = "Update"
END SELECT
Thanks for those who contributed to finding answers for this thread. I have not used your solutions for now (maybe some other time). After some research, I've found an answer for problem 2 (more user friendly at that):
2 . Or is it possible to just click an item in the Datagridview then
it will be edited at that without passing it to a Textbox, and to be
updated at that.
Here's what i did:
in Private Sub Form1_Load, just add:
yourDataGridView.EditMode = DataGridViewEditMode.EditOnEnter
in Private Sub yourDataGridView_(whatever event here: DoubleCellClick, CellContentClick, etc.) add:
DataGridView1(e.ColumnIndex, e.RowIndex).[ReadOnly] = False
DataGridView1.BeginEdit(False)