millwalll

Hi all,

Working on a simple program that will convert money from GB pounds to US or Euro using this code. But i get a error with the ... after the As it says it is a invalid Character.

Thanks in advance Smile

Code SnippetPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
Label1.Text = "US$ " & Str(Val(TextBox1.Text) * 1.79)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
Label1.Text = "Euro " & Str(Val(TextBox1.Text) * 1.47)
End Sub
End Class




Re: Visual Basic General Error Help

ahmedilyas

Well you should use the .NET Classes rather than using the old VB classes otherwise whats the point in using .NET

Be sure that your input (textboxes) contain valid characters. Have you tried using the float class and did a parse

Dim format as new System.Globalization.CultureInfo("en-gb")

Dim result as float = Float.Parse(Me.TextBox1.Text, System.Globalization.NumberStyles.Currency, format) * 1.79

MessageBox.Show(result.ToString())

what happens now






Re: Visual Basic General Error Help

millwalll

Hi to be honest you lost me abit I am very new to VB. I am using VB 2005 Express Edition the course I am doing has a tutorial to follow. Here is copy of the tutorial maybe this will help.

Also what is the difference between net and older vb

Code Snippet

Visual Programming Tools C Tutorial 03

This week we will be writing VB programs that allow user input via various types of control, respond to control events, perform calculations and manipulate the property values of controls on the VB form. If you are just starting with your own log-in username, you may need to configure Visual Basic 2005 by performing steps 1-2 in last weeks tutorial.

1. Starting with the Projects tab panel, click on the New Project button at the bottom of the screen. You will be presented with the following:

2. Make sure that you have Windows Application Template selected, enter the name Currency Calculator in place of the default name and click OK.

3. By adding TextBox, Label and Button controls to the blank form in the centre and by adjusting the sizes and properties of each accordingly, construct a form with the following layout:



4. What we want the user to be able to do eventually is enter a value in GB into TextBox1, then click either the button US$ or Euro to get the equivalent amount displayed in the Label1 box below. The simplest way to define what happens when we click Button1 simply double click it now whilst in Design Mode; you will be presented with a VB code window containing the header and footer for a Button1 click event to which the middle line should be added thus:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

Label1.Text = "US$ " & Str(Val(TextBox1.Text) * 1.79)

End Sub

5. If you are perplexed by the cryptic nature of the formulae we have used above, please ask your tutor to explain.

6. The 1.79 in the formula is the GB to US$ exchange rate that may be adjusted as required.

7. Similarly, double click Button2 to enter the following code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As

Label1.Text = "Euro " & Str(Val(TextBox1.Text) * 1.47)

End Sub

8. You can now try a trial run of your program by clicking the button marked 4.

9. Why do you think we used a Label rather than a TextBox to receive the result.

10. Test you program with a few different whole number GB values. Now try it with a pounds and pence value. What happens

11. How is this helped if you replace the line in the event handler with the following:

Label1.Text = "US$ " & Format(Val(TextBox1.Text) * 1.79, "0.00")

Also make a similar change to the second event handler and rerun the program.

