PDA

Click to See Complete Forum and Search --> : problem programing with vb.net for pocket pc


llamothe
Oct 23rd, 2008, 10:54 AM
hi,

i am trying to make a program to do equations... and i cannot get any command line to work :( i am used with vb6 command line and i guess the .net ones are different...

i have this code so far:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim txttest As Integer
txttest = 1234
End Sub
End Class

i made it to test my command so when i press on the button, the numbers 1234 are supposed to show on the textfield (txttest)... but nothing happens when i press on the button... can someone help me please?

petevick
Oct 23rd, 2008, 11:36 AM
Hi,
sure you aren't getting an error as the line should read
txttest.text = "1234"

In VB.Net there is no default property, so you need to use the .text property

Pete

Shaggy Hiker
Oct 23rd, 2008, 11:37 AM
Where are you showing the number? What you have shown in the code is filling a variable, but at no time do you put the number into a label, textbox, or any other control. Where did you want it to display?

Shaggy Hiker
Oct 23rd, 2008, 11:38 AM
Hi,
sure you aren't getting an error as the line should read
txttest.text = "1234"

In VB.Net there is no default property, so you need to use the .text property

Pete

While I rarely disagree with Pete, he overlooked something here. txttest is declared in the sub as a variable of type integer. If there is also some control with that name on the form, then the local declaration in the sub will shadow the control and cause trouble.

llamothe
Oct 23rd, 2008, 11:59 AM
well... i showed the number just because my button for the equation was not working... so i was trying to see if the button was working at all... without success... here is the code i really want as a final product:

dim lbltps as integer
dim lbltvq as integer
dim txtavtaxes as integer
dim txtaptaxes as integer
lbltps = txtavtaxes * 0.05
tps2 = txtavtaxes * 1.05
lxbltvq = tps2 * 0.075
txtaptaes = lbltvq + tps2
lbltvq = Format(lbltvq, "0.00")
lbltps = Format(lbltps, "0.00")
txtavtaxes = Format(txtavtaxes, "0.00")
txtaptaxes = Format(txtaptaxes, "0.00")
so... when i press on a button it will do all those calculs... this was orginally a vb6 code that i am trying to port into a windows mobile device.

si_the_geek
Oct 23rd, 2008, 12:22 PM
That wouldn't work in VB6 either, for the same reasons... You have created variables with the same names as the controls, and are putting the values into the variables - not the controls.

Remove the Dim's for items that have the same names as your controls.


You will probably then get an error due to what Pete mentioned, as you apparently have what is considered to be a bad habit for VB6, and is not valid for VB.Net (not specifying which property you want to use).

llamothe
Oct 23rd, 2008, 12:52 PM
alright, so i remove the dim for my textbox and label...

all the values are monetary numbers... for example: 99.02... what do i put?

for lbltps i'd write for example:

txtavtaxes.text=txtavtaxes.texte*0.05


is that right or i understood it wrong?

Shaggy Hiker
Oct 23rd, 2008, 01:45 PM
That will only work if you have Option Strict OFF, which is a bad habit to get into (though one I had for a year or two, so I won't be throwing any stones). The proper way to write that would be something like this:

txtavtaxes.Text = (cdbl(txtavtaxes.Text)*0.05).ToString

That probably looks considerably more wordy than what you wrote, but the only difference is that I am explicitly telling the compiler to convert the string in the the txtavtaxes textbox .Text property into a double, then perform the computation, then turn the result back into a string to put into the other textbox. Your code would do that implicitly, but doing it implicitly is not as fast. Worse, in some situations (though not this one), doing implicit conversions can lead to really odd and puzzling bugs. For these two reasons, turning Option Strict ON is a good habit, despite the fact that it will force you to write a bit more code.

One other point I should make is that if txtavtaxes.Text is not a valid number, then CDbl will raise an exception, but so would your code. There are a few safer ways to do the conversion, but they take more code. One option would be this:

Dim d as double

if Double.TryParse(Txtavtaxes.Text,d) then
txtavtaxes.Text = (d * 0.05).ToString
Else
'Whatever was in the textbox could not be converted to a double.
txtavtaxes.Text = 0
End if

petevick
Oct 23rd, 2008, 02:57 PM
Sorry - didn't read the code properly - been a long day :(

I assumed that something starting with txt*** was a text box - never assume :)

You could always do ....

txtavtaxes.Text = (cdbl(val(txtavtaxes.Text))*0.05).ToString

which should work without errors, although not necessarily give the correct result.

Shaggy Hiker
Oct 23rd, 2008, 05:13 PM
Yeah, that would work. You wouldn't even need the cdbl in that example. Of course, if the value in the textbox was not a number then the answer would always be 0.

llamothe
Oct 24th, 2008, 09:00 AM
perfect, it is working! now i am trying to find a way to round the results to the second digits after the .

(for example, the answer is 87.837, it will display 87.84)


Sorry - didn't read the code properly - been a long day :(

I assumed that something starting with txt*** was a text box - never assume :)

