|
-
Feb 4th, 2011, 08:41 AM
#1
Thread Starter
New Member
[RESOLVED] Passing Variables from Form to Form
Im having a bit of a problem passing variables from one form to another. If somone could help i would be really greatful! Oh and for some reason my saveing sub routine dosent work, i cant get it to save the file in text format. Thanks!
Public Class DijkstrasAlgorithmFinal
Dim x As Integer
Dim Dijkstras(4, 3) As Integer
Dim InputValues(4, 4) As Integer
Dim valNum As Single
Dim txtBxArray As TextBox()
Dim shortestPath As Integer
Dim validation(5) As Boolean
Dim path(5) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtBxArray = New TextBox() {Aa, Ba, Ca, Da, Ab, Bb, Cb, Db, Ac, Bc, Cc, Dc, Ad, Bd, Cd, Dd}
Aa.Text = "0"
Bb.Text = "0"
Cc.Text = "0"
Dd.Text = "0"
End Sub
Private Function readgrid(ByVal x As Integer, ByVal y As Integer) As Integer
Dim index As Integer
index = 4 * (y - 1) + x - 1
readgrid = txtBxArray(index).Text
End Function
'Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
' 'example of how to use readgrid function to get data from grid
' Dim x As Integer
' Dim y As Integer
' x = txtX.Text
' y = txtY.Text
' txtOutput.Text = readgrid(x, y)
'End Sub
Private Sub Ab_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ab.TextChanged
Ba.Text = Ab.Text
End Sub
Private Sub Ba_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ba.TextChanged
Ab.Text = Ba.Text
Try
valNum = Ba.Text / Ab.Text
InputValues(1, 0) = Ba.Text
InputValues(0, 1) = Ab.Text
Catch ex As Exception
If x > 0 Then
MsgBox("You may only enter positive numbers! Error: " & Err.Number, MsgBoxStyle.Exclamation, "Error")
End If
x = x + 1
Ba.Text = ""
End Try
End Sub
Private Sub Ca_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ca.TextChanged
Ac.Text = Ca.Text
End Sub
Private Sub Ac_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ac.TextChanged
Ca.Text = Ac.Text
Try
valNum = Ac.Text / Ca.Text
InputValues(0, 2) = Ac.Text
InputValues(2, 0) = Ca.Text
Catch ex As Exception
If x > 0 Then
MsgBox("You may only enter positive numbers! Error: " & Err.Number, MsgBoxStyle.Exclamation, "Error")
End If
x = x + 1
Ac.Text = ""
End Try
End Sub
Private Sub Da_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Da.TextChanged
Ad.Text = Da.Text
End Sub
Private Sub Ad_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ad.TextChanged
Da.Text = Ad.Text
Try
valNum = Ad.Text / Da.Text
InputValues(0, 3) = Ad.Text
InputValues(3, 0) = Da.Text
Catch ex As Exception
If x > 0 Then
MsgBox("You may only enter positive numbers! Error: " & Err.Number, MsgBoxStyle.Exclamation, "Error")
End If
x = x + 1
Ad.Text = ""
End Try
End Sub
Private Sub Cb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cb.TextChanged
Bc.Text = Cb.Text
End Sub
Private Sub Bc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bc.TextChanged
Cb.Text = Bc.Text
Try
valNum = Bc.Text / Cb.Text
InputValues(1, 2) = Bc.Text
InputValues(2, 1) = Cb.Text
Catch ex As Exception
If x > 0 Then
MsgBox("You may only enter positive numbers! Error: " & Err.Number, MsgBoxStyle.Exclamation, "Error")
End If
x = x + 1
Bc.Text = ""
End Try
End Sub
Private Sub Dc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dc.TextChanged
Cd.Text = Dc.Text
End Sub
Private Sub Cd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cd.TextChanged
Dc.Text = Cd.Text
Try
valNum = Cd.Text / Dc.Text
InputValues(2, 3) = Cd.Text
InputValues(3, 2) = Dc.Text
Catch ex As Exception
If x > 0 Then
MsgBox("You may only enter positive numbers! Error: " & Err.Number, MsgBoxStyle.Exclamation, "Error")
End If
x = x + 1
Cd.Text = ""
End Try
End Sub
Private Sub Bd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bd.TextChanged
Db.Text = Bd.Text
End Sub
Private Sub Db_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Db.TextChanged
Bd.Text = Db.Text
Try
valNum = Db.Text / Bd.Text
InputValues(3, 1) = Db.Text
InputValues(1, 3) = Bd.Text
Catch ex As Exception
If x > 0 Then
MsgBox("You may only enter positive numbers! Error: " & Err.Number, MsgBoxStyle.Exclamation, "Error")
End If
x = x + 1
Db.Text = ""
End Try
End Sub
Private Sub refreshTxt(ByVal shortestpath As Integer, ByRef validation As Array, ByRef path As Array)
Dim i As Integer
shortestpath = 0
For i = 1 To 5
validation(i) = False
path(i) = 0
Next
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim whichPath As Integer
Call refreshTxt(shortestPath, validation, path)
If Ad.Text = 0 Then
validation(1) = False
Else
validation(1) = True
path(1) = Val(Ad.Text)
End If
If Ab.Text = 0 Or Bd.Text = 0 Then
validation(2) = False
Else
validation(2) = True
path(2) = Val(Ab.Text) + Val(Bd.Text)
End If
If Ac.Text = 0 Or Cd.Text = 0 Then
validation(3) = False
Else
validation(3) = True
path(3) = Val(Ac.Text) + Val(Cd.Text)
End If
If Ab.Text = 0 Or Bc.Text = 0 Or Cd.Text = 0 Then
validation(4) = False
Else
validation(4) = True
path(4) = Val(Ab.Text) + Val(Bc.Text) + Val(Cd.Text)
End If
If Ac.Text = 0 Or Cb.Text = 0 Or Bd.Text = 0 Then
validation(5) = False
Else
validation(5) = True
path(5) = Val(Ac.Text) + Val(Cb.Text) + Val(Bd.Text)
End If
Call calculateShortestPath(validation, path, shortestPath, whichPath)
MsgBox("Path " & whichPath & " is the shortest route and has a distance of " & shortestPath, , "Results")
btnVisualRepresentation.Enabled = True
End Sub
Private Sub btnPaths_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaths.Click
MsgBox("Path 1 is A-D", MsgBoxStyle.Information, "Path#")
MsgBox("Path 2 is A-B-D", MsgBoxStyle.Information, "Path#")
MsgBox("Path 3 is A-C-D", MsgBoxStyle.Information, "Path#")
MsgBox("Path 4 is A-B-C-D", MsgBoxStyle.Information, "Path#")
MsgBox("Path 5 is A-C-B-D", MsgBoxStyle.Information, "Path#")
End Sub
Private Sub btnVisualRepresentation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisualRepresentation.Click
VisualRepresentation.Show()
End Sub
Sub calculateShortestPath(ByVal validation As Array, ByVal path As Array, ByRef shortestPath As Integer, ByRef whichPath As Integer)
Dim i As Integer
For i = 1 To 5
If validation(1) = True Then
shortestPath = path(i)
End If
Next
For i = 1 To 5
If validation(i) = True Then
If path(i) < shortestPath Then
shortestPath = path(i)
whichPath = i
End If
End If
Next
End Sub
Private Sub btnInfo1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInfo1.Click
MsgBox("Please enter your node distances in the grid provided then use this button to calculate the shortest path", MsgBoxStyle.Information, "Calculate")
End Sub
Private Sub btnInfo2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInfo2.Click
MsgBox("Use this button to aquire a list of routes", MsgBoxStyle.Information, "Paths")
End Sub
Private Sub btnInf03_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInf03.Click
MsgBox("Use this button to view a visual representation of the algorithm (you must use calculate first)", MsgBoxStyle.Information, "VisualRepresentation")
End Sub
-
Feb 4th, 2011, 08:41 AM
#2
Thread Starter
New Member
Re: Passing Variables from Form to Form
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim open As OpenFileDialog = New OpenFileDialog
open.Title = "Open"
open.InitialDirectory = "E:\"
open.Filter = "Text Files (*.txt)|.txt*"
open.FilterIndex = 1
open.RestoreDirectory = True
If open.ShowDialog() = DialogResult.OK Then
txtBrowse.Text = open.FileName
End If
btnLoad.Enabled = True
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim Save As SaveFileDialog = New SaveFileDialog
Save.Title = "Save"
Save.InitialDirectory = "E:\"
Save.Filter = "Text Files (*.txt)|*txt"
Save.FilterIndex = 1
Save.RestoreDirectory = True
If Save.ShowDialog() = DialogResult.OK Then
''''''''''''''''''''''''''''
End If
FileOpen(1, Save.FileName, OpenMode.Output)
PrintLine(1, Ab.Text)
PrintLine(1, Ac.Text)
PrintLine(1, Ad.Text)
PrintLine(1, Ba.Text)
PrintLine(1, Bc.Text)
PrintLine(1, Bd.Text)
PrintLine(1, Ca.Text)
PrintLine(1, Cb.Text)
PrintLine(1, Cd.Text)
PrintLine(1, Da.Text)
PrintLine(1, Db.Text)
PrintLine(1, Dc.Text)
FileClose(1)
MsgBox("Successfuly saved to " & Save.FileName, MsgBoxStyle.Information, "Save")
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
FileOpen(1, txtBrowse.Text, OpenMode.Input)
Ab.Text = LineInput(1)
Ac.Text = LineInput(1)
Ad.Text = LineInput(1)
Ba.Text = LineInput(1)
Bc.Text = LineInput(1)
Bd.Text = LineInput(1)
Ca.Text = LineInput(1)
Cb.Text = LineInput(1)
Cd.Text = LineInput(1)
Da.Text = LineInput(1)
Db.Text = LineInput(1)
Dc.Text = LineInput(1)
FileClose(1)
MsgBox("Successfuly loaded", MsgBoxStyle.Information, "Load")
End Sub
End Class
-
Feb 4th, 2011, 08:42 AM
#3
Thread Starter
New Member
Re: Passing Variables from Form to Form
Public Class VisualRepresentation
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call hideVisuals(Ab, Ac, Ad, Bc, Cb)
End Sub
Private Sub hideVisuals(ByVal Ab As Integer, ByVal Ac As Integer, ByVal Ad As Integer, ByVal Bc As Integer, ByVal Cb As Integer, ByVal Cd As Integer)
If Ab = 0 Then
connectionAB.Visible = False
End If
If Ac = 0 Then
connectionAC.Visible = False
End If
If Ad = 0 Then
connectionAD.Visible = False
End If
If Bc = 0 Or Cb = 0 Then 'If Bc = 0 then Cb = 0 anyway but this makes it easier to understand
connectionBCCB1.Visible = False
connectionBCCB2.Visible = False
End If
If Cd = 0 Then
connectionCD.Visible = False
End If
If Ac = 0 And Bc = 0 Then
C.Visible = False
End If
If Ab = 0 And Cb = 0 Then
B.Visible = False
End If
End Sub
End Class
-
Feb 4th, 2011, 09:19 AM
#4
Re: Passing Variables from Form to Form
Three posts of that size, no formatting, and you expect us to figure out where you are doing the passing of the variables?
Here is a whole thread on passing variables between forms:
http://www.vbforums.com/showthread.php?t=466253
Aside from that, turn Option Strict ON, and use OrElse rather than Or.
My usual boring signature: Nothing
 
-
Feb 8th, 2011, 07:34 AM
#5
Thread Starter
New Member
Re: Passing Variables from Form to Form
Ok thanks for your help, iv sorted out the passing variables. However im genuinly stumped by my saving issue. I will only post the relevant code this time, sorry about before i was hoping that giving you all the code would make it easier.
VB08 Code:
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim open As OpenFileDialog = New OpenFileDialog
open.Title = "Open"
open.InitialDirectory = "E:\"
open.Filter = "Dijkstra's Algorithm Final Files (*.daf)|*.daf|txt|*.txt"
open.FilterIndex = 1
open.RestoreDirectory = True
If open.ShowDialog() = DialogResult.OK Then
txtBrowse.Text = open.FileName
ElseIf DialogResult.Cancel Then
'Do nothing
End If
btnLoad.Enabled = True
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim Save As SaveFileDialog = New SaveFileDialog
Save.Title = "Save"
Save.InitialDirectory = "E:\"
Save.Filter = "daf Files (*.daf)|*.daf"
Save.FilterIndex = 1
Save.RestoreDirectory = True
If Save.ShowDialog = DialogResult.OK Then
FileOpen(1, Save.FileName, OpenMode.Output)
PrintLine(1, Aa.Text)
PrintLine(1, Ab.Text)
PrintLine(1, Ac.Text)
PrintLine(1, Bc.Text)
PrintLine(1, Bd.Text)
PrintLine(1, Cd.Text)
FileClose(1)
MsgBox("Successfully saved to: " & Save.FileName)
FileOpen(1, Save.FileName, OpenMode.Output)
txtBrowse.Text = Save.FileName
ElseIf DialogResult.Cancel Then
'Do nothing
End If
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
FileOpen(1, txtBrowse.Text, OpenMode.Input)
Ab.Text = LineInput(1)
Ac.Text = LineInput(1)
Ad.Text = LineInput(1)
Bc.Text = LineInput(1)
Bd.Text = LineInput(1)
Cd.Text = LineInput(1)
FileClose(1)
MsgBox("Successfuly loaded", MsgBoxStyle.Information, "Load")
End Sub
At first i struggled with getting it to automatically save as a certain format, however now my problem is that i get an "EndOfStreamException Unhandled" error. Please any help would really be appreciated, thank you!
-
Feb 8th, 2011, 09:05 AM
#6
Re: Passing Variables from Form to Form
much easier to read with the formatting like Shaggy suggested. giving us the exact error like you did is good too.
Another tip would be telling us what line the error occurs and checking the values of any variables.
VB6 Library
If I helped you then please help me and rate my post!
If you solved your problem, then please mark the post resolved
-
Feb 8th, 2011, 10:03 AM
#7
Re: Passing Variables from Form to Form
That's all pretty old school VB6 code. People don't generally use those methods for opening files anymore (or for messageboxes, for that matter), and I barely remember them. Therefore, I'm not sure whether this is right or wrong, but it sure looks strange:
Code:
MsgBox("Successfully saved to: " & Save.FileName)
FileOpen(1, Save.FileName, OpenMode.Output)
The MsgBox is fine, though a legacy, I just included it to make it easier to locate the other line. Why do you open the file a second time? You already had it open, you don't write anything else to it, and you don't appear to close it after that line.
My usual boring signature: Nothing
 
-
Feb 8th, 2011, 04:33 PM
#8
Thread Starter
New Member
Re: Passing Variables from Form to Form
Thanks for your help, turns out it was a very basic error. That extra FileOpen shouldnt have been there, resolved now, thanks again. Dont you just hate it when you spend hours looking for the problem and re-writing code only to find it was the most blatent mistake :P
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|