How to "add" not "overwrite" amount using WriteDMAInteger - vb.net

I'm making a program that changes address values however I want to be able to "add" lets say "500" to the current address. Not to change "1500" to "500". Any ways I can make this happen?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ammount As String = TextBox1.Text
Try
WriteDMAInteger("Process.dat", GetModuleHandle("Process.dat", "Process.dat") + &H3B80F8, {&H790}, ammount, 1, 4)
Catch ex As Exception
End Try
End Sub

Big thanks to #Visual Vincent for recommending or reminding me to use the "ReadDMAInteger" method. Using "ReadDMAInteger" I'm able to read the value of a address and have that be the value of the integer. From there I can simply use the integer and plug and play.
Of course, you would want to have a timer setup using this.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
My.Computer.Audio.Play(My.Resources.Speech_Sleep, AudioPlayMode.Background)
Dim ammount As String = TextBox1.Text
Dim total1 As Integer
Dim totalstring1 As Integer
Dim final1 As Integer
total1 = ReadDMAInteger("Process.dat", GetModuleHandle("Process.dat", "Process.dat") + &H3B80F8, {&H790}, 1)
totalstring1 = TextBox1.Text.Trim
final1 = (total1 + totalstring1)
Try
WriteDMAInteger("Process", GetModuleHandle("Process.dat", "Process.dat") + &H3B80F8, {&H790}, final1, 1, 4)
Catch ex As Exception
End Try
End If

Related

System.NullReferenceException with datagridview

I'm trying to fill two arrays with the content of two columns of a Datagridview. I wrote this:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim t As Integer = (DataGridView1.Rows.Count - 1)
For i = 0 To t
avx(i) = DataGridView1.Rows(i).Cells("Av").Value
hi(i) = DataGridView1.Rows(i).Cells("h").Value
avsum = Val(avsum + avx(i))
Next
Label2.Text = Val(avsum)
End Sub
When I start debugging, I receive this error at the fourth line of the reported code.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Does someone know how to solve this issue? How could I initialise Datagridview1? It has been already initialised in Designer section so, if I try to initialise it again, I receive obviously a conflict.
Based on my test, I can not reproduce your problem.
However, I make a sample code that may be similar to your question.
Form_Load event
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table As New DataTable
table = New DataTable()
table.Columns.Add("Av")
table.Columns.Add("h")
table.Rows.Add("test1", "1001")
table.Rows.Add("test2", "1002")
table.Rows.Add("test3", "1003")
DataGridView1.DataSource = table
DataGridView1.AllowUserToAddRows = False
End Sub
Note: The AllowUserToAddRows property will reduce an extra row.
Button_Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t As Integer = (DataGridView1.Rows.Count - 1)
Dim array1(t) As String
Dim array2(t) As String
For i = 0 To t
array1(i) = DataGridView1.Rows(i).Cells("Av").Value
array2(i) = DataGridView1.Rows(i).Cells("h").Value
Next
For index = 0 To t
RichTextBox1.AppendText(array1(index) + Environment.NewLine)
RichTextBox2.AppendText(array2(index) + Environment.NewLine)
Next
End Sub
Final result:
Can you describe about variable avx() and hi()
This error occur maybe datagridview row count greater than Array bound

VB Entering a Non_Numeric value crashes the program