You could always do ....

txtavtaxes.Text = (cdbl(val(txtavtaxes.Text))*0.05).ToString

which should work without errors, although not necessarily give the correct result.

llamothe
Oct 24th, 2008, 09:10 AM
oh and 1 last thing that i am looking for:
i want that, when i click in the textbox, it changes all the labels to nothing.. in vb the code was, for example the label is called lbltest:

the command would be:
lbltest=""

what is the equivalent in vb .net?

thnx a lot!

petevick
Oct 24th, 2008, 09:15 AM
Hi,
look at the 'format' property.

In vb.Net there is no default property, so it would be...
lbltest.text = ""

Pete

llamothe
Oct 24th, 2008, 12:36 PM
thnx for all the answers, the last thing i am confused with is how to round to the second digit after the .

for example: lbltest = 192.88993

so, lbltest would only show: 192.89

any idea for that last code?

petevick
Oct 24th, 2008, 12:50 PM
Did you look at 'format'?

in 'old' vb
Format (lblText.text,"###0.00")

llamothe
Oct 24th, 2008, 01:09 PM
Did you look at 'format'?

in 'old' vb
Format (lblText.text,"###0.00")

yeah i knew about that code in vb6, but it is not working on vb .net... anyone know another code?

llamothe
Oct 24th, 2008, 01:52 PM
one last thing, i am trying also to create the file so i can put in on my pocket pc.... and when i press build it is creating some kind of .exe file... is it possible to create a setup file or a cab file so i can install the program on my device and is it possible to install automatically the .net mobile framework with the installation file??

petevick
Oct 24th, 2008, 01:54 PM
look for 'deploy' or 'deployment' on this forum - loads of entries showing how to make a setup project

Shaggy Hiker
Oct 24th, 2008, 04:56 PM
Apparently you can't round a double, but you can round a decimal nicely. Since the two are very close, try this:

Assuming you have the value in a double called d

lblTest.Text = Decimal.Round(CDec(d),2).ToString

