7 Replies - 8092 Views - Last Post: 03 December 2008 - 09:41 PM Rate Topic: -----

#1 Mattman4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-January 08

creating a formated .txt file

Post icon  Posted 11 January 2008 - 01:46 PM

Im having problems with creating a .txt file from user input. (checkboxes txtboxes and radiobuttons)
My little project here is that im trying ot make a "Script Generator" for a game. The script are used by the Admin on a server to change all the players guns to a certain type.

heres the GUI so far adn maybe it will help get my point across
http://i168.photobuc...0444/layout.jpg

now what i need it to do is:
the user selects the gun type or types form the 1 section
then types what message is to be displayed when he runs that script (txt file) in game, and select color of that message in the 2nd section.
then inputs a file name for the txt file, and pushes the generate button and that txt file will be put on desktop.

the format of the txt file needs to be in this format.


admin message (color user selcts from radio button) (Admin message user types in)
admin forceclass 1 (weapon user selected from checkbox)
admin forceclass 2 (weapon user selected from checkbox)
etc all the way to 1000

This i the Code i have so far, its able to name a txt file based on what the user puts in, and write 1 line of text, but i can't format it......
	Private Sub btnDesktopgen1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDesktopgen1.Click

		Dim strAdminmessage, strFilename As String

		If (txtAdminmessage.Text.Length = 0) Then
			MessageBox.Show("You Must have an Admin Message.")
			txtAdminmessage.Focus()
			Exit Sub
		Else
			strAdminmessage = txtAdminmessage.Text
		End If

		If (txtFilename.Text.Length = 0) Then
			MessageBox.Show("You Must enter a File name.")
			txtFilename.Focus()
			Exit Sub
		Else
			strFilename = txtFilename.Text
		End If

		My.Computer.FileSystem.WriteAllText("D:\" & strFilename & ".txt", _
"Admin Message " & strAdminmessage & ".", True)


the problem is i can't get a new line of text to go underneth it

also im confused on how to make the checkboxes and radio buttons work, do i make If than statements or make them all strings? i have no idea

thx in advance ;)

This post has been edited by Mattman4: 11 January 2008 - 07:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: creating a formated .txt file

#2 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: creating a formated .txt file

Posted 12 January 2008 - 03:59 AM

You can add a new line to the end of each sentence by appending Environment.NewLine to the end of your string, like so:

My.Computer.FileSystem.WriteAllText("D:\" & strFilename & ".txt", _
"Admin Message " & strAdminmessage & "." & Environment.NewLine, True)


Also with checkboxes and radio buttons, you use If Then statements, using the Checked property associated with the controls.

Hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

#3 Mattman4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-January 08

Re: creating a formated .txt file

Posted 12 January 2008 - 01:51 PM

execelent, than you so much
the only other thing im confused on is how to get it to write multiple lines that are sequential....
like right now its write 1 line of "admin forceclass 1 r"
how do i get it to write the next line to "admin forceclass 2 r" automatically
like the next line would be 3, and the one after that 4, etc. i can't get it to automatically number the next line in sequential order...

