在计算中操作文本框变量 [英] manipulating textbox variables in calculations

查看:27
本文介绍了在计算中操作文本框变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码试图在标签页中使用变量.第一个标签页只有一个用于用户输入的文本框 (miles.text) 和一个用于计算的按钮:traveltime = mileage/speed.来自miles.text 的值存储在一个名为mileage 的变量中,而使用的速度存储在一个名为speed (me.speedtextbox.text) 的变量中.

I have some code where I am trying to use variables in a tabpage. The first tabpage only has one text box for user entry (miles.text) and a button to do a calculation: traveltime = mileage/speed. The value from miles.text is stored into a variable called mileage while the speed used is stored in a variable called speed (me.speedtextbox.text).

通常,执行 val(variable.text) 就像一个魅力,在这种情况下它不是这样做的.当用户输入 100 时,应该除以 65(数据库中的数字),因此答案应该是 1.53 小时.在我的例子中,我得到了无穷大",每当我对变量做其他任何事情时,我都会得到当从一个数字进行转换时,该值必须是一个小于无穷大的数字."但它是!只有 65,我仔细检查了数据集是否也这么说,确实如此.不知道为什么我会收到这个错误...谢谢!

Ordinarily, doing val(variable.text) works like a charm and it's not doing it in this case. When the user enters 100 for the mileage, it should be divided by 65 (the number in the database) and, therefore, the answer should be 1.53 hours. In my case, I'm getting "infinity" and whenever I do anything else with the variable, I get "when casting from a number, the value must be a number less than infinity." But it is! It's only 65 and I double-checked that the dataset said that too, which it does. Not sure why I am getting this error...thank you!

Public Class Form1

    Private Property Traveltime As Decimal

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'fooDataSet.testdata' table. You can move, or remove it, as needed.
        Me.TestdataTableAdapter.Fill(Me.foouDataSet.testdata)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mileage As Integer
        Dim speed As Integer


        mileage = Val(miles.Text)
        speed = Val(Me.SpeedTextBox.Text)
        traveltime = mileage / speed

        txttraveltime.text = Traveltime.ToString

    End Sub

    Private Sub txtrate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttraveltime.TextChanged

    End Sub
End Class

所以我做了一个测试程序,它只做一件事,那就是简单地读取一行数据库中的一个数据列,并将其存储到一个局部变量中,然后乘以 1.60,除了现在我得到对一个非共享成员需要一个对象引用",并且在我声明它时它似乎无法识别 Me.Speed.我究竟做错了什么?

So I did a test program where it did only one thing and that was to simply read one data column in a one row database and store it to a local variable and multiply it by 1.60 except now I am getting "reference to a non-shared member requires an object reference" and it doesn't seem to recognize Me.Speed when I declare it. What am I doing wrong?

Public Class Form1

    Dim Speed As Object
    Dim Me.Speed As New Speed

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Speed = CDec(fooDataSet.testdataRow.Item("speed"))*1.60
        Speedtextbox.text = Me.Speed.tostring

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'fooDataSet.testdata' table. You can move, or remove it, as needed.
        Me.TestdataTableAdapter.Fill(Me.fooDataSet.testdata)

    End Sub
End Class

推荐答案

我发现了问题所在.

要将字段从单行数据库存储到局部变量进行计算,显然它必须发生在 form1_load 事件中,在 dataadapter 填充语句之后,如下所示:

To store a field from a one-line database to a local variable for calculations, apparently it has to happen in the form1_load event, after the dataadapter fill statement, like so:

Me.TestdataTableAdapter.Fill(Me.foouDataSet.testdata)
  speed = Me.fooDataSet.testdata(0).speed

并且只是在公共类行之后将速度变暗为十进制.对于您想在类似的单个数据行中使用的任何其他字段,也可以这样做:

and just DIM speed as Decimal after the Public Class line. The same could be done for any other field you want to work with in a similar kind of single datarow:

yourvarname = Me.yourdatasetname.yourtablename(0).the_database_field_you_want_to_fetch

(哇!我刚刚写了一些教科书式的东西吗?大声笑)

(Wow! Did I just write something textbooky? LOL)

然后,点击按钮后,做一个计算,就是:

Then, after the button click, to do a calculation, it is:

traveltime = CDec(miles.Text/ speed) 
txttraveltime.Text = traveltime.ToString

确保将旅行时间调暗为十进制.

making sure to DIM traveltime as Decimal.

有效!问题是 (0) 表示第 0 行(因为它只有一行.)谢谢大家的帮助,尤其是 Competent_Tech.我学到了一些东西,我很高兴能回到你们身边并分享.

Works! The problem was the (0) to indicate row 0 (because it's only one row.) Thank you everyone for your help, especially Competent_Tech. I learned something and I'm happy that I could get back to you guys and share.

这篇关于在计算中操作文本框变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