Vb.net capture "+" keypress and add the value already in a cell of a datagridview to the new value entered - vb.net

i used to have some code that worked "kinda" ok for a stocktaking application. Where the user can scan a product and then enter the value of how many they have counted. i added the ability to be able to press the "+" key on the numpad and it would add the value that was already in the cell to the value you now entered in. so for example you counted 2 and then later you find more of the same product you now press + 5 and the cell would then show 7.
i had to use this stocktaking application today, and noticed the + function didnt work. when i looked at my code later tonight, there is no code there to handle the + and add the values. and for the life of me i cant remember how i got this working.
this is the code thats remaing so far.
STCellValue is a public integer value that stores the value already in the cell.
Private Sub DataGridView1_CellBeginEdit...
If Not IsDBNull(DataGridView1.Rows(rowindex).Cells("COUNTED").Value) Then
If STAddKeyPressed = True Then STCellValue =
CInt(DataGridView1.Rows(rowindex).Cells("COUNTED").Value)
ElseIf STAddKeyPressed = True Then
STCellValue = 0
End If
STAddKeyPressed is a boolean public value that is changed to true on the keydown event for the datagridview.
Private Sub DataGridView1_KeyDown(....
If e.KeyCode = Keys.Escape Then
'does things for escape key
ElseIf e.KeyCode = Keys.Add And FILE_Advanced_StocktakeMode.Checked = True Then
STAddKeyPressed = True
end if
i then have this code in the datagridview cellbeginedit subroutine.
If Not IsDBNull(DataGridView1.Rows(rowindex).Cells("COUNTED").Value) Then
If STAddKeyPressed = True Then STCellValue =
CInt(DataGridView1.Rows(rowindex).Cells("COUNTED").Value)
ElseIf STAddKeyPressed = True Then
STCellValue = 0
End If
End if
obviously this does nothing with the Value in the cell, nor does it add the two together, i played around with several things, but cant get it work.
i would have thought that in:
Private Sub DataGridView1_CellValueChanged
there would have been the code to handle the addition of the two values. but its not there.
at the moment if I press the + key on the numpad then a number, the cell will only record the number it will ignore the + and wont even leave it in the cell. so you don't end up with "+5" in the cell just "5". not sure how that's occurring to be honest.
like I said I did have this working in the past, but no idea why the code is now missing :/ I remember that it didn't always 100% of the time work the way it was intended but i didn't mind it was rare the issue would arise. I know im probably not going about the right way in handeling the key.down and events so if you can recommend a better way then im all ears!

Related

Variable not updating in vb.net

I am having a problem getting a global variable to update in VB.NET. The declared value is 0, but the variable changes according to a choice that the user makes.
I have multiple forms, and I have tried updating the variable on 2 different forms with the same result.
I declared the variable like so:
Public Shared creativity As Integer = 0
Public Shared comboBox = frmStart.cbxCombo.SelectedItem
To change the value I used:
If comboBox = "Yes" Then
creativity += 30
End If
I expect the value of creativity to be 30, but it is still showing as 0. I have even tried:
creativity = creativity + 30
but I am still getting the same result, with the value displaying as 0.
Where is the function being called in the code?
For something like
public sub comboBox_change()
If comboBox = "Yes" Then
creativity += 30
End If
End sub
The function call will occur whenever the combo box is change, which you could end up with a stacking issue (multiple calls of the same function, all increment it each time it is changed).
If you have it in a function that is not called, then of course it is not being incremented, as the code is never executed, like if you made a
public sub ThisIsMyCode()
...
End sub
Without a call reference.
you should put this code
If comboBox = "Yes" Then
creativity += 30
End If
inside the SelectedItemChanged event of your combobox
Also, you should put this:
Public Shared comboBox = frmStart.cbxCombo.SelectedValue
Hope this helps

Making a form that users enter a value into text boxes a certain number of times

I have a series of text boxes that are hidden, except one which users enter a value into. After a button is pressed, this text box disappears and the rest appear. From there, I want users to be able to enter different values into each text box. These values will be saved as variables. From there, users press a button and enter new values into the text boxes, until values have been entered into the text boxes for each number up to the first value entered into the hidden text box.
I'm not sure how else to explain this, but please let me know if you have any questions.
Correct me if im wrong. i think you want an input in different textbox?
Like textbox1 = 1, textbox2 = 2 and so on. If this is right then try this.
Lets try this to your 3 textbox for an example.
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
If TextBox1.Visible = True Then
If Trim(TextBox1.Text) <> "" Then
TextBox1.Visible = False
TextBox2.visible = True
Else
MsgBox("Required")
'or user error provider.
End If
Exit Sub
End If
If textbox2.visible = True Then
If Trim(TextBox2.Text) <> "" Then
TextBox2.Visible = False
TextBox3.visible = True
Else
MsgBox("Required")
'or user error provider.
End If
Exit Sub
End If
'and so on
End Sub
hope this is what you want. just basic but maybe usefull :)
This is the workflow of what you want to accomplish in JavaScript. VB.Net has similar functions to Visibility. https://msdn.microsoft.com/en-us/library/ms748821(v=vs.85).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
In the visual editor you can create buttons and add Click events to them, this is basic so I won't tell you how to do it. Google if you can't.
var options = document.getElementsByClassName("options");
var optionsSetOne = document.getByClassName('optionsSetOne');
This will select all your options. Now that you have all your options selected and in a variable called options you can play around with the visibility, as the user progresses thought the buttons.
First I would set all my options to hidden with:
options.style.visibility = 'hidden';
After the users progresses and clicks the button, I will make the first set of the options visible. With:
$('#buttonOne').click(function(){
optionsSetOne.style.visibility = 'visible';
});
This is the basic workflow of what you want to accomplish, you have to figure out how to save all the information the user gives to you.

