Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm making a gui for an archaic fortran program. The fortran program can run automatically when a text file with all the inputs are in the same folder.
I have almost no experience with visual basic, but I have gotten a text field to write to a file. What I need is help with a loop and formatting the output since the fortran program needs the input formatted perfectly.
this is the input form i have
http://img60.imageshack.us/img60/10...
-------------
Public Class TSMODPrivate Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "h:\test.txt"
Dim i As Integer
Dim arytext(100) As StringFor i = 1 To 69
arytext(i) = TextBox1.Text
NextIf System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
For i = 1 To 2
objWriter.WriteLine(i & " " & arytext(i))
NextobjWriter.Close()
MsgBox("Text written to file")
Else
MsgBox("File Does Not Exist")
End IfEnd Sub
End Class
------------My two questions are.
How can I get the text box here to increment with the loop?
arytext(i) = TextBox1.TextThen the hard part.
Getting the output to be strictly formatted like this list i have(1st set of numbers right align in 3rd column, numbers will only be max of 2 places)
(2nd set of number period at 19th column, 4 places after period no limit in front (until you hit the other number of course))
1 2.9410
2 0.1250
3 0.0300
4 0.0300
22 6546.0348
Thanks for any help guysI have a correctly formatted copy of the list and code here, http://rafb.net/p/JDf6Kc29.html this link will last for 24 hours.
is there a way to get things formatted in posts here? cause it's kinda hard to explain the formatting i need in my list without having it pasted right. thanks

How can I get the text box here to increment with the loop?
I think I know what you're asking. Try moving i and aryText() to the class level, then add buttons to increment and decrement as needed. Optionally, you could look at some other controls (especially the ones for DB interaction). I don't have VS in front of me at the moment, so I can't name specifics.Then the hard part. Getting the output to be strictly formatted like this list i have
That should be relatively easy. Read up on String.Formatis there a way to get things formatted in posts here?
Nope.
Can't do it.
Impossible.
Use the HTML PRE tags.

thanks for the link i'll read up on that.
don't think you got what i was talking about.
For i = 1 To 69
arytext(i) = TextBox1.Text
Next
in that I want to increment the TextBox1.Text to TextBox2.Text and so on. so arytext(69) would be equal to TextBox69.Textthanks for the pre tags
of course you can't edit the first post but watever.
the list
1 2.9410
2 0.1250
3 0.0300
4 0.0300
69 646846.6483

That's much harder. Hard enough that I'd suggest rethinking your GUI.
What I would do would be to make an array of TextBoxes, then programmically place them. Then you'd have no problem iterating though them.
If order doesn't matter, you could iterate though Me.Controls looking for TextBoxes, then incrementing i when you find one.

so I just need
arytext(1) = TextBox1.Text
.............
........................
..............................
......................................
.................................................
...............................................................
..........................................................................................
arytext(69) = TextBox69.Textthen?

Again, alternative approaches would be better, but if you're determined to use separate TextBoxes, then yes, I suppose. But don't become rooted in an implementation just because you already have something that kinda does what you want. VB.NET offers so much more.
Also, make sure your StreamWriter is writing to ASCII, and not UNICODE. If you don't, expect FORTRAN to either crash or spew out bad data.

well thanks for the hints and tips, I'll see what I can do. Then probably I'll be back.
EDIT
Read through that and didn't understand anything really. Now I know why I'm an engineer and not a programmer. I have the logical skills but suck at the syntax and commands.

I'm still quite stuck on this. Could someone help get me on the right track please. Just forget everything said up till now and I'll try and explain in the simplest way possible.
I'd like a gui with a set of text boxes each one for a different item. I'd enter in the data then hit a button and the data entered would be written to a new text file.
The data entered would be numbers with up to 4 places behind the decimal point.
The numbers will be output into a text file with a number in the first column. This number is a simple number from 1-70 or so. This number identifies what the value with it is for, like 1 is the inside diameter, 2 is the outside diameter, and so on.
The first number is right hand aligned to the 3rd column and the decimal place in the value number is right hand aligned to the 19th column. Formatted list shown below.
1 2.9410
2 0.1250
3 0.0300
4 0.0300
69 646846.6483I learned of the tab() command which allows the text to be written in a certain column but couldn't get it work with the command to write to a new line each time.
So if someone could help me get something that would create a file then write each line formatted, that would be of great help.

Alright, since I seem to 'help' you so much, I might as well follow though.
Now this is a very rough example, but it answers the questions you've asked so far:
Public Class Form1#Region " Setup stuff "
'Just ignore everything in this Region. I'm just programmically adding the buttons and whatnot.
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents btn1 As System.Windows.Forms.ButtonPrivate Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim col As System.Windows.Forms.DataGridViewTextBoxColumn
Me.DataGridView1 = New System.Windows.Forms.DataGridView
Me.btn1 = New System.Windows.Forms.ButtonMe.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.btn1)
Me.Controls.Add(Me.DataGridView1)
Me.Text = "Form1"
Me.DataGridView1.Location = New System.Drawing.Point(10, 34)
Me.DataGridView1.Size = New System.Drawing.Size(268, 156)col = New System.Windows.Forms.DataGridViewTextBoxColumn
col.HeaderText = "#"
col.ReadOnly = True
Me.DataGridView1.Columns.Add(col)col = New System.Windows.Forms.DataGridViewTextBoxColumn
col.HeaderText = "Value"
Me.DataGridView1.Columns.Add(col)Me.btn1.Location = New System.Drawing.Point(160, 230)
Me.btn1.Size = New System.Drawing.Size(117, 33)
Me.btn1.Text = "Save File"
End Sub
#End Region'Adds the number to the index column
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
sender.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
End Sub'This is what you care about. This is what does all of the work
Private Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click
Dim fout As IO.StreamWriter = New System.IO.StreamWriter("someFile.txt", False, System.Text.Encoding.ASCII)
For Each r As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows
If IsNumeric(r.Cells(1).Value) Then _
fout.WriteLine(FormatNum("##0", r.Cells(0).Value) & " " & FormatNum("#####0.0000", r.Cells(1).Value))
Next
fout.Close()
End Sub'.NET's Format functions don't allow space padding. So we must do it ourselves.
Private Function FormatNum(ByVal fmt As String, ByVal num As Double) As String
Dim s As String = num.ToString(fmt)
FormatNum = Microsoft.VisualBasic.Space(s.Length - fmt.Length) & s
End Function
End Class
You should be able to make a new default Win project, replace anything in the code window with that, and compile it.

thanks very much. Probably won't be able to test it since i have other things to do for a while

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |