Cancel EnterCell event of spread farpoint - vb.net

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

Related

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)

Listview Checkbox - 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.

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!

DataGridView - ReadOnly Cell KeyDown Event

i have a datagridview with a readonly cell, i would like to show a formdialog window when the user press the space key. but is not possible since the cell is readonly=true.
i'v been using the following code with the EditingControlShowing event. and when the cell is readonly=false it works sometimes.
Private Sub sub_fecha_keydown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Space Then
Dim frm As New frmFecha
frm.fecha_inicial = Me.m_dtp_id_fecha.Fecha
Dim res As DialogResult = frm.ShowDialog()
If res = Windows.Forms.DialogResult.OK Then
Me.m_dgv_detalle.Rows(Me.m_dgv_detalle.CurrentRow.Index).Cells("m_dgv_dtm_documento").Value = frm.fecha_format
Else
Me.m_dgv_detalle.Rows(Me.m_dgv_detalle.CurrentRow.Index).Cells("m_dgv_dtm_documento").Value = ""
End If
End If
End Sub
i would like to keep the cell readonly=true.
is there any other way to do it?
thanks very much for your time and help.
Rather than trying to intercept a keystroke for a readonly cell, which may not be possible, why not add a button column next to the field and when it is pressed, perform your actions.
This way you don't have to worry about whether the cell is readonly or not and it will be easier for your users to understand how the form is to be accessed.
Here is a link to the MSDN documentation on the DataGridViewButtonColumn.

Help with events

I have multiple textboxes which I want them to perform the same thing upon clicking them. By default I can use the handles textbox1.click for 1 single textbox as shown below but I am not sure how to do handle multiples of them. Of course I can write a handler for every single textbox but I have about 50 of them. I am sure there must be a more efficient way. Please advice. Thanks.
Sub TextBox1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
If Button9.Text = "Make Changes" Then
If TextBox2.Text <> "" Then
Frm_Cine1.Show()
Frm_Cine1.chooseCine(ComboBox1.SelectedItem)
Else
MsgBox("Please check input!")
Exit Sub
End If
End If
End Sub
why don't you create a customizable textbox?
If Button9.Text = "Make Changes" Then
If TextBox2.Text <> "" Then
These two lines are going to be same for all those 50 buttons?
If yes, then I think you can assign same event handler to each of the button's click event.
Other way is, create one private method which takes one string as an argument and returns boolean value depending on whether your string is blank or not and call this method from all the 50 button's click event.
Thanks for all your advices, I am not sure if this is what you guys are suggesting but apparently it is how I wanted it to work:
Sub TextBoxs_click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TextBox2.Click, TextBox3.Click, TextBox4.Click 'This part is disturbing if I have 50 textboxes...
'For Each obj As Control In Panel2.Controls
If sender.GetType.ToString = "System.Windows.Forms.TextBox" Then
Dim txtbox As TextBox = sender
textbox_verification(txtbox)
End If
'Next
End Sub
Sub textbox_verification(ByVal txtbox As TextBox)
If Button9.Text = "Make Changes" Then
If txtbox.Text <> "" Then
Frm_Cine1.Show()
Frm_Cine1.chooseCine(ComboBox1.SelectedItem, "FILE1-->This should be a variable")
Else
MsgBox("Please check timings input!")
Exit Sub
End If
End If
End Sub
If you indeed need to use the same click handler for multiple test boxes, you can use the AddHandler command to associate the click event of each test box with the handler routine, as shown:
AddHandler TextBoxX.Click AddressOf TextBox1_Click
You will need to add this statement to your program (maybe in the form load routine) once for each text box you want to handle. (Using the name of each text box in place of the "TextBoxX" in the above code.)