Visual Studio 2010 String.Format exception - visual-studio-2010

Im trying to insert column to a listbox using format.string but it seems to be not working, "Input string was not in correct format" it says. I have ran out of ideas to solve this problem, can't really find any solution on google. So I decided to ask here, btw here's my code
Public Class Form1
Dim stddetails As String = "{0, -20}{1, -20}{2, -20}{3, -20}(4, -20}{5, -20}{6, -20}"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lstoutput.Items.Add(String.Format(stddetails, "Customer ID", "Nama Depan", "Nama Belakang", "Pemesanan", "Tanggal Pemesanan", "Pembayaran", "Total"))
cmbpemesanan.Items.Add("Pilih Cara Pemesanan")
cmbpemesanan.Items.Add("Telepon")
cmbpemesanan.Items.Add("Ditempat")
cmbpemesanan.Items.Add("Pemesanan Online")
cmbpembayaran.Items.Add("Pilih Pembayaran")
cmbpembayaran.Items.Add("Cash")
cmbpembayaran.Items.Add("Master Card")
cmbpembayaran.Items.Add("Visa Card")
cmbpembayaran.Items.Add("Debit Langsung")
lstmerek.Items.Add("Indonesian")
lstmerek.Items.Add("Japanese")
lstmerek.Items.Add("Middle East")
lstmerek.Items.Add("European")
seafood.Checked = False
traditional.Checked = False
drinks.Checked = False
fullpack.Checked = False
rbsatu.Checked = False
rbdua.Checked = False
rbempat.Checked = False
rbdelapan.Checked = False
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkeluar.Click
Dim ikeluar As DialogResult
ikeluar = MessageBox.Show("Keluar dari menu?", "Food Order Menu", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If ikeluar = DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhapus.Click
seafood.Checked = False
traditional.Checked = False
drinks.Checked = False
fullpack.Checked = False
rbsatu.Checked = False
rbdua.Checked = False
rbempat.Checked = False
rbdelapan.Checked = False
txtid.Clear()
txtnamabelakang.Clear()
txtnamadepan.Clear()
cmbpembayaran.Text = "Pilih Pembayaran"
cmbpemesanan.Text = "Pilih Cara Pemesanan"
lstoutput.Items.Clear()
lstmerek.SelectedItems.Clear()
lstoutput.Items.Add(String.Format(stddetails, "Customer ID", "Nama Depan", "Nama Belakang", "Pemesanan", "Tanggal Pemesanan", "Pembayaran", "Total"))
End Sub
Public Function getprices(ByVal brand As String, ByVal items As List(Of String), ByVal quantity As Integer) As Double
Dim prices = {
{"Indonesian", 2.5, 2, 0.95},
{"Japanese", 2.3, 1.5, 2.5},
{"Middle East", 3.5, 5, 4.9},
{"European", 3.8, 2.7, 1.5}
}
Dim sum As Double = 0
For i As Integer = 0 To 4
If (brand = prices(i, 0)) Then
For j As Integer = 0 To items.Count - 1
If (items(j) = "Seafood") Then
sum = sum + prices(i, 1)
ElseIf (items(j) = "Traditional") Then
sum = sum + prices(i, 2)
ElseIf (items(j) = "Drinks") Then
sum = sum + prices(i, 3)
End If
Next
sum = sum * quantity
End If
Next
Return sum
End Function
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Dim ID, namadepan, namabelakang, pemesanan, tanggal, pembayaran, price As String
ID = txtid.Text
namadepan = txtnamadepan.Text
namabelakang = txtnamabelakang.Text
pemesanan = cmbpemesanan.Text
tanggal = cmbtanggal.Text
pembayaran = cmbpembayaran.Text
Dim items As New List(Of String)
Dim quantity As Integer
If (seafood.Checked = True) Then
items.Add(seafood.Text)
End If
If (traditional.Checked = True) Then
items.Add(traditional.Text)
End If
If (drinks.Checked = True) Then
items.Add(drinks.Text)
End If
If (fullpack.Checked = True) Then
items.Add(fullpack.Text)
End If
If (rbsatu.Checked = True) Then
quantity = 1
ElseIf (rbdua.Checked = True) Then
quantity = 2
ElseIf (rbempat.Checked = True) Then
quantity = 4
ElseIf (rbdelapan.Checked = True) Then
quantity = 8
End If
If lstmerek.Text = "" Then
MessageBox.Show("Anda harus memilih jenis makanan.", "Jenis Makanan", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf (seafood.Checked = False And traditional.Checked = False And drinks.Checked = False) Then
MessageBox.Show("Anda harus memilih kategori.", "Kategori", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf (rbsatu.Checked = False And rbdua.Checked = False And rbempat.Checked = False And rbdelapan.Checked = False) Then
MessageBox.Show("Anda harus memilih jumlah.", "Jumlah Pesanan", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
price = FormatCurrency(getprices(lstmerek.Text, items, quantity))
lstoutput.Items.Add(String.Format(stddetails, ID, namadepan, namabelakang, pemesanan, tanggal, pembayaran, price))
End If
End Sub
Private Sub btntambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntambah.Click
seafood.Checked = False
traditional.Checked = False
drinks.Checked = False
fullpack.Checked = False
rbsatu.Checked = False
rbdua.Checked = False
rbempat.Checked = False
rbdelapan.Checked = False
txtid.Clear()
txtnamabelakang.Clear()
txtnamadepan.Clear()
cmbpembayaran.Text = "Pilih Pembayaran"
cmbpemesanan.Text = "Pilih Cara Pemesanan"
lstoutput.Items.Clear()
lstmerek.SelectedItems.Clear()
End Sub
End Class
Any help would be much appreciated, thanks.

There's a syntax error on line 2
You used a "(" instead of "{"
Replace
Dim stddetails As String = "{0, -20}{1, -20}{2, -20}{3, -20}(4, -20}{5, -20}{6, -20}"
with
Dim stddetails As String = "{0, -20}{1, -20}{2, -20}{3, -20}{4, -20}{5, -20}{6, -20}"

Related

Need help fixing Exception Unhandled

I am getting the following error:
System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string.
Parameter name: length'
(Look for Bold Italic on code *** that's where it is taking me to fix that)
Not sure what the problem is. Here is the whole code:
Imports GroceryApp.GroceryItem
Imports System.IO
Public Class GroceryItemForm
Private strFileName As String = String.Empty
Private Sub btnAddToBasket_Click(sender As Object, e As EventArgs) Handles btnAddToBasket.Click, AddToolStripMenuItem.Click
Dim gi As GroceryItem
Dim price As Double
' Validate that brand name is entered
If txtBrandName.Text = "" Then
MsgBox("Please input an Brand Name", , "Value Required")
txtBrandName.Focus()
Exit Sub
End If
' Validate that price is entered
If Not Double.TryParse(numPrice.Text, price) Then
MsgBox("Please input an Price", , "Value Required")
numPrice.Focus()
Exit Sub
End If
' Validate that Aisle is selected
If cboAisle.Text = "" Then
MsgBox("Please select an Aisle", , "Value Required")
cboAisle.Focus()
Exit Sub
End If
***txtScanNumber.Text = txtBrandName.Text.Substring(0, 3) & "1019"***
gi = New GroceryItem(txtScanNumber.Text, txtBrandName.Text, price)
gi.Type = [Enum].Parse(GetType(Aisle), cboAisle.Text)
gi.Description = txtDescription.Text
basket.Add(gi)
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub ViewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewToolStripMenuItem.Click
'Dim result As String = ""
'Dim i As Integer = 1
'For Each gi As GroceryItem In basket
'result = result & "Item " & i & vbNewLine & "Aisle: " & gi.Type.ToString & vbNewLine & "Scan Number: " & gi.ScanNumber & vbNewLine & "Brand Name: " & gi.BrandName & vbNewLine & vbNewLine
'i = i + 1
'Next
'MsgBox(result, , "Basket Details")
Dim oForm As BasketDisplayForm
oForm = New BasketDisplayForm()
oForm.Show()
oForm = Nothing
End Sub
Private Sub GroceryItemForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oForm As LoginForm
oForm = New LoginForm()
oForm.Show()
oForm = Nothing
End Sub
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
Dim rowLine As String = ""
'If strFileName = String.Empty Then
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFileName = SaveFileDialog1.FileName
Dim fsStream As New FileStream(strFileName, FileMode.Append, FileAccess.Write)
Dim sw As New StreamWriter(fsStream)
Dim sb As New System.Text.StringBuilder
For Each Item As GroceryItem In basket
sb.AppendLine(String.Concat(Item.ScanNumber, ",", Item.Type.ToString, ",", Item.BrandName, ",", Item.Description, ",", Item.Price))
'rowLine = rowLine + Item.ScanNumber + "," + Item.Type.ToString + "," + Item.BrandName + "," + Item.Description + "," + Item.Price.ToString
Next
'IO.File.WriteAllText(strFileName, sb.ToString)
'rowLine = rowLine.Remove(rowLine.Length - 1, 1)
sw.WriteLine(sb)
sw.Flush()
MsgBox("Data Saved Successfully")
sw.Close()
basket.Clear()
End If
'End If
End Sub
Private Sub LoadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadToolStripMenuItem.Click
Dim basketFile As StreamReader
Dim gi As GroceryItem
Dim sNo, brand, desc, aisle As String
Dim price As Double
Dim y As Integer
basket.Clear()
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFileName = OpenFileDialog1.FileName
basketFile = File.OpenText(strFileName)
' Read Power rating from file
Using sr As StreamReader = New StreamReader(strFileName)
Do While sr.Peek() > -1
For Each c As String In sr.ReadToEnd().Split(CType(Chr(10), Char))
sNo = ""
For Each d As String In c.Split(",")
If y = 0 Then
sNo = d
End If
If y = 1 Then
aisle = d
End If
If y = 2 Then
brand = d
End If
If y = 3 Then
desc = d
End If
If y = 4 Then
price = d
End If
y += 1
Next
If (sNo <> "") Then
gi = New GroceryItem(sNo, brand, price)
gi.Type = [Enum].Parse(GetType(Aisle), aisle)
gi.Description = desc
If (sNo <> "" & vbCr & "") Then
basket.Add(gi)
End If
End If
y = 0
Next
Loop
End Using
basketFile.Close()
End If
End Sub
End Class

Wont insert data to Access Database, keep stuck on Open Connection

I create a Learning Aplication for English kindergarten, and I got stuck on the editing form for insert data about sing a song...
Here's my program view:
Editing Learning
Translate
Tambah = Add
Ubah = Edit
Simpan = Save
Batal = Cancel
Hapus = Delete
Cari = Find
Now the problem is, when i click SIMPAN (SAVE), nothing happened, i try click SAVE again, show up an error like this
The Connection wasn't closed
Here's the coding:
Public Class frmeditlearning
Dim baru, valid As String
Dim id1, judul1, video1, databaru As String
Dim cmd1 As New OleDbCommand(databaru, mycon)
Private Sub btntambahsing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntambahsing.Click
'This is button for ADD when get clicked
txtjudul.Enabled = True
txtvideo.Enabled = True
txtid.Clear()
txtid.Select()
btnsingcarivideo.Enabled = True
btnsimpansing.Enabled = True
btnbatalsing.Enabled = True
btnubahsing.Enabled = False
btnhapussing.Enabled = False
baru = True
btntambahsing.Enabled = False
End Sub
Private Sub btnsimpansing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsimpansing.Click
If txtid.Text = "" Or txtjudul.Text = "" Or txtvideo.Text = "" Then
MessageBox.Show("Empty textbox is not allowed!", "EMPTY TEXTBOX", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
If baru = True Then
mycon.Open()
Dim noid, str As String
str = "Select id from sing where (id = '" & txtid.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, mycon)
dr = cmd.ExecuteReader
While dr.Read
noid = dr("id").ToString
If txtid.Text = noid Then
mycon.Close()
MessageBox.Show("SAME ID CANNOT USED!", "DUPLICATE ID", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
Else
If Not (txtid.Text = noid) Then
mycon.Open()
id1 = txtid.Text
judul1 = txtjudul.Text
video1 = txtvideo.Text
databaru = "INSERT INTO sing (id,judul,video) values(#id,#judul,#video)"
cmd1.Parameters.AddWithValue("#id", id1)
cmd1.Parameters.AddWithValue("#judul", judul1)
cmd1.Parameters.AddWithValue("#video", video1)
cmd1.ExecuteNonQuery()
mycon.Close()
btnbatalsing.PerformClick()
End If
End If
End While
End If
End Sub
Thanks in advance, sorry if my English is bad.

Acces denied to registry on windows 8 for vb.net app

I created a small app in vb.net for a friend. The problem is that on my PC (W7 64bits) it works great (registry reading / writing) but on my friends PC (W8 64bits) it doesn't work.
It it strange but at the moment he runs my app (it checks the registry on loading) it gives him a "Access to registry denied" error. I tried to run it with admin rights but it doesn't work either.
Here is my code:
Imports Microsoft.Win32
Public Class Form1
Dim planned As Date
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
End Sub
Private Sub planifier_Click(sender As Object, e As EventArgs) Handles planifier_button.Click
Dim toExecute As String = String.Format("shutdown -s -f -t {0}", 100)
Dim currentDate = System.DateTime.Now
Dim futureDate = DateTimePicker1.Value
Dim difference = futureDate - currentDate
Console.Out.WriteLine(difference)
System.Diagnostics.Process.Start("shutdown", "-s -f -t " & Math.Round(difference.TotalSeconds))
Me.planned = futureDate
saveStgFile(futureDate)
setup()
End Sub
Private Sub Shutdown_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Console.Out.WriteLine(Application.ProductName & " & " & Application.ExecutablePath)
getStgFile()
getPlanned()
setup()
Dim baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
Startup.Checked = baseKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).GetValue(Application.ProductName).Equals(Application.ExecutablePath)
End Sub
Private Sub cancel_button_Click(sender As Object, e As EventArgs) Handles cancel_button.Click
System.Diagnostics.Process.Start("shutdown", "-a")
Me.planned = Nothing
saveStgFile(Nothing)
setup()
End Sub
Sub setup()
If Me.planned.Equals(Nothing) Then
cancel_button.Enabled = False
planifier_button.Enabled = True
planifier_button.Text = "Planifier"
Else
cancel_button.Enabled = True
planifier_button.Enabled = False
planifier_button.Text = Me.planned.TimeOfDay.ToString()
End If
End Sub
Function getPlanned() As TimeSpan
Dim stg As String = getStgFile()
Dim fileContent As String = My.Computer.FileSystem.ReadAllText(stg)
Dim timespan As Date
If fileContent = "none" Then
Me.planned = Nothing
ElseIf Date.TryParse(fileContent, timespan) Then
Me.planned = timespan
End If
Return (Me.planned - System.DateTime.Now)
End Function
Function getStgFile() As String
Dim stgFile = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "AutoShutdown")
If My.Computer.FileSystem.DirectoryExists(stgFile) = False Then
FileSystem.MkDir(stgFile)
End If
stgFile = System.IO.Path.Combine(stgFile, "settings.ini")
If My.Computer.FileSystem.FileExists(stgFile) = False Then
My.Computer.FileSystem.WriteAllText(stgFile, "none", False)
End If
Return stgFile
End Function
Sub saveStgFile(planned As Date)
Dim stgFile As String = getStgFile()
Try
If (planned.Equals(Nothing)) Then
My.Computer.FileSystem.WriteAllText(stgFile, "none", False)
Else
My.Computer.FileSystem.WriteAllText(stgFile, planned.ToString(), False)
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub Startup_CheckedChanged(sender As Object, e As EventArgs) Handles Startup.CheckedChanged
Dim baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
If Startup.Checked Then
baseKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
Console.Out.WriteLine("Add to startup")
Else
baseKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)
Console.Out.WriteLine("Remove from startup")
End If
End Sub
End Class

I want to send data and receive data as string if possible or at least as integer

I am trying to send and receive data through mscomm port, but am receiving datatype mismatch.so what should I do to avoid this error, I need to send data as integer and receive it as string if possible ,minimum I should get it as integer datatype. The following is my code, please help me to solve this problem.
The send and receive code is working when run independently.
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
If (MSComm1.PortOpen = False) Then
MSComm1.PortOpen = True
End If
Command1.Enabled = False
Command2.Enabled = True
Text3.Text = "COM1, Baud - 9600, Databit - 8, Parity - None, Stopbit - 1....CONNECTED." & Text3.Text
End Sub
Private Sub Command2_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
Command1.Enabled = True
Command2.Enabled = False
Text3.Text = "DISCONNECTED" & Text3.Text
End Sub
Private Sub Command3_Click()
Text1.Text = " "
Text2.Text = " "
Text3.Text = "CLEARED" & Text3.Text
End Sub
Private Sub Command4_Click()
MSComm1.Output = Text2.Text
Text3.Text = "SENDING" & Text3.Text
End Sub
Private Sub Command5_Click()
Text3.Text = " "
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.DTREnable = True
MSComm1.Handshaking = comRTS
MSComm1.InBufferSize = 2
MSComm1.RThreshold = MSComm1.InBufferSize
MSComm1.RTSEnable = True
MSComm1.InputLen = 2
MSComm1.InputMode = comInputModeText
MSComm1.NullDiscard = True
MSComm1.OutBufferSize = 2
MSComm1.SThreshold = MSComm1.OutBufferSize
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As String
Select Case MSComm1.CommEvent
Case comEvReceive
'Text1.Text = " "
Buffer = Cstr(MSComm1.Input)
Text1.Text = Buffer
End Select
End Sub
I think the main problem is that you are sending data asynchroniously without any attempt to transmission control. Since there are multiple characters to be transmitted you need to tell the receiver where a new transmission start to let it find the right offset.
In your case you say you only want to send values in the range 0..100. By using a single byte values in the range 0 to 255 can be addressed so sending a single byte per value to send is sufficient in that case and for a single character no transnission control will be necessary.
Here an example where the number is converted to a single byte, send and reconverted to a number:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub cmdOpen_Click()
If (MSComm1.PortOpen = False) Then
MSComm1.PortOpen = True
End If
cmdOpen.Enabled = False
cmdClose.Enabled = True
txtState.Text = "COM1, Baud - 9600, Databit - 8, Parity - None, Stopbit - 1....CONNECTED." & txtState.Text
End Sub
Private Sub cmdClose_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
cmdOpen.Enabled = True
cmdClose.Enabled = False
txtState.Text = "DISCONNECTED" & txtState.Text
End Sub
Private Sub cmdClear_Click()
txtReceived.Text = " "
txtSend.Text = " "
txtState.Text = "CLEARED" & txtState.Text
End Sub
Private Sub cmdSend_Click()
Dim Number As Byte
'some checking - needed depending on source of data
If Not IsNumeric(txtSend.Text) Then
MsgBox "only numbers (from 0 to 255)"
Exit Sub
End If
If Val(txtSend.Text) < 0 Or Val(txtSend.Text) > 255 Then
MsgBox "out of range (from 0 to 255)"
Exit Sub
End If
Number = CByte(Val(txtSend.Text))
MSComm1.Output = Chr(Number)
txtState.Text = "SENDING" & txtState.Text
End Sub
Private Sub cmdClearState_Click()
txtState.Text = " "
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.DTREnable = True
MSComm1.Handshaking = comNone 'comRTS i only got a 3-wire connection - so no handshaking
MSComm1.InBufferSize = 1024 'not 2 we don't want a buffer overflow THAT fast
MSComm1.RThreshold = 1 'Raise OnComm-Event if 1 character is in Rx-buffer
MSComm1.RTSEnable = True
MSComm1.InputLen = 1 'get one character at a time
MSComm1.InputMode = comInputModeText 'the Binary mode never worked out for me - even when transmitting binary data
MSComm1.NullDiscard = False 'also NULL-Characters will be received
MSComm1.OutBufferSize = 512 'again we don't want overflow
MSComm1.SThreshold = 0 'don't hold back data - blow them out directly
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As String
Select Case MSComm1.CommEvent
Case comEvReceive
'txtReceived.Text = " "
Buffer = MSComm1.Input
txtReceived.Text = Asc(Buffer)
txtState.Text = "RX" + txtState.Text
End Select
End Sub

How to change the location of a picture box in VB 2010 using the arrow or wasd keys?

I only have access to the internet at school, so the filters get in the way of any real research. I'm currently coding an rpg for a school project but it's difficult to get the avatar to move on a map. Here's my pathetic code so far:
Public Class Map1
Private Sub USER_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
User.Top = User.Top - 1
End Sub
Private Sub USER_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
User.Top = User.Bottom + 1
'User.Location.X = 200
End Sub
End Class
I have the following problems with it:
User.location.x = 200 had syntax errors when I deleted the x and when I didn't.
The player also had to continually press the keys to move.
both I do not know how to correct.
Any help at all is greatly appreciated as it's for my final grade.
You can put it in a timer_tick to loop over it, that is what I do.
And the correct version of User.Location.X = 200 is:
User.location = new point(200, User.location.y)
nvm found the solution.
Here it is for anyone else how might need the code in the future.
Private Sub Map_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
User.Location = New Point(User.Location.X, User.Location.Y - 5)
ElseIf e.KeyCode = Keys.Down Then
User.Location = New Point(User.Location.X, User.Location.Y + 5)
ElseIf e.KeyCode = Keys.Left Then
User.Location = New Point(User.Location.X - 5, User.Location.Y)
ElseIf e.KeyCode = Keys.Right Then
User.Location = New Point(User.Location.X + 5, User.Location.Y)
End If
Module
Public Sub MovePictureBox(ByRef PictureBox As PictureBox)
Tiempo.Tag = PictureBox
AddHandler PictureBox.Parent.KeyDown, AddressOf Parent_KeyDown
AddHandler PictureBox.Parent.KeyUp, AddressOf Parent_KeyUp
AddHandler CType(PictureBox.Parent, Form).Load, AddressOf Parent_Load
End Sub
Private Up, Down, Left, Right As Boolean
WithEvents Tiempo As New Timer() With {.Interval = 1}
Private Sub Tiempo_Tick() Handles Tiempo.Tick
If Up Then Tiempo.Tag.Top -= 2
If Down Then Tiempo.Tag.Top += 2
If Left Then Tiempo.Tag.Left -= 2
If Right Then Tiempo.Tag.Left += 2
End Sub
Private Sub Parent_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Up Then Up = True
If e.KeyCode = Keys.Down Then Down = True
If e.KeyCode = Keys.Left Then Left = True
If e.KeyCode = Keys.Right Then Right = True
Tiempo.Enabled = True
End Sub
Private Sub Parent_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Tiempo.Enabled = False
Up = False : Down = False : Right = False : Left = False
End Sub
Private Sub Parent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
sender.KeyPreview = True
End Sub
End Module

Resources