Please consider adding a description to this question to attract more helpful responses.
Public Class Form1
Private Sub BtnCalculateRevenue_Click(sender As Object, e As EventArgs) Handles BtnCalculateRevenue.Click
Dim intvalue As Integer = CInt(TxtInputClassA.Text)
Dim intvalue2 As Integer = CInt(TxtInputClassB.Text)
Dim intvalue3 As Integer = CInt(TxtinputClassC.Text)
Dim total As Double
Try
LblStatus.Text = String.Empty
LblClassAResult.Text = (intvalue * 15).ToString("c")
LblClassBResult.Text = (intvalue2 * 12).ToString("c")
LblClassCResult.Text = (intvalue3 * 9).ToString("c")
total = CDbl((intvalue * 15) + (intvalue2 * 12) + (intvalue3 * 9))
LblTotal.Text = total.ToString("c")
Catch
LblStatus.Text = "Please Enter a Number"
End Try
End Sub
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
TxtInputClassA.Clear()
TxtInputClassB.Clear()
TxtinputClassC.Clear()
LblClassAResult.Text = String.Empty
LblClassBResult.Text = String.Empty
LblClassCResult.Text = String.Empty
LblTotal.Text = String.Empty
End Sub
End Class
VB Entering a Non_Numeric value crashes the program
Validation is built right into Windows Forms so you should make use of it. If you want to force the user to enter numbers in each of three TextBoxes then you can do this:
Private Sub TextBoxes_Validating(sender As Object, e As ComponentModel.CancelEventArgs) Handles TextBox3.Validating, TextBox2.Validating, TextBox1.Validating
Dim tb = DirectCast(sender, TextBox)
If Not Integer.TryParse(tb.Text, Nothing) Then
'Select all the invalid text and highlight it.
tb.SelectAll()
tb.HideSelection = False
MessageBox.Show("Please enter a whole number",
"Invalid Input",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
'Remove highlight when TextBox is not focused.
tb.HideSelection = True
'Don't let the control lose focus while data is invalid.
e.Cancel = True
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ValidateChildren() Then
'All data is valid so proceed.
Dim n1 = CInt(TextBox1.Text)
Dim n2 = CInt(TextBox2.Text)
Dim n3 = CInt(TextBox3.Text)
'...
End If
End Sub
The ValidateChildren method will raise the Validating event for every control on the form and return False if any fail validation, i.e. e.Cancel is set to True in any event handlers, so that ensures that even controls that never received focus will be validated before the data is used.
Its bombing out on your cast "CInt(*value*)" so you can fix the code a couple ways. You can move your try above the casts like...
Try
Dim intvalue As Integer = CInt(TxtInputClassA.Text)
Dim intvalue2 As Integer = CInt(TxtInputClassB.Text)
Dim intvalue3 As Integer = CInt(TxtinputClassC.Text)
Dim total As Double
LblStatus.Text = String.Empty
You can do a data validation on your inputs and exit if they aren't all numeric (put this above your Dim intvalue code)
For Each value As String In {TxtInputClassA.Text, TxtInputClassA.Text, TxtInputClassA.Text}
If Not IsNumeric(TxtInputClassA.Text) Then
LblStatus.Text = "Please Enter a Number"
Exit Sub
End If
Next
Instead of casting as an int, use the tryparse method on Int32...
Dim intvalue As Integer
If Not Int32.TryParse(TxtInputClassA.Text, intvalue) Then
Exit Sub
End If
Or you could intercept the keypresses on each text box so that only numbers can be entered
Private Sub TxtInputClassA_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtInputClassA.KeyPress
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub
You can make that routine universal and append the event handler for all three text boxes like...
Private Sub EnforceOnlyNumericKeyPresses(sender As Object, e As KeyPressEventArgs) Handles TxtInputClassA.KeyPress, TxtInputClassB.KeyPress, TxtInputClassC.KeyPress
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub
Choose your favorite or do all of them, lots of choices.
Replace your textboxes with NumericUpDown controls and retrieve their .Value for your calculation. NUDs don't allow non numeric input and can have a fixed number of decimal places which may also be useful in a financial context

How to compare textbox value to txt file

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim linenumber0 As Integer
linenumber0 = 0
Dim mass As Double
mass = (File.ReadAllLines("225.txt").ElementAt(linenumber0).ToString)
If (Math.Abs((cDbl(TextBox1.Text) - mass < 0.5) Then
TextBox1.BackColor = Color.Green
End If
Im getting an error conversing from string to double is not valid. It is probably a simple solution but i cant see it right now
Your error occurs because the data read from the file is a String, however you are attempting to assign it to a variable declared as Double.
You can use TryParse to convert the String to Double, avoid errors and provide appropriate feedback.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim lineNumber0 As Integer
Dim mass As Double
Dim input As Double
If Double.TryParse(File.ReadAllLines("225.txt").ElementAt(linenumber0), mass) Then
If Double.TryParse(TextBox1.Text, input) AndAlso Math.Abs(input - mass) < 0.5 Then
TextBox1.BackColor = Color.Green
End If
Else
'Bad file input
End If
'...
End Sub
I think that when you set the value to mass is catching a String, so parse it with a simple CDbl, like this
mass = cDbl(File.ReadAllLines("225.txt").ElementAt(linenumber0).ToString)
I supose that with this fix it will works.
Just in case, surround it with a TRY CATCH in case what it reads is not valid

Add a completely different line with every button click

Complete noob to vb.net (and programming in general) here, all I really want is every time I click a button, the number in the textbox is added by 1 but the new number shows up on the next line. Tried to google this a hundred times but nothing really helped.
I don't want to use loops as I don't want all numbers to show up at once, only for the added number to show up after clicking a specific button (on a new line).
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtoutput As String = ""
Dim a As Integer = 1
txtoutput &= "the value of a =" & a & Environment.NewLine
a = a + 1
TextBox1.Text = txtoutput
End Sub
You are replacing the Text, you want to append a new line, so you need to do:
Private a As Int32 = 0
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a += 1
Dim newLine = $"the value of a = {a}"
TextBox1.Text = TextBox1.Text & Environment.NewLine & newLine
End Sub
You also have to use a field and not a local variable if you want to retain the old value and increment it. Otherwise it is reset always to it's inital value.
Please try to change dim a to static a
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtoutput As String = ""
Static a As Integer = 1
txtoutput &= "the value of a =" & a & Environment.NewLine
a = a + 1
TextBox1.Text = txtoutput
End Sub

why e.Keychar is not working in keypress event?

I have a problem regarding the KeyChar e.KeyChar. It is not working. I want to get the value of e from, please have a look and your comments are highly appreciated.
Private Sub txtTax_KeyPress(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtTax.KeyPress
If (Strings.Asc(e.KeyChar) > 0) Then
Try
If (Me.conn.State = ConnectionState.Closed) Then
Me.conn.Open()
End If
Dim adapter As New SqlDataAdapter(("Select * from TaxGroup where TaxName='" & Me.txtTax.Text & "'"), Me.conn)
Dim dataSet As New DataSet
adapter.Fill(dataSet, "TaxGroup")
Me.dgrdTax.DataSource = dataSet.Tables.Item(0)
Me.dgrdTax.Visible = True
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
Interaction.MsgBox(exception.Message, MsgBoxStyle.ApplicationModal, Nothing)
ProjectData.ClearProjectError()
End Try
End If
End Sub
Your event declaration is wrong. The correct signature is:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs)
Handles TextBox1.KeyPress
KeyChar is not a part of EventArgs, which is largely just a base class.
I am guessing maybe your thought you needed to type it in yourself. Just pick the control from the left drop down and the event from the right and the IDE will create it for you. I am not sure how that could happen otherwise.
You can also try this:
If Not (Asc(e.KeyChar) = 8) Then
Dim allowedChars As String = "1234567890" ' allowed In textbox
If Not allowedChars.Contains(e.KeyChar.ToString.ToLower) Then
e.KeyChar = ChrW(0)
e.Handled = True
End If
End If