my code so far (and i know its not even close) i know i need to use loops and variables, but im lost, i need a bump in the right direction

	Private Sub btnDesktopgen1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDesktopgen1.Click

		Dim strAdminmessage, strFilename As String


		If (txtAdminmessage.Text.Length = 0) Then
			MessageBox.Show("You Must have an Admin Message.")
			txtAdminmessage.Focus()
			Exit Sub
		Else
			strAdminmessage = txtAdminmessage.Text
		End If

		If (txtFilename.Text.Length = 0) Then
			MessageBox.Show("You Must enter a File name.")
			txtFilename.Focus()
			Exit Sub
		Else
			strFilename = txtFilename.Text
		End If

		My.Computer.FileSystem.WriteAllText("C:\Documents and Settings\Mattman\Desktop\" & strFilename & ".txt", _
		"Admin Message " & strAdminmessage & Environment.NewLine, True)


		If chkR.Checked = True Then
			My.Computer.FileSystem.WriteAllText _
			("C:\Documents and Settings\Mattman\Desktop\" & strFilename & ".txt", "Admin Forceclass 1 r" & Environment.NewLine, True)
		End If

			MessageBox.Show("Script Created on Desktop Sucsessfully")


	End Sub

This post has been edited by Mattman4: 12 January 2008 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: creating a formated .txt file

Posted 12 January 2008 - 04:47 PM

Not exactly sure if this is what you want but in my mind you have two options.

The first option would be to count the amount of "Forceclass", and have them added to the string programmatically, this can be done like so:

Dim forceCount As Integer = 0

For i As Integer = 0 To <INSERT THE AMOUNT OF "Forceclass" TO USE>
	forceCount += 1
	My.Computer.FileSystem.WriteAllText _
	("C:\Documents and Settings\Mattman\Desktop\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " r" & Environment.NewLine, True)
Next



The other option I thought of probably isn't the answer to your troubles, but as for the line number question, we can actually count the amount of lines in the file, and when we use this function:

Function getLineCount(ByRef file As String) As Integer
	Dim lineCount As Integer = 0
	Using lineByLine As New IO.StreamReader(file)
		While Not lineByLine.EndOfStream
			lineByLine.ReadLine()
			lineCount += 1
		End While
	End Using
	Return lineCount
End Function



Copy and paste that code below the Sub you are using, and then use the following code to write your "Forceclass" string to the file.

My.Computer.FileSystem.WriteAllText _
("C:\Documents and Settings\Mattman\Desktop\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(getLineCount("C:\Documents and Settings\Mattman\Desktop\" & strFilename & ".txt") + 1) & " r" & Environment.NewLine, True)


Hope that helps! :)
Was This Post Helpful? 0
  • +
  • -

#5 Mattman4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-January 08

Re: creating a formated .txt file

Posted 12 January 2008 - 11:31 PM

once again thx u so much, thats what i needed. so Thank you
and for the record i feel bad asking all these questions, but im a vb noob and i need help, ha ha ha

right now its set up that it will make 1500 admin forceclasses of the selected checkbox. How would i set up if more than one check box was selected like 3 or 4, or 10+ boxes, but for example lets say 2 check boxs like "r" and "m" were selected, how could i get them so they would alternate, like:

admin forceclass 1 r
admin forceclass 2 m
admin forceclass 3 r
admin forceclass 4 m
ect

and i looked into making a if statement for each combination of guns, but if i did the math right, there would be over 355 billion possible combos......HA HA HA, i don't wanna code that many statements

my code:
	Private Sub btnFoldergen1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFoldergen1.Click

		Dim strAdminmessage, strFilename As String
		Dim forceCount As Integer = 0


		'Dummy Edits

		If (txtAdminmessage.Text.Length = 0) Then
			MessageBox.Show("You Must have an Admin Message.")
			txtAdminmessage.Focus()
			Exit Sub
		Else
			strAdminmessage = txtAdminmessage.Text
		End If

		If (txtFilename.Text.Length = 0) Then
			MessageBox.Show("You Must enter a File name.")
			txtFilename.Focus()
			Exit Sub
		Else
			strFilename = txtFilename.Text
		End If


		'Gun Generation
		If chkR.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " r" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
			("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkM.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " m" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkAr.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " ar" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkG.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " g" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkAt4.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " at4" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkD.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " D" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkM82.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " m82" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkM24.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " m24" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkSpr.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " spr" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkM9.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " m9" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkSf.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " sf" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkBdm.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " bdm" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkRct.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " rct" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkAk.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " ak" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkAk74su.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " ak74su" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkGp.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " gp" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkRpk.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " Rpk" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkSvd.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " svd" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkMos.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " mos" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkV.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " v" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkRpg.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " rpg" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		ElseIf chkPso.Checked = True Then
			For i As Integer = 0 To 1499
				forceCount += 1
				My.Computer.FileSystem.WriteAllText _
				("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " pso" & Environment.NewLine, True)
			Next
			My.Computer.FileSystem.WriteAllText _
("C:\Program Files\America's Army\System\" & strFilename & ".txt", "Admin message	 " & strAdminmessage, True)
		End If

		MessageBox.Show("Script Created In C:\Program Files\America's Army\System\ Successfully")

	End Sub



thx in advance

This post has been edited by Mattman4: 13 January 2008 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#6 Mattman4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-January 08

Re: creating a formated .txt file

Posted 13 January 2008 - 12:25 PM

deleted by poster

This post has been edited by Mattman4: 13 January 2008 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 Mattman4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-January 08

Re: creating a formated .txt file

Posted 15 January 2008 - 02:06 PM

sorry about that ^^^ i though i could edit it later, but my time ran out i guess

but i think im getting closer to figuring this out, but im not quite there.

i guess my logic is that code gets executed like this: (in plain english)

if chkR is checked, then write "Admin forceclass 1 r" ONCE to the specified file
the move to the next if statement
if chkm is checked then write "Admin forceclass 2 m" ONCE to the specified file
the move to the next if statement

and so on till it ran out of IF statements, then it would loop to the 1st IF statement and continue

if chkR is checked, then write "Admin Forceclass 3 r" ONCE to the specified file
the move to the next if statement
if chkm is checked then write "Admin Forceclass 4 m" ONCE to the specified file
the move to the next if statement
it would keep running the if statements untill forceCount reached 1500

is there a way to limit how many times a thing is written to the document, without messing up the forceCount sequential order? Right now it will write it 1500 times, i only need one.

and on another note:
it would need to work if a statement was not true, for example:

if chkR is checked, chkM is NOT checked, and chkAR is checked:
it would go something like this:

if chkR is checked, then write "Admin forceclass 1 r" ONCE to the specified file
the move to the next if statement

if chkm is checked then write "Admin forceclass 2 m" ONCE to the specified file **(gets Skipped over because it is False, but the NEXT true statement would be the next sequential Number)**
the move to the next if statement

if chkAR is checked then write "Admin Forceclass 2 Ar" ONCE to the specified file
move on to the next if statement

and so on
getting looped back up to the 1st if statement (which is the chkR one) untill forceCount is less than 1501

this is so hard to explain, and its frustrating me
the code that is kinda close to what i want......i think
			While forceCount < 1501
			If chkR.Checked = True Then
				For i As Integer = 0 To 1499
					forceCount += 1
					My.Computer.FileSystem.WriteAllText _
					("C:\Documents and Settings\Mattman\Desktop\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " r" & Environment.NewLine, True)
				Next
			End If

			If chkM.Checked = True Then
				For i As Integer = 0 To 1499
					forceCount += 1
					My.Computer.FileSystem.WriteAllText _
					("C:\Documents and Settings\Mattman\Desktop\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " m" & Environment.NewLine, True)
				Next
			End If

			If chkAr.Checked = True Then
				For i As Integer = 0 To 1499
					forceCount += 1
					My.Computer.FileSystem.WriteAllText _
					("C:\Documents and Settings\Mattman\Desktop\" & strFilename & ".txt", "Admin Forceclass " & Convert.ToString(forceCount) & " ar" & Environment.NewLine, True)
				Next
			End If


Are while loops and If statements even the way to go?

any help? im really struggling and im running outta hair to pull out, ha ha ha

This post has been edited by Mattman4: 15 January 2008 - 11:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 arturorivas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-December 08

Re: creating a formated .txt file

Posted 03 December 2008 - 09:41 PM

Hi Matt, the current NewLine you have using the Environment.NewLine code works but in my opinion, its a little overkill because it is to format and set the environment on a terminal to work with, for example, Linux for "new line" or "Enter" key like for emulators. use the simple one that doesnt wait for more parameters and less chances of errors.(ControlChars.NewLine)

Here is an example on how to put it to use on a button click event:

My.Computer.FileSystem.WriteAllText _
("C:\MyTextFile.txt", TextBox1.Text _
& ControlChars.NewLine, True)


Also, using ControlChars allows you to set the standard for the ControlChars's class in your applications which is what you want to do since its methods such as "ControlChars.Tab"(for tabbing), "Cr"(Carriage-return character.), "NullChar"(ignoring a key), "ControlChars.Back"(backspace) you might actually use one day in your apps, this Class is also Printing formatting friendly.

-Arturo Rivas
San Rafael, CA
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1