13 Replies - 496 Views - Last Post: 22 February 2013 - 10:30 AM Rate Topic: -----

#1 mattijsstu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 18-February 13

Let a user pick one variable out of a table

Posted 21 February 2013 - 12:33 AM

Hi

This is my code:
Imports System.IO

Public Class Form1
    Dim result1 As String = ""
    Dim result2 As String = ""
    Dim result3 As String = ""
    Dim result4 As String = ""
    Dim result5 As String = ""
    Dim result6 As String = ""
    Dim result7 As String = ""
    Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        result1 = 2.718281828 ^ ((3 / 7) * (Math.Log(Val(TextBox1.Text)) - Math.Log(57.7) - Math.Log(Val(TextBox2.Text))))
        result2 = 50 / (result1 ^ (2 / 3))
        result3 = 0.5 * (result1)
        result6 = result3 + result1
        result4 = (result6 * (Math.Cos(Val(TextBox7.Text)).ToString)) / 2
        result5 = result4 + Val(TextBox6.Text).ToString
        result7 = result1 * Val(TextBox8.Text)
        TextBox3.Text = result1
        TextBox4.Text = result2
        TextBox5.Text = result3

    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            'open a new file stream with your selected filename from the SaveDialog control
            Using myStream As New StreamWriter(saveFileDialog1.FileName)
                myStream.WriteLine("D=" & result1)
                myStream.WriteLine("n_max=" & result2)
                myStream.WriteLine("d=" & result3)
                myStream.WriteLine("hv=" & result4)
                myStream.WriteLine("V=" & result5)
                myStream.WriteLine("ht=" & result7)

                myStream.Close()
            End Using
            MsgBox("Created File " & saveFileDialog1.FileName)
        End If
    End Sub

End Class


I would like to be able to let a user of my program pick 2 variables out of a table, the one he picks out of table one goes to the value of textbox2 and another value he picks out of a second table goes to textbox8. So when the programs runs the user gets 3 textboxes he needs to fill in and 2 tables to pick a variable out of, when he then pushes button2, it calculates.

Buth for now i still get an error with this basic program i included 2 screenshots of the error message and the values i fill in:
error message :
let me translate a bit:the conversion of string 2,144285745211874,28857149042375 to double is unvalid
Posted Image
this i fill in to my program, after clicking calculat(button2) i get the error above
Posted Image

Ty i hope i gave anough info

Is This A Good Question/Topic? 0
  • +

Replies To: Let a user pick one variable out of a table

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 06:46 AM

Above the Class statement, place the following code...

Option Strict On
Option Explicit On



Then fix any errors that are indicated with underlines in the code editor. You problem will be solved.

The culprit is line 15 in your code. result6 = result3 + result1 is not doing what you think it's doing. It is not adding two decimal values. It is concatenating two strings.

My advice to every programmer, beginner or not, is to ALWAYS use these two Options. They will save you time and frustration by making you think about what data types you are using, and showing you where the potential bugs in the code are.
Was This Post Helpful? 0
  • +
  • -

#3 mattijsstu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 18-February 13

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 07:15 AM

i have changes the result1 , result 2 etc to result as double, deleted the .tostring and then it still gave an error(when i start debug it fails) : conversion from string to double invalid. i find this strange since i can't find where i am converting strings to doubles..
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 07:40 AM

Did you fix all the errors after you put in the two Option lines I mentioned?

You should also change all your result variables to double. Yoy don't need to be converting numeric things to strings until you actually need to show them.

Perhaps you could re-post your code, and indicate the line in error.
Was This Post Helpful? 0
  • +
  • -

#5 mattijsstu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 18-February 13

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 07:47 AM

did some more changes, only thing now is that it does calculate but as result i get values wich say: NAN(not a number) so my results are not numbers?? don't get this


Imports System.IO
Public Class Form1
    Dim result1 As Double
    Dim result2 As Double
    Dim result3 As Double
    Dim result4 As Double
    Dim result5 As Double
    Dim result6 As Double
    Dim result7 As Double
    Dim resultfromtextbox1 As Double
    Dim resultfromtextbox2 As Double
    Dim resultfromtextbox6 As Double
    Dim resultfromtextbox7 As Double
    Dim resultfromtextbox8 As Double



    Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        result1 = 2.718281828 ^ ((3 / 7) * (Math.Log(resultfromtextbox1) - Math.Log(57.7) - Math.Log(resultfromtextbox2)))
        result2 = 50 / (result1 ^ (2 / 3))
        result3 = 0.5 * (result1)
        result6 = result3 + result1
        result4 = (result6 * (Math.Cos(resultfromtextbox7))) / 2
        result5 = result4 + resultfromtextbox6
        result7 = result1 * resultfromtextbox8
        TextBox3.Text = result1
        TextBox4.Text = result2
        TextBox5.Text = result3
        Double.TryParse(TextBox1.Text, resultfromtextbox1)
        Double.TryParse(TextBox2.Text, resultfromtextbox2)
        Double.TryParse(TextBox6.Text, resultfromtextbox6)
        Double.TryParse(TextBox7.Text, resultfromtextbox7)
        Double.TryParse(TextBox8.Text, resultfromtextbox8)
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            'open a new file stream with your selected filename from the SaveDialog control
            Using myStream As New StreamWriter(saveFileDialog1.FileName)
                myStream.WriteLine("D=" & result1)
                myStream.WriteLine("n_max=" & result2)
                myStream.WriteLine("d=" & result3)
                myStream.WriteLine("hv=" & result4)
                myStream.WriteLine("V=" & result5)
                myStream.WriteLine("ht=" & result7)

                myStream.Close()
            End Using
            MsgBox("Created File " & saveFileDialog1.FileName)
        End If
    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 08:04 AM

Of course you are going get NAN, all of values are Zero. Have a look using the debugger. Then step through the actual order of execution and see the values of the various variables.
Was This Post Helpful? 0
  • +
  • -

#7 mattijsstu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 18-February 13

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 08:24 AM

found the problem, wrong order of coding... buth my results are all wrong now ://>

edit: never mind i used points not comma's it's fixed.....

do you know the anwser or tips to my second question about the tables??

thanks you for the help alreasy

This post has been edited by mattijsstu: 21 February 2013 - 08:36 AM

Was This Post Helpful? 1
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 09:49 AM

View Postmattijsstu, on 21 February 2013 - 09:24 AM, said:

edit: never mind i used points not comma's it's fixed.....

do you know the anwser or tips to my second question about the tables??

I see by your second code posting that you did not have the Option statements that I mentioned. Again, I strongly recommend you use them. I hope that isn't your final code, because the variables resultfromtextbox.. are not doing anything at all. They are declared but never assigned values. If it's working, you must have changed them, though.

As for your second question... I don't see anything in your code to indicate how you are going to display a table, or how you want the selection to happen.
Was This Post Helpful? 0
  • +
  • -

#9 mattijsstu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 18-February 13

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 10:35 AM

thats just the thing i don't knwo i would like to display a table where the user can pick a variabel out. and for the resultfromtextbox, i relocated them so the program uses them
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Let a user pick one variable out of a table

Posted 21 February 2013 - 03:01 PM

View Postmattijsstu, on 21 February 2013 - 11:35 AM, said:

thats just the thing i don't knwo i would like to display a table where the user can pick a variabel out. and for the resultfromtextbox, i relocated them so the program uses them

Would these be numeric values? How many would you have in each table?

There are a number of ways to present them to the user. ComboBox, ListBox, Labels, UpDown controls, even RadioButtons, etc.
Was This Post Helpful? 0
  • +
  • -

#11 mattijsstu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 18-February 13

Re: Let a user pick one variable out of a table

Posted 22 February 2013 - 12:14 AM

the 2 tables look like this:
Posted Image

The x's are the values wich the user can pick from, they are numbers lik 0,00393 for the first table and 0,38 for the second table. So yes al numeric.
Was This Post Helpful? 0
  • +
  • -

#12 mattijsstu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 18-February 13

Re: Let a user pick one variable out of a table

Posted 22 February 2013 - 01:51 AM

i made a database in acces, mate datagrid in vs, and now it displays the table. then i made it so that the selected value moves to textbox2(see program code below) buth somtimes when i select a different cell the value in textbox2 does not change, how can this be, this is realy anoying and not userfriendly.
Imports System.IO
Public Class Form1
    Dim result1 As Double
    Dim result2 As Double
    Dim result3 As Double
    Dim result4 As Double
    Dim result5 As Double
    Dim result6 As Double
    Dim result7 As Double
    Dim resultfromtextbox1 As Double
    Dim resultfromtextbox2 As Double
    Dim resultfromtextbox6 As Double
    Dim resultfromtextbox7 As Double
    Dim resultfromtextbox8 As Double

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

    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

        TextBox2.Text = DataGridView1.CurrentCell.Value
    End Sub



    Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Double.TryParse(TextBox1.Text, resultfromtextbox1)
        Double.TryParse(TextBox2.Text, resultfromtextbox2)
        Double.TryParse(TextBox6.Text, resultfromtextbox6)
        Double.TryParse(TextBox7.Text, resultfromtextbox7)
        Double.TryParse(TextBox8.Text, resultfromtextbox8)
        result1 = 2.718281828 ^ ((3 / 7) * (Math.Log(resultfromtextbox1) - Math.Log(57.7) - Math.Log(resultfromtextbox2)))
        result2 = 50 / (result1 ^ (2 / 3))
        result3 = 0.5357 * result1
        result6 = result3 + result1
        result4 = (result6 / 2) * (Math.Cos((resultfromtextbox7 * Math.PI) / 180))
        result5 = result4 + resultfromtextbox6
        result7 = result1 * resultfromtextbox8
        TextBox3.Text = result1
        TextBox4.Text = result2
        TextBox5.Text = result3
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            'open a new file stream with your selected filename from the SaveDialog control
            Using myStream As New StreamWriter(saveFileDialog1.FileName)
                myStream.WriteLine("D=" & result1)
                myStream.WriteLine("n_max=" & result2)
                myStream.WriteLine("d=" & result3)
                myStream.WriteLine("hv=" & result4)
                myStream.WriteLine("V=" & result5)
                myStream.WriteLine("ht=" & result7)

                myStream.Close()
            End Using
            MsgBox("Created File " & saveFileDialog1.FileName)
        End If
    End Sub

   
End Class


EDIT: changed cellcontentclick to cellclick... made lots of difference :P

still can some one help me with the problem of sending the results i get to a database wich is blank, and when i run the program say a second time i want to be able to do the calculations and make the data go to the database in a second row, and not overwrite my previous calculations

This post has been edited by mattijsstu: 22 February 2013 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

#13 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Let a user pick one variable out of a table

Posted 22 February 2013 - 05:53 AM

Well, I'll have to bow out of this one now, because I don't use databases, and can't offer any advice on them.
Was This Post Helpful? 0
  • +
  • -

#14 mattijsstu  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 18-February 13

Re: Let a user pick one variable out of a table

Posted 22 February 2013 - 10:30 AM

ok no problemo! thanks for the info! i got it working with those databases :) just wondring if i can save the access form from within my prog

TY!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1