I'm supposed to make a score calculator for a Programming assignment, for a football game.
it has 4 textboxes and a button, the function NEEDS to be there for full credit, I'm just not sure what I'm doing wrong.
Public Class Form1
Dim intTotal = 0
Dim intFirst = 0
Dim intSecond = 0
Dim intThird = 0
Dim intFourth = 0
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Try
Dim intFirst As Integer = Convert.ToInt32(txtFirst.Text)
Dim intSecond As Integer = Convert.ToInt32(txtSecond.Text)
Dim intThird As Integer = Convert.ToInt32(txtThird.Text)
Dim intFourth As Integer = Convert.ToInt32(txtFourth.Text)
Catch ex As Exception
MessageBox.Show("Enter in Digits!")
End Try
intTotal = calcTotal(intFirst, intSecond, intThird, intFourth, intTotal)
Me.lblTotal.Text = intTotal 'Shows as 0 at run-time
End Sub
Function calcTotal(ByVal intFirst As Integer, ByVal intSecond As Integer, ByVal intThird As Integer, ByVal intFourth As Integer, ByVal intTotal As Integer) As Integer
intTotal = intFirst + intSecond + intThird + intFourth
Return intTotal
End Function
End Class
lblTotal ends up displaying 0.
Your variables are declared at the block-level inside the try catch. Move the declarations out of the block and remove the class-level declarations (since they're not necesarry).
Like this:
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim intFirst As Integer = 0
Dim intSecond As Integer = 0
Dim intThird As Integer = 0
Dim intFourth As Integer = 0
Try
intFirst = Convert.ToInt32(txtFirst.Text)
intSecond = Convert.ToInt32(txtSecond.Text)
intThird = Convert.ToInt32(txtThird.Text)
intFourth = Convert.ToInt32(txtFourth.Text)
Catch ex As Exception
MessageBox.Show("Enter in Digits!")
End Try
Dim intTotal as Integer = calcTotal(intFirst, intSecond, intThird, intFourth, intTotal)
Me.lblTotal.Text = intTotal 'Shows as 0 at run-time
End Sub
Function calcTotal(ByVal intFirst As Integer, ByVal intSecond As Integer, ByVal intThird As Integer, ByVal intFourth As Integer, ByVal intTotal As Integer) As Integer
Return intFirst + intSecond + intThird + intFourth
End Function
End Class
I am not sure what your problem is but your variable declaration are outside of your function . You should initialize them inside.
Related
I am making a Math quiz game and I wondering why the question does not change to a NEW ONE when I get the right answer and HOW to make it to 10 and stop running then jump out a message box to ask the user want to PLAY AGAIN or not?
Public Class Multiplication
Dim TotalQuestion As Integer
Dim CorrectAnswer As Integer
Dim WrongAnswer As Integer
Dim R As New Random
Dim numOne As Integer = R.Next(0, 10)
Dim numTwo As Integer = R.Next(1, 10)
Dim Ans As Integer = 0
Dim Tries As Integer = 0
Private Sub Multiplication_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Generate()
End Sub
Sub Generate()
TotalQuestion = TotalQuestion + 1
Dim Show As String
Show = numOne & " x " & numTwo & " = "
lblQuestionMUL.Text = Show
End Sub
Private Function Multiply(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Return num1 * num2
Generate()
End Function
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Integer.TryParse(lblQuestionMUL.Text, numOne & numTwo)
Ans = Multiply(numOne, numTwo)
If Val(txtAnswer.Text) = Ans Then
CorrectAnswer = CorrectAnswer + 1
Else
WrongAnswer = WrongAnswer + 1
End If
lblCorrectAns.Text = CorrectAnswer
lblWrongAns.Text = WrongAnswer
txtAnswer.Clear()
txtAnswer.Focus()
Generate()
End Sub
End Class
In addition to previous comments you should set the numOne and numTwo variables to random numbers in Generate Sub (Keeping the declaration as Global Variables). Your code just set them in the beginning to random numbers only once. Check the code below:
Public Class Multiplication
Dim TotalQuestion As Integer = 0
Dim CorrectAnswer As Integer
Dim WrongAnswer As Integer
Dim R As New Random
Dim NumOne As Integer
Dim NumTwo As Integer
Dim QuizCompleted As Boolean = False
Dim Ans As Integer = 0
Dim Tries As Integer = 0
Private Sub Multiplication_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Generate()
End Sub
Sub Generate()
If TotalQuestion < 10 Then
NumOne = R.Next(0, 10)
NumTwo = R.Next(1, 10)
TotalQuestion = TotalQuestion + 1
Dim Show As String
Show = NumOne & " x " & NumTwo & " = "
lblQuestionMUL.Text = Show
Else
QuizCompleted = True
End If
End Sub
Private Function Multiply(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Generate()
Return num1 * num2
End Function
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Integer.TryParse(lblQuestionMUL.Text, NumOne & NumTwo)
Ans = Multiply(NumOne, NumTwo)
If Val(txtAnswer.Text) = Ans Then
CorrectAnswer = CorrectAnswer + 1
Else
WrongAnswer = WrongAnswer + 1
End If
lblCorrectAns.Text = CorrectAnswer
lblWrongAns.Text = WrongAnswer
txtAnswer.Clear()
txtAnswer.Focus()
If QuizCompleted Then
MsgBox("Quiz Completed")
End If
End Sub
End Class
I'm working on a school project using VIsual Studio 2015, the assignment is to create GUI "Commute Calculator" with three options for mode of transportation. I've wrote the code following the instructions in my textbook but am getting the following error:
"BC30057 Too many arguments to 'Private Function CarFindCost(intCommuteChoice As Integer, intDays As Integer) As Decimal'."
I'm a newbie to vs, but based on the error I believe the problem is with how I declared variables. I googled how to convert an integer to decimal, but haven't found anything that worked. The code is lengthy, but I included it all as an FYI. The error is in the private sub btnCommute and appears to be tied to three private functions: CarFindCost, BusFindCost and TrainFindCost. How to I fix it so I don't get errors on the variable intLength in the private sub bthCommute?
Option Strict On
Public Class frmCommuteCalc
Dim intCommuteChoice As Integer
Dim strSelectedMode As String = ""
Private _strGas As Integer
Private _strMiles As String = "Enter the total miles for a round trip: "
Private _strMilesPerGallon As Double = 2.15
Private _strDailyParking As Decimal = 10
Private _strMonthlyParking As Decimal
Private _strMonthlyUpkeep As Decimal = 112
Private _strRTBusFare As String = "Round trip bus fare is "
Private _strRTTrainFare As String = "Round trip train fare is "
Private _StrDays As String = "Enter the number of days worked per month: "
Private _intTrainFare As Integer
Private Sub frmCommuteCalc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Threading.Thread.Sleep(5000)
End Sub
Private Sub cboCommuteMethod_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCommuteMethod.SelectedIndexChanged
Dim intCommuteChoice As Integer
intCommuteChoice = cboCommuteMethod.SelectedIndex()
lstCommute.Items.Clear()
Select Case intCommuteChoice
Case 0
Car()
Case 1
Train()
Case 2
Bus()
End Select
lblDays.Visible = True
lblMiles.Visible = True
lblLength.Visible = True
txtDays.Visible = True
'txtMonthlyTotal.Visible = True
End Sub
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim intCommuteChoice As Integer
Dim intDaysPerMonth As Integer
Dim decTotalCost As Decimal
Dim intLength As Integer = 0
Dim strSelectedMode As String = ""
Dim blnNumberInDaysIsValid As Boolean = False
Dim blnCommuteMethodIsSelected As Boolean = False
blnNumberInDaysIsValid = ValidateNumberInDays()
intCommuteChoice = ValidateCommuteSelection(blnCommuteMethodIsSelected, strSelectedMode)
If (blnNumberInDaysIsValid And blnCommuteMethodIsSelected) Then
intDaysPerMonth = Convert.ToInt32(txtDays.Text)
intCommuteChoice = cboCommuteMethod.SelectedIndex()
Select Case intCommuteChoice
Case 0
decTotalCost = CarFindCost(intCommuteChoice, intDaysPerMonth, intLength)
Case 1
decTotalCost = BusFindCost(intCommuteChoice, intDaysPerMonth, intLength)
Case 2
decTotalCost = TrainFindCost(intCommuteChoice, intDaysPerMonth, intLength)
End Select
End If
End Sub
Private Function intLength() As Object
Throw New NotImplementedException()
End Function
Function ComputeCommuteCost(ByVal decMiles As Decimal, ByVal decGallons As Decimal) As Decimal
Dim decMilage As Decimal
decMilage = decMiles / decGallons
Return decMilage
End Function
Private Sub Car()
lstCommute.Items.Add(_strMiles)
lstCommute.Items.Add(_strMilesPerGallon)
lstCommute.Items.Add(_StrDays)
lstCommute.Items.Add(_strMonthlyParking)
lstCommute.Items.Add(_strMonthlyUpkeep)
End Sub
Private Sub Bus()
lstCommute.Items.Add(_strRTBusFare)
lstCommute.Items.Add(_StrDays)
End Sub
Private Sub Train()
lstCommute.Items.Add(_StrDays)
lstCommute.Items.Add(_strRTTrainFare)
End Sub
Private Function ValidateNumberInDays() As Boolean
Dim intDays As Integer
Dim blnValidityCheck As Boolean = False
Dim strNumberInDaysMessage As String = "Please enter the No. of days per month you will be commuting "
Dim strMessageBoxTitle As String = "Error"
Try
intDays = Convert.ToInt32(txtDays.Text)
If intDays >= 1 And intDays <= 21 Then
blnValidityCheck = True
Else
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
End If
Catch Exception As FormatException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
Catch Exception As OverflowException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
Catch Exception As SystemException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
End Try
Return blnValidityCheck
End Function
Private Function ValidateCommuteSelection(ByRef blnDays As Boolean, ByRef strDays As String) As Integer
Dim intCommuteChoice As Integer
Try
intCommuteChoice = Convert.ToInt32(lstCommute.SelectedIndex)
strDays = lstCommute.SelectedItem.ToString()
blnDays = True
Catch Exception As SystemException
MsgBox("Select a commute mode", , "Error")
blnDays = False
End Try
Return intCommuteChoice
End Function
Private Function CarFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim decDaysPerMonth As Decimal
Dim decMiles As Decimal
Dim decMilesPerGallon As Decimal = 2
Dim decGasTotal As Decimal
Dim decDailyParking As Decimal = 10
Dim decMonthlyParking As Decimal
Dim decMonthlyUpkeep As Decimal = 112
Dim decFinalCost As Decimal
Dim intLength As Integer = 0
decMiles = Convert.ToDecimal(txtMiles.Text)
decMilesPerGallon = Convert.ToDecimal(txtGallons.Text)
decGasTotal = decMilesPerGallon * decMiles
decMonthlyParking = Convert.ToDecimal(lblLength.Text)
decMonthlyParking = decDailyParking * decDaysPerMonth
decFinalCost = Convert.ToDecimal(lblLength.Text)
decFinalCost = decGasTotal + decMonthlyUpkeep + decMonthlyParking
Return decFinalCost
End Function
Private Function BusFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim intLength As Integer = 0
Dim decDaysPerMonth As Decimal
Dim decBusFarePerDay As Decimal = 4
Dim decFinalCost As Decimal
decBusFarePerDay = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = decBusFarePerDay * decDaysPerMonth
Return decFinalCost
End Function
Private Function TrainFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim intLength As Integer = 0
Dim decDaysPerMonth As Decimal
Dim decTrainFarePerDay As Decimal = 18
Dim decFinalCost As Decimal
decTrainFarePerDay = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = decDaysPerMonth * decTrainFarePerDay
Return decFinalCost
End Function
End Class
In:
Select Case intCommuteChoice ... End Select
in each case; call function CarFindCost with 3 parameters y you have declared with only 2.
I have 4 text box that accepts numbers from user and i want to check for what are the two largest numbers entered, can this be done using if then else statements
Dim Big_num_1 As Integer
Dim Big_num_2 As Integer
'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
'if then else statements
End Sub
i am a beginer in Vb.net and i would really appreciate any help that i can get
If possible i would like a If then else statement solution for this
This can be done with an if-statement like this..
Dim Big_num_1 As Integer = 0
Dim Big_num_2 As Integer = 0
'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
Dim numList(4) as Integer
numList(0) = txtbox_1
numList(1) = txtbox_2
numList(2) = txtbox_3
numList(3) = txtbox_4
For x as Integer = 0 to numList.count - 1
If numList(x) > Big_num_1 Then
Big_num_2 = Big_num_1
Big_num_1 = numList(x)
Else If numList(x) > Big_num_2 Then
Big_num_2 = numList(x)
End If
Next
End Sub
Private Sub Docheck()
Dim Numbers As String() = {TextBox1.Text, TextBox2.Text, TextBox3.Text}
Dim Ordered As String() = Numbers.OrderByDescending(Function(x) CInt(x)).ToArray
Dim Highest As Integer = CInt(Ordered(0))
Dim SecondHighest As Integer = CInt(Ordered(1))
MessageBox.Show(String.Concat(Highest , " " , SecondHighest))
End Sub
And in your button click event you call
Docheck()
You could use If/Else statements, but that would be the hard way to do this:
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
Dim Numbers = { TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text }
Dim results = Numbers.Select(Function(s) Integer.Parse(s)).OrderByDescending().Take(2).ToList()
Big_num_1 = results(0)
Big_num_2 = results(1)
End Sub
I found one code which auto-change proxies from own text file located in computer.
Now I would like to rather this get path from my own URL where I store my private list of proxies, so what I have to change in the code please? and could also someone explain why? thank you.
Imports System
Imports System.Runtime.InteropServices
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Timers
Public Class Form1
Dim FILE_NAME As String = "C:\Users\name\Documents\proxylist.txt"
Dim label As String
Public proxy(2000) As String
Public index As Integer = 0
Public max_proxys As Integer = 0
Dim a As String
Dim start_check As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (start_check > 0) Then
index = 0
Do While index <> max_proxys
proxy(index) = ""
index = index + 1
Loop
If TextBox3.Text = "" Then
FILE_NAME = "C:\Users\name\Documents\proxylist.txt"
End If
End If
If TextBox3.Text <> "" Then
FILE_NAME = TextBox3.Text
End If
Try
Dim reader As StreamReader = My.Computer.FileSystem.OpenTextFileReader(FILE_NAME)
index = 0
Do While reader.Peek <> -1
a = reader.ReadLine
proxy(index) = a.ToString
index = index + 1
Loop
max_proxys = index
reader.Close()
Catch ex As Exception
MessageBox.Show("File Not Found")
Timer1.Stop()
End Try
label = "true"
index = 0
TextBox1.Text = proxy(0)
If TextBox2.Text = "" Then
Timer1.Interval = 1000 'ms
Else
Try
Dim a As Integer = Convert.ToDecimal(TextBox2.Text)
Timer1.Interval = a * 1000 'ms
Catch ex As Exception
MessageBox.Show(ex.Message & "Please Enter Valid Time in Seconds ")
If Timer1.Enabled Then
Timer1.Stop()
End If
End Try
End If
start_check = 1
Timer1.Start()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
label = "false"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim clsProxy As New IEProxy
clsProxy.DisableProxy()
End Sub
Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
TextBox1.Text = proxy(index)
index = index + 1
Dim clsProxy As New IEProxy
If clsProxy.SetProxy(TextBox1.Text) Then
MessageBox.Show("Proxy successfully enabled.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Error enabling proxy.", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
If index >= max_proxys Then
index = 0
End If
If label.Equals("false") Then
Timer1.Stop()
End If
End Sub
End Class
Public Class IEProxy
Public Enum Options
INTERNET_PER_CONN_FLAGS = 1
INTERNET_PER_CONN_PROXY_SERVER = 2
INTERNET_PER_CONN_PROXY_BYPASS = 3
INTERNET_PER_CONN_AUTOCONFIG_URL = 4
INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5
INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_PER_CONNECTION_OPTION = 75
INTERNET_OPTION_SETTINGS_CHANGED = 39
PROXY_TYPE_PROXY = &H2
PROXY_TYPE_DIRECT = &H1
End Enum
<StructLayout(LayoutKind.Sequential)> _
Private Class FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Class
<StructLayout(LayoutKind.Explicit, Size:=12)> _
Private Structure INTERNET_PER_CONN_OPTION
<FieldOffset(0)> Dim dwOption As Integer
<FieldOffset(4)> Dim dwValue As Integer
<FieldOffset(4)> Dim pszValue As IntPtr
<FieldOffset(4)> Dim ftValue As IntPtr
Public Function GetBytes() As Byte()
Dim b(12) As Byte
BitConverter.GetBytes(dwOption).CopyTo(b, 0)
Select Case dwOption
Case Options.INTERNET_PER_CONN_FLAGS
BitConverter.GetBytes(dwValue).CopyTo(b, 4)
Case Options.INTERNET_PER_CONN_PROXY_BYPASS
BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4)
Case Options.INTERNET_PER_CONN_PROXY_SERVER
BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4)
End Select
Return b
End Function
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Class INTERNET_PER_CONN_OPTION_LIST
Public dwSize As Integer
Public pszConnection As String
Public dwOptionCount As Integer
Public dwOptionError As Integer
Public pOptions As IntPtr
End Class
<StructLayout(LayoutKind.Sequential)> _
Private Class INTERNET_PROXY_INFO
Public dwAccessType As Integer
Public lpszProxy As IntPtr
Public lpszProxyBypass As IntPtr
End Class
Private Const ERROR_INSUFFICIENT_BUFFER = 122
Private Const INTERNET_OPTION_PROXY = 38
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
<DllImport("wininet.dll")> _
Private Shared Function InternetSetOption(ByVal hInternet As IntPtr, _
ByVal dwOption As Integer, _
ByVal lpBuffer As INTERNET_PER_CONN_OPTION_LIST, _
ByVal dwBufferLength As Integer) As Boolean
End Function
<DllImport("kernel32.dll")> _
Private Shared Function GetLastError() As Integer
End Function
Public Function SetProxy(ByVal proxy_full_addr As String) As Boolean
Dim bReturn As Boolean
Dim list As New INTERNET_PER_CONN_OPTION_LIST
Dim dwBufSize As Integer = Marshal.SizeOf(list)
Dim opts(3) As INTERNET_PER_CONN_OPTION
Dim opt_size As Integer = Marshal.SizeOf(opts(0))
list.dwSize = dwBufSize
list.pszConnection = ControlChars.NullChar
list.dwOptionCount = 3
'set flags
opts(0).dwOption = Options.INTERNET_PER_CONN_FLAGS
opts(0).dwValue = Options.PROXY_TYPE_DIRECT Or Options.PROXY_TYPE_PROXY
'set proxyname
opts(1).dwOption = Options.INTERNET_PER_CONN_PROXY_SERVER
opts(1).pszValue = Marshal.StringToHGlobalAnsi(proxy_full_addr)
'set override
opts(2).dwOption = Options.INTERNET_PER_CONN_PROXY_BYPASS
opts(2).pszValue = Marshal.StringToHGlobalAnsi("local")
Dim b(3 * opt_size) As Byte
opts(0).GetBytes().CopyTo(b, 0)
opts(1).GetBytes().CopyTo(b, opt_size)
opts(2).GetBytes().CopyTo(b, 2 * opt_size)
Dim ptr As IntPtr = Marshal.AllocCoTaskMem(3 * opt_size)
Marshal.Copy(b, 0, ptr, 3 * opt_size)
list.pOptions = ptr
'Set the options on the connection
bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
If Not bReturn Then
Debug.WriteLine(GetLastError)
End If
'Notify existing Internet Explorer instances that the settings have changed
bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_SETTINGS_CHANGED, Nothing, 0)
If Not bReturn Then
Debug.WriteLine(GetLastError)
End If
'Flush the current IE proxy setting
bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_REFRESH, Nothing, 0)
If Not bReturn Then
Debug.WriteLine(GetLastError)
End If
Marshal.FreeHGlobal(opts(1).pszValue)
Marshal.FreeHGlobal(opts(2).pszValue)
Marshal.FreeCoTaskMem(ptr)
Return bReturn
End Function
Public Function DisableProxy() As Boolean
Dim bReturn As Boolean
Dim list As New INTERNET_PER_CONN_OPTION_LIST
Dim dwBufSize As Integer = Marshal.SizeOf(list)
Dim opts(0) As INTERNET_PER_CONN_OPTION
Dim opt_size As Integer = Marshal.SizeOf(opts(0))
list.dwSize = dwBufSize
list.pszConnection = ControlChars.NullChar
list.dwOptionCount = 1
opts(0).dwOption = Options.INTERNET_PER_CONN_FLAGS
opts(0).dwValue = Options.PROXY_TYPE_DIRECT
Dim b(opt_size) As Byte
opts(0).GetBytes().CopyTo(b, 0)
Dim ptr As IntPtr = Marshal.AllocCoTaskMem(opt_size)
Marshal.Copy(b, 0, ptr, opt_size)
list.pOptions = ptr
'Set the options on the connection
bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)
If Not bReturn Then
Debug.WriteLine(GetLastError)
End If
'Notify existing Internet Explorer instances that the settings have changed
bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_SETTINGS_CHANGED, Nothing, 0)
If Not bReturn Then
Debug.WriteLine(GetLastError)
End If
'Flush the current IE proxy setting
bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_REFRESH, Nothing, 0)
If Not bReturn Then
Debug.WriteLine(GetLastError)
End If
Marshal.FreeCoTaskMem(ptr)
Return bReturn
End Function
End Class
Maybe just download your url to the FILE_NAME location in your form's load event. Be warned that if there is a file at FILE_NAME, this sub I provide you will overwrite it.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim YourURL As String = "http://www.yoururl.com/filename.ext"
Using w As New System.Net.WebClient
w.DownloadFile(YourURL, FILE_NAME)
End Using
End Sub
Im trying to make a program that when i put a value less than 0, in a label something like "
negative numbers cant be used i have this
Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
Dim intSide1 As Integer = txtSide1.Text
Dim intSide2 As Integer = txtSide2.Text
Dim intSide3 As Integer = txtSide3.Text
Dim intSide4 As Integer = txtSide4.Text
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
Catch ex As Exception
MessageBox.Show("Incorrect Input. Enter a numeric value.")
End Try
End Sub
End Class
Yes i have to use the try catch method and i need help for this
This sounds an awful lot like homework, but you really need to convert your integers properly and then perform actual comparisons.
Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
' Relies on the GetIntegerInput method to throw exceptions
' for invalid entries
Dim intSide1 As Integer = GetIntegerInput(txtSide1.Text)
Dim intSide2 As Integer = GetIntegerInput(txtSide2.Text)
Dim intSide3 As Integer = GetIntegerInput(txtSide3.Text)
Dim intSide4 As Integer = GetIntegerInput(txtSide4.Text)
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Function GetIntegerInput(ByVal input as String) As Integer)
Dim returnValue as Integer
' Will attempt a proper try parse. AndAlso will short circuit
' the comparison so a failure in TryParse will not perform the
' the second evaluation. In either case, an actual exception is
' thrown with your invalid numeric message
If (Not Int32.TryParse(input, returnValue) AndAlso returnValue < 0) Then
Throw New ArgumentException("Incorrect input. Enter a proper numeric value.")
End If
Return returnValue
End Function
End Class
Well a integer can be negative...
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
Dim intSide1 As Integer = txtSide1.Text
Dim intSide2 As Integer = txtSide2.Text
Dim intSide3 As Integer = txtSide3.Text
Dim intSide4 As Integer = txtSide4.Text
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
//New code
if intSide1 < 0 or intSide2 < 0 or intSide3 < 0 intSide4 < 0 Then
MessageBox.Show("Incorrect Input. Negative number not valid")
end if
Catch ex As Exception
MessageBox.Show("Incorrect Input. Enter a numeric value.")
End Try
End Sub