DataGridView properties not being set when called from Form Load

I'm having a really strange issue with the DataGridView control in a VS2008 / .NET 3.5 winforms project. I have a simple form with a grid. In the form constructor I call a function to bind the grind to a DataTable, and then loop through the rows setting the background colour of the last cell to LightGrey and the cell itself to read-only if the column value is true. After the form finishes loading the code didn't work i.e. the cells are not set to LightGrey and are not read-only (even though when I step through the code I can see the properties being set). I then call the function again from a button, but this time the colour is changed to LightGrey and the cell is made read-only i.e. the code works.
CODE:
Dim dgr As DataGridViewRow
For i_DsRow As Integer = 0 To ds.Tables(0).Rows.Count - 1
dr = ds.Tables(0).Rows(i_DsRow)
For i_row As Integer = 0 To DgSearch.Rows.Count - 1
dgr = DgSearch.Rows(i_row)
If dr("DsColoumn1").ToString.ToUpper = dgr.Cells("DgColoumn1").Value.ToString.ToUpper Then
If Val(dr("Coloumn2").ToString) = 3 Then
dgr.Cells("SomeColomname").Value = dr("SomeColoumName2").ToString
If dgr.Cells("SomeColomname3").Value.ToString <> "" Then dgr.Cells("SomeColomname3").Value &= ", "
dgr.Cells("SomeColomname3").Value &= dr("SomeColoumName2").ToString
SetCellColor(dgr.Cells("SomeColomname"), dgr.Cells("SomeColomname3"))
End If
Exit For
End If
Next
Next
Private Sub SetCellColor(ByVal resultCell As DataGridViewCell, ByVal ColorCell As DataGridViewCell)
If resultCell.Value.ToString().ToUpper = "A".ToUpper Or resultCell.Value.ToString().ToUpper = "B".ToUpper Then
ColorCell.Style.BackColor = Color.FromName("Red")
ElseIf resultCell.Value.ToString().ToUpper = "C".ToUpper Or resultCell.Value.ToString().ToUpper = "D".ToUpper Then
ColorCell.Style.BackColor = Color.FromName("MediumSeaGreen")
Else
ColorCell.Style.BackColor = Color.FromName("Yellow")
End If
End Sub
Thanks for adding the code. I would suggest putting all formatting in the CellFormatting event of the DataGridView.
There are a couple of gotchas that you have to watch, which looking at your code shouldn't be a problem but to be aware of:
This runs for each cell in the grid. Therefore Database lookups are a really bad idea.
You do best to get values where possible from the eventargs parameter of the method
If you need a code sample, I believe there are some good c# examples (possibly vb to) this one for example.

Null Value for datagridview cell vb.net

