Subtraction using 1 textbox visualbasic - vb.net

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?

Related

Button press not capturing correct Row status from DataGridView: how to reset or capture status?

Code works except for a bug: using the 'Modify' button, it gets the selected Row in a DataGridView and sends that data to ComboBoxes on a different Form if the left Column is clicked before the first column with data.
The issue arises if the user selects a cell in the DataGridView or doesn't select at all, which I have msgbox telling them to select row. When a Row is selected after that msgbox and the 'Modify' button is pressed again DataGridView1.SelectedRows.Count does not seem to be picking it up and it still comes up blank.
Odd thing, 20% of the time it seems to work. How do I set it so that it checks the status and make sure it picks up the new selection?
Below is the code:
Private Sub Modify_BTN_Click(sender As Object, e As EventArgs) Handles Modify_BTN.Click
If (DataGridView1.SelectedRows.Count > 0) Then
Dim newMod As New Mod_Form
newMod.ChangeType = DataGridView1.CurrentRow.Cells(8).Value.ToString
newMod.PartNum = DataGridView1.CurrentRow.Cells(9).Value.ToString
newMod.PriorityT = DataGridView1.CurrentRow.Cells(10).Value.ToString
newMod.DESC = DataGridView1.CurrentRow.Cells(11).Value.ToString
newMod.Show()
Else
MsgBox("You did not select the row for the request from the data below, please select the row you want to modify.", MsgBoxStyle.Information)
End If
End Sub

DataGridView select only certain number of column vb.net

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!

DataGridView flickering issue [duplicate]

I am using vb.net and I have data coming into my DGV and I have a column labeled deployed if it's a '1', I want to have all the rows with '1' in the deployed column RED and if it's a '0', I want all the rows to be GREEN. This is my method I have, right now the column is the 10th column but it doesn't like the = operator. Even when I use quotes on the 1 with equals compare operator for strings. Should be an integer but I was trying every way I could to see why it's not working.
Private Sub LaptopGrid_CellFormatting(ByVal Sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles LaptopGrid.CellFormatting
For i As Integer = 0 To LaptopGrid.Rows.Count - 1
If LaptopGrid.Rows(i).Cells(9).Value = 1 Then
LaptopGrid.RowsDefaultCellStyle.BackColor = Color.Green
End If
Next
End Sub
There are a couple of issues with your code.
First, you are handling the CellFormatting event but you are iterating every row to set the backcolor. That event is meant for you to do something to a single, specific cell, the one in question is indicated in the event args: e.RowIndex and e.ColumnIndex. Using a loop, you are acting on many more rows than needed and doing so over and over.
Second, VB has data types. Int32 is one type, String is another, and Object is yet another. You need to convert one type to the other type before you compare. LaptopGrid.Rows(r).Cells(c).Value returns Object (since a cell can literally hold anything), so to compare to 1 you need to convert it to integer.
Finally, you may not want the CellFormatting event for this. If the cell in question is not on screen, the event wont fire (perhaps the user resized the columns). RowPrePaint on the other hand will fire when the row scrolls into view.
Private Sub dgv1_RowPrePaint(sender As Object,
e As DataGridViewRowPrePaintEventArgs) Handles dgv1.RowPrePaint
' dont do the NewRow
If e.RowIndex < 0 OrElse dgv1.Rows(e.RowIndex).IsNewRow Then Return
' convert to int32, then compare
' act on just this row - e.RowIndex
If Convert.ToInt32(dgv1.Rows(e.RowIndex).Cells(3).Value) > 3 Then
dgv1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LemonChiffon
Else
dgv1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.MistyRose
End If
End Sub
If the user can edit that cell value, you will want to update the back color accordingly.

I was trying to Change the row color in DataGridView based on the quantity of a cell value but first row doesn't change [duplicate]

I am using vb.net and I have data coming into my DGV and I have a column labeled deployed if it's a '1', I want to have all the rows with '1' in the deployed column RED and if it's a '0', I want all the rows to be GREEN. This is my method I have, right now the column is the 10th column but it doesn't like the = operator. Even when I use quotes on the 1 with equals compare operator for strings. Should be an integer but I was trying every way I could to see why it's not working.
Private Sub LaptopGrid_CellFormatting(ByVal Sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles LaptopGrid.CellFormatting
For i As Integer = 0 To LaptopGrid.Rows.Count - 1
If LaptopGrid.Rows(i).Cells(9).Value = 1 Then
LaptopGrid.RowsDefaultCellStyle.BackColor = Color.Green
End If
Next
End Sub
There are a couple of issues with your code.
First, you are handling the CellFormatting event but you are iterating every row to set the backcolor. That event is meant for you to do something to a single, specific cell, the one in question is indicated in the event args: e.RowIndex and e.ColumnIndex. Using a loop, you are acting on many more rows than needed and doing so over and over.
Second, VB has data types. Int32 is one type, String is another, and Object is yet another. You need to convert one type to the other type before you compare. LaptopGrid.Rows(r).Cells(c).Value returns Object (since a cell can literally hold anything), so to compare to 1 you need to convert it to integer.
Finally, you may not want the CellFormatting event for this. If the cell in question is not on screen, the event wont fire (perhaps the user resized the columns). RowPrePaint on the other hand will fire when the row scrolls into view.
Private Sub dgv1_RowPrePaint(sender As Object,
e As DataGridViewRowPrePaintEventArgs) Handles dgv1.RowPrePaint
' dont do the NewRow
If e.RowIndex < 0 OrElse dgv1.Rows(e.RowIndex).IsNewRow Then Return
' convert to int32, then compare
' act on just this row - e.RowIndex
If Convert.ToInt32(dgv1.Rows(e.RowIndex).Cells(3).Value) > 3 Then
dgv1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LemonChiffon
Else
dgv1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.MistyRose
End If
End Sub
If the user can edit that cell value, you will want to update the back color accordingly.

Datagridview show date or time (VB.NET)

I have a datagridview in which some columns where initially specified as String and they contain date or time, depending on some bussiness logic.
I am changing the type of the column (to datetime) to allow correct sorting. And I would like to know if it is possible to show date values or time values depending on the row. Or I will always have to show both.
Thank you.
You can handle the CellFormatting event. This event is fired everytime a cell is painted. You can change what value is being displayed without changing the underlying value and the format of the cell.
By doing this you can change the format depending on the row and column
Private Sub CellFormatting_EventHandler(sender As Object, e As DataGridViewCellFormattingEventArgs)
If e.ColumnIndex = 2 AndAlso CType(sender, DataGridView)(5, e.RowIndex).Value.ToString = "Steve" Then 'Or whatever logic you like
e.Value = CType(e.Value, Date).ToShortTimeString
End If
End Sub