Decimal.Round will do the rounding, but it may not accept a double (it certainly won't if Option Strict is ON), so I explicitly cast it to a Decimal type. Oddly, the tooltip help that shows up for Decimal.Round suggests that you can only get an integer from that method, but that's true only for one of the four different versions of the method. If you pass it a decimal and an integer, as I am doing, then it rounds the decimal to the number of decimal places specified in the integer argument.

llamothe
Oct 27th, 2008, 09:56 AM
Apparently you can't round a double, but you can round a decimal nicely. Since the two are very close, try this:

Assuming you have the value in a double called d

lblTest.Text = Decimal.Round(CDec(d),2).ToString

Decimal.Round will do the rounding, but it may not accept a double (it certainly won't if Option Strict is ON), so I explicitly cast it to a Decimal type. Oddly, the tooltip help that shows up for Decimal.Round suggests that you can only get an integer from that method, but that's true only for one of the four different versions of the method. If you pass it a decimal and an integer, as I am doing, then it rounds the decimal to the number of decimal places specified in the integer argument.

when i try this code, i get an error message:
Error 1 Name 'd' is not declared.

also, another code i am looking for: to change a caracter for another...
for example: someone write 87.29 it will change the . for a , automatically... so it would write 87,29 instead of 87.29...

petevick
Oct 27th, 2008, 11:34 AM
Format statement is still supported in .Net - try
format(cdbl(lbltext.text),"##0.00")

Replace would change the . for a , but the decimal char depends on the culture used on the device, so in the UK it would be . but in Spain it would be ,

llamothe
Oct 27th, 2008, 12:05 PM
Format statement is still supported in .Net - try
format(cdbl(lbltext.text),"##0.00")

Replace would change the . for a , but the decimal char depends on the culture used on the device, so in the UK it would be . but in Spain it would be ,


i dont get any error message now but the command doesnt seem to change anything...

here is the full code:

Private Sub btajtaxes_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btajtaxes.Click
Dim tps2 As String
lbltps.Text = (CDbl(txtavtaxes.Text) * 0.05).ToString
tps2 = (CDbl(txtavtaxes.Text) * 1.05).ToString
lbltvq.Text = (CDbl(tps2) * 0.075).ToString
txttotal.Text = (CDbl(lbltvq.Text) + tps2).ToString
Format(CDbl(lbltvq.Text), "##0.00")
End Sub

techgnome
Oct 27th, 2008, 12:22 PM
tps2 shouldn't be a string.... dim it as a double... or decimal...

then eliminate the .ToStyring on the second calculation (where you set tps2).

also, some of your calculations are unnecessary...
Private Sub btajtaxes_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btajtaxes.Click
Dim tps2 As Decimal = CDbl(txtavtaxes.Text) * 1.05
lbltps.Text = tps2.ToString
lbltvq.Text = (tps2 * 0.075).ToString("##0.00")
txttotal.Text = (tps2 * 1.075).ToString
End Sub

fyi = Format is a function, so it returns a value... it doesn't actually DO the formating.... but the code above eliminates it anyways.

-tg

llamothe
Oct 27th, 2008, 12:50 PM
tps2 shouldn't be a string.... dim it as a double... or decimal...

then eliminate the .ToStyring on the second calculation (where you set tps2).

also, some of your calculations are unnecessary...
Private Sub btajtaxes_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btajtaxes.Click
Dim tps2 As Decimal = CDbl(txtavtaxes.Text) * 1.05
lbltps.Text = tps2.ToString
lbltvq.Text = (tps2 * 0.075).ToString("##0.00")
txttotal.Text = (tps2 * 1.075).ToString
End Sub

fyi = Format is a function, so it returns a value... it doesn't actually DO the formating.... but the code above eliminates it anyways.

-tg


it is working perfectly for lbltps! cool!
but when i try to add the same kind of code for the other fields

(i need to do the same thing for txtavtaxes.text, lbltps.text an d txttotal.text so it will change the . for a , and it will put only 2 digits after the ,)

Shaggy Hiker
Oct 27th, 2008, 04:00 PM
Show us the code that is not working.

By the way, my example with Decimal.Round assumed that you had a variable named d declared. That was just an example, had you put any other double variable in place of d, the code would work. Since you have now defined tps2 as type Decimal, you can now use Decimal.Round(<your decimal variable>,2) to get two decimal places. However, it looks like TechGnome has an easier formatting solution.

techgnome
Oct 27th, 2008, 04:22 PM
as for the rest of your calculations, samethign... do all your calculations on numerical datatypes (decimal, double, integer, etc) as much as you can. ONLY convert to string WHEN you are ready to put your result somewhere you need it to be a string. don't try to convert things to/from strings during the calculations. that only slows things down and gets confusing.

-tg