How to get for null value in datagridview i did this in the cell validation event but it dosent seem to work. I want the user to add a new row and someone force him to give an ID or delete the row. What am i doing wrong. This is not a how to do question. This is a what is wrong Question. So right now it detects null but once i corrected the cell it still dosent allow me to get out of the row.
If DataGrid.CurrentCell.ColumnIndex = 0 Then
If IsDBNull(DataGrid.CurrentCell.Value) Then
Msgbox("Cannot be Null")
e.cancel = true
ElseIf Not IsDBNull(DataGrid.CurrentCell.Value) Then
e.cancel = False
End If
End If
So i tried this and it works for me . it simply uses e.formatedvalue. Current Cell Value is the cell value before and after edit while formatedvalue is what is being typed it . i guess i understand now so here is the coding
If grdDataGrid.CurrentCell.ColumnIndex = 2 Then
If e.FormattedValue = "" Or IsDBNull(e.FormattedValue) Then
MsgBox("Cannot be Null")
e.Cancel = True
Exit Sub
End If
End If
They are also different ways like adjusting your column properties to not allowed null but since the column properties inherit from the database i decided to use this.
I think this should be works ...
If DataGrid.CurrentCell.ColumnIndex = 0 Then
If IsDBNull(DataGrid.CurrentCell.Value) Then
Msgbox("Cannot be Null")
e.cancel = true
exit sub
End If
End If
Another way around this would be to explicitly set the default null value for the new datagrid cell to the value you are checking for. For instance,set the null= to an empty string if you are pulling string values. You can use the properties palette to set this.

SetFocus inside a GotFocus procedure initiated by another SetFocus

Objective: Redirect focus from one command button to another using the first's GotFocus procedure.
Context: I have a form-independent procedure in a generic module that, on most forms, sets focus to the NewRecord button after saving the previous record. But on one form, I would like to redirect (based on certain conditions) focus back to the SignRecord button so the user can "sign" a second part of the same record (I may need this for other uses in the future). The target control is enabled and visible and can otherwise be focused and the original control can be focused when the redirect doesn't occur. Reference [2] below implies that this should be possible, though I'm not changing visibility of my controls.
Issue: When the conditions are met to redirect focus in the GotFocus procedure, it redirects as desired but the original (test) SetFocus call throws a "Run-time error '2110', Can't move focus to the control CommandNew".
What I've tried:
Exit Sub after my downstream SetFocus calls.
Call CommandSign.SetFocus in the hopes that it would make it happen outside the previous SetFocus process.
In a module,
Public Sub test()
Forms("TargetForm").CommandNew.SetFocus 'This gets the error '2110'
End Sub
In the 'TargetForm',
Private Sub CommandNew_GotFocus()
If IsNull(textDateTime) Then Exit Sub 'Works as expected
'I can see these two parts work. The framSign value changes
'and CommandSign gets focus
If checPPC And IsNull(textSigID_PPC) And framSign = 2 Then
framSign = 1
CommandSign.SetFocus
ElseIf checDAS And IsNull(textSigID_DAS) And framSign = 1 Then
framSign = 2
CommandSign.SetFocus
End If
End Sub
References:
[1]: SelectNextControl() a bad idea in a GotFocus event?
[2]: http://www.access-programmers.co.uk/forums/showthread.php?t=100071
I think your problem is that the call to Forms("TargetForm").CommandNew.SetFocus doesn't quite seem to, in fact, finish setting the focus to CommandNew until after Private Sub CommandNew_GotFocus() has finished executing. Because you've called another SetFocus before the first SetFocus could finish, there is a conflict that Access seems to be unable to cope with.
Whether or not that is the case, one thing is clear: the way you have your execution plan set up right now is unfortunately not going to work. You might try adding either a global variable or a public variable to each form that determines whether or not you should set your focus to CommandSign after you set the focus to CommandNew.
Ex. TargetForm:
Public boolSetCommandSignFocusInstead As Boolean
Private Sub CommandNew_GotFocus()
If IsNull(textDateTime) Then Exit Sub 'Works as expected
'I can see these two parts work. The framSign value changes
'and CommandSign gets focus
If checPPC And IsNull(textSigID_PPC) And framSign = 2 Then
framSign = 1
boolSetCommandSignFocusInstead = True
ElseIf checDAS And IsNull(textSigID_DAS) And framSign = 1 Then
framSign = 2
boolSetCommandSignFocusInstead = True
Else
boolSetCommandSignFocusInstead = False
End If
End Sub
Module:
Public Sub test()
Forms("TargetForm").CommandNew.SetFocus
If Forms("TargetForm").boolSetCommandSignFocusInstead Then
Forms("TargetForm").CommandSign.SetFocus
End If
End Sub