12. Add a third button to enable conversion of GB to be converted to Yen; (at the time writing there were 196.45 Yen to the GB.

13. To close down development of your project. Right click on Currency Calculator in the Solution Explorer panel in the top right of your screen and select Remove. If asked to save files, do so.

14. Start a new project along the same lines as the previous that will convert a temperature entered in degrees Celsius to Fahrenheit.

The formula is F = C * 1.8 + 32, but you will need to use Val & Str functions.

15. Close down the Visual Studio IDE and locate the saved files in your user area. You may be a little surprised at just how many folders and files have been set up for each project. Locate the bin folder and inside it you should find a self standing EXE executable version of your visual program. Try double clicking it to see if it runs outside of the Visual Studio environment.

16. We learn a lot by studying and analysing programs that have already been written. Copy the Sample Projects folder to your own area. Load up, run and study the Control Sampler project investigating the controls, properties and event handlers it involves.

17. Investigate the properties of the Horizontal Scrollbar property. It could be used as an alternative means of entering a Celsius (or any other value). Its value property indicates the position that the scrollbar is currently set to and an event is generated each time it is scrolled. Try producing a form that will show a Celsius value determined by the setting of a scroll-bar and calculate and display its equivalent Fahrenheit value.

18. Investigate the other sample programs included this week and check out how the different events change the attribute values of other controls.

19. Try and find out what is meant by the term Method. How does a Method differ from a Function

20. Investigate the properties of the DateTimePicker control. Place two DateTimePicker control objects on a form and produce code that will display the difference between them (in days) in a TextBox.

21. Investigate the following websites:

VB.NET Tutorial:

http://www.homeandlearn.co.uk/NET/vbNet.html

VB.NET General Standards:

http://faculty.stcc.edu/mccarthy/PROG208/Standards.htm

22. In particular, the second of the two sites above lays down the fairly well accepted standards for how one should give names to controls. Up to now we have tended to stick with the systems default names.

23. Challenge: Create a form looking something like the following:


Use meaningful names for each of the controls, trying to adopt the (Hungarian) standard for names.

Now make each of the +1 buttons add one to their respective score boxes, but remember that you are adding one to whats already in the box.

Make the Reset button clear both scores back to zero.

Have you tutor check your result.

24. Challenge: Experiment with alternative ways of controlling the score in the home/away boxes. (How about sliders )

25. Create a VAT calculator that will enable an ex VAT price to be entered and then, at the click of a button, will add VAT and show the new price. Try and keep the calculated price to two places of decimals only. Add a facility that given an inclusive price will show the amount of VAT and the ex VAT price.






Re: Visual Basic General Error Help

ahmedilyas

There is a lot of difference between vb6 and vb.net.

vb.net have classes that are not available in vb6 and since vb6 is old and unsupported, the new classes in .NET are better developed, designed and offer more choice and better overall than vb6 classes/modules so its better to use the newest classes available. Whoever designed that tutorial shouldnt really make tutorials imho.

What is the input in your textbox If you get an argumentexception then the input is not valid.






Re: Visual Basic General Error Help

millwalll

Hi this is picture of what I have got.

What has got to happen is the text box will allow the user to enter a amount. Then he/she will click on the us or the euro and that will then work out the price, and display it below in label field.

http://s109.photobucket.com/albums/n47/millwalll/ action=view&current=Untitled-1.jpg

You can find picture of it there what may help.






Re: Visual Basic General Error Help

ahmedilyas

Ah wait hangon, sorry, i misread your original post. I see the problem.

Your button event handler should be declared as follows:

Code Snippet

private sub button1_Click(byval sender as object, byval e as EventArgs) handles button1.Click

end sub

Still however, you should be using the .NET Classes rather than the whole VB6 legacy classes (Str, Val)






Re: Visual Basic General Error Help

millwalll

ok this what I have now is this right Thanks for the info on .net but I just started so dont really want learn about classes and variables and strings so on just yet.
Code SnippetPublic Class Form1
Private Sub Button1_Click(ByVal sender As system.Object, ByVal e As EventArgs)
Label1.Text = "US$ " & Str(Val(TextBox1.Text) * 1.79)
End Sub
Private Sub Button2_Click(ByVal sender As system.Object, ByVal e As EventArgs)
Label1.Text = "Euro " & Str(Val(TextBox1.Text) * 1.47)
End Sub
End Class







Re: Visual Basic General Error Help

ahmedilyas

Almost right, but add the "handles" portion in each declaration.

Really, if you double click the button control on the form, it will automatically create the event handler for you. So do this:

Private Sub Button1_Click(ByVal sender As system.Object, ByVal e As EventArgs) handles Button1.Click

Me.Label1.Text = "US$ " & Str(Val(Me.TextBox1.Text) * 1.79)


End Sub


Private Sub Button2_Click(ByVal sender As system.Object, ByVal e As EventArgs) handles Button2.Click

Me.Label1.Text = "Euro " & Str(Val(Me.TextBox1.Text) * 1.47)


End Sub






Re: Visual Basic General Error Help

CsabaU

Hi,

How are your settings Decimal comma or decimal point I'm using this functtion to parse a string.

Function GetReal(ByVal numberInString As String) As Double

Dim aStr As String = numberInString.Replace(","c, "."c)

Try

Return Val(aStr) ' It works like Float.Parse

Catch ex As Exception

GetReal = Double.MinValue

End Try

End Function

I have found Val more robust than the direct .NET Float.Parse so I stick to Val.

Can anyone confirm that Val is more robust

Csaba





Re: Visual Basic General Error Help

millwalll

Hi thanks,

That is working fine now Just one more question do you think you could explain what this does and mean

Private Sub Button1_Click(ByVal sender As system.Object, ByVal e As EventArgs) handles

I understand after that is assigns a value US$ to the label then get's the value by taking the information in the text box * it by 1.79.




Re: Visual Basic General Error Help

ahmedilyas

im afraid your doing things incorrectly. you should use the Float.Parse or TryParse method, giving it the options if any. In addition, you shouldn't be performing any replacements on your strings but rather make sure your string contains VALID inputs.

The classes were designed for a reason and should be used rather than old methods or "re-inventing" the wheel.




Re: Visual Basic General Error Help

ahmedilyas

millwalll wrote:
Hi thanks,

That is working fine now Just one more question do you think you could explain what this does and mean

Private Sub Button1_Click(ByVal sender As system.Object, ByVal e As EventArgs) handles

I understand after that is assigns a value US$ to the label then get's the value by taking the information in the text box * it by 1.79.

the "handles" keyword is generally used for an event (telling the method declaring what method it will handle), such as a Click or keypress or whatever the event maybe for that control.






Re: Visual Basic General Error Help

millwalll

What about this

Private Sub

and

(ByVal sender As system.Object, ByVal e As EventArgs)


many thanks sorry to be pain Smile





Re: Visual Basic General Error Help

ahmedilyas

millwalll wrote:
What about this

Private Sub

and

(ByVal sender As system.Object, ByVal e As EventArgs)


many thanks sorry to be pain

private sub is a declaration of a method (subroutine) which is accessible only to the current class, but not seen outside of this class if you were to make an instance of this class as an object.

As for the sender and e as eventargs, these are standard parameters for control events in general. You don't need to worry about this, especially if you are new to development.

The sender holds information about the control that was clicked on/used and the eventargs is just an empty parameter given.