help with vb.net

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
hey all,

I'v writed some code (below) that runs a batch file and i'm trying to direct the output to a form called formstatus. On this form there is a list box (so as to make it scrollable) in which i'd like the batch file output to be displayed.

firstly, the red line throws an error "value cannot be null, parameter name: item" since i'm new to this and lerning as i go i have no idea how to correct it!!

secondly, Once that error is cleared am i write in thinking that the listbox will show all of the outputed display but only after it has completed? Is there a way to make to update "live" so i could see each line displayed as it's beening run in the batch file?

cheers in advance.

Imports system.io

Public Class formtest
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles testrun.Click

'writes box1.text to a txt file for go.bat to import
Shell("c:\2go.bat " + box1.Text)
' Set start information.
Dim start_info As New ProcessStartInfo("c:\go.bat")
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True

' Make the process and set its start information.
Dim proc As New Process()
proc.StartInfo = start_info

' Start the process.
proc.Start()

' Attach to stdout and stderr.
Dim std_out As StreamReader = proc.StandardOutput()
Dim std_err As StreamReader = proc.StandardError()

' Display the results.
Dim i As Integer

For i = 0 To 15 ' 0 to <number of lines>
Formstatus.Show()
Formstatus.Status.Items.Add(std_out.ReadLine)
Next i

Me.Close()

' Clean up.
std_out.Close()
std_err.Close()
proc.Close()
End Sub


End Class
 
Last edited:
Formstatus.Status.Items.Add takes a ListItem object create a new one ussing the values from std_out.ReadLine then use that.

Seems to me though you'd be better of with just a multiline textbox? (maybe i've read it wrong! its still early!)
 
Last edited:
sorry i'm very new to this.

Thanks for the reply.

Could you please be a bit more specific???

cheers again
 
have you tried debugging the program and then stepping through it.

I usually do that if I find an issue with any of my code.

maybe it is worth putting some form of test condition for when your for loop is reading in the output.
not used vb.net but using c#
something like
//test if std_out.ReadLine is null
if(std_out.ReadLine == null)
{
//if condition true
Formstatus.Status.Items.Add("null value")
}
else
{
//condition is false
Formstatus.Status.Items.Add(std_out.ReadLine)
}


but as .Andy said you may be better off with a multiline textbox rather than a list box.

all you would need to do is set the multiline property for the textbox to true.
 
hey, thanks.

as i said i'm very green when it comes to vb.net...

i've done this..

Formstatus.statusbox.Multiline = True
Formstatus.statusbox.Text.????(std_out.ReadLine)

but i'm unsure what should proceed the .text ????? to allow input from std_out.readline

I bet it's something simple:rolleyes:

any ideas?
 
just change the second line to something like this:

Formstatus.statusbox.Text += std_out.ReadLine + "\r\n"


This should then give you the line output on seperate lines.


if needed send me the project to my trust and i'll take a look at it.
 
ok, have done as suggested...

it now returns all of the commands in the said batch file not just the 1 file(s) copied as needed,

the textbox now shows

r/ncopy c:\%1%.txt c:\temp\itworked.txtr/n 1 file(s) (have to press right arrow then) copied.r/nr/nrem call

idealy i'm after just the "1 file(s) copied" to be display and if there was anything further in the batch file like a c:\dir it needs to be on a sperate line.

code i've got is,

Formstatus.Show()
Formstatus.statusbox.Multiline = True
Formstatus.statusbox.Text += std_out.ReadLine + "r/n"


edit;

have changed to,

Formstatus.Show()
Formstatus.statusbox.Multiline = True
Formstatus.statusbox.Text = std_out.ReadLine

as .text = string is undelecred,

but now i get nothing in the text box at all!!

i'm lost!:confused:
 
ok, have done as suggested...

it now returns all of the commands in the said batch file not just the 1 file(s) copied as needed,

the textbox now shows

r/ncopy c:\%1%.txt c:\temp\itworked.txtr/n 1 file(s) (have to press right arrow then) copied.r/nr/nrem call

idealy i'm after just the "1 file(s) copied" to be display and if there was anything further in the batch file like a c:\dir it needs to be on a sperate line.

code i've got is,

Formstatus.Show()
Formstatus.statusbox.Multiline = True
Formstatus.statusbox.Text += std_out.ReadLine + "r/n"


edit;

have changed to,

Formstatus.Show()
Formstatus.statusbox.Multiline = True
Formstatus.statusbox.Text = std_out.ReadLine

as .text = string is undelecred,

but now i get nothing in the text box at all!!

i'm lost!:confused:


change this section to
Formstatus.Show()
Formstatus.statusbox.Multiline = True
Formstatus.statusbox.Text += std_out.ReadLine + "\r\n"


that will seperate the lines for you.

as for the second solution all you are doing is replacing the text in the textbox by what ever is in the std_out string so if there is nothing in the std_out then nothing will be copied over to the textbox line.
 
Last edited:
change this section to
Formstatus.Show()
Formstatus.statusbox.Multiline = True
Formstatus.statusbox.Text += std_out.ReadLine + "\r\n"


that will seperate the lines for you.

as for the second solution all you are doing is replacing the text in the textbox by what ever is in the std_out string so if there is nothing in the std_out then nothing will be copied over to the textbox line.

have done as suggested (copied and pasted) but still get;

\r\ncopy c:\%1%.txt c:\temp\itworked.txt\r\n 1 (then have to press right arrow at the end of text box to see) file(s) copied.\r\n\r\nrem call

?
 
try doing Formstatus.statusbox.Text += std_out.ReadLine + '\n' with single quotes not double. if its pasting \n into the box its not picking it up as a new line feed but treating it as a string.
 
right,

using '\n' will do nothing as ' will rem out the rest of that line.

I have been hitting google fairly hard and found that the \r\n is a c# command.

to do in vb.net you have to,

Imports Microsoft.VisualBasic.ControlChars (at top of form)

then use,

Formstatus.statuslist.Items.Add(std_out.ReadLine + Lf)

I have also gone back to the list box as i'm am making progress with it.

now when run, i get the whole output of the batch file (including the commands writen in like copy c:\.....) but on seperate lines!!!!!

only real issue now is that if a line is longer than the width of the list box you can't see it as there is no left to right scroll bar.

Also i'd really like not to see the commands in dos and only the "command completed successfully" in my status listbox.

we're getting there, but it a slow train!!!!

any ideas?

cheers guys - u all rock!!
 
set the horizontial scrollbar property to true.

sorry about the new line feed code. coming from a c# background I assumed that would work for vb.net as well.

at least you are getting there now.
 
Last edited:
right,

using '\n' will do nothing as ' will rem out the rest of that line.

I have been hitting google fairly hard and found that the \r\n is a c# command.

to do in vb.net you have to,

Imports Microsoft.VisualBasic.ControlChars (at top of form)

then use,

Formstatus.statuslist.Items.Add(std_out.ReadLine + Lf)

I have also gone back to the list box as i'm am making progress with it.

now when run, i get the whole output of the batch file (including the commands writen in like copy c:\.....) but on seperate lines!!!!!

only real issue now is that if a line is longer than the width of the list box you can't see it as there is no left to right scroll bar.

Also i'd really like not to see the commands in dos and only the "command completed successfully" in my status listbox.

we're getting there, but it a slow train!!!!

any ideas?

cheers guys - u all rock!!

\r\n is not a c# command its an escape sequence of carriage return followed by a new line feed which can be used in many languages, and yep just noticed that it cannot be used in vb because vb sucks :P. using ' ' (in c anyway) is just a way of defining whats enclosed as a character rather than a string which is " ".
 
Last edited:
you can use Environment.NewLine as well which will work out the correct line break char for you and insert it i.e. "billy" + Environment.NewLine

As for just getting the "files copied" bit you could redirect the output of the bat file to a logfile then read that in which would give you your desired result but have quite an overhead. You can use the Diagnostics.Process class as well and a quick google yielded this code:

Code:
Dim prs As New System.Diagnostics.Process()
prs.StartInfo.UseShellExecute = False
prs.StartInfo.FileName = "C:\mybat.bat"
prs.StartInfo.RedirectStandardOutput = True
prs.StartInfo.CreateNoWindow = True
prs.Start()
prs.WaitForExit()
Dim sr As System.IO.StreamReader = prs.StandardOutput()
output = sr.ReadToEnd()
 
right,

this is what i get displayed when run.



is there a way of getting rid of the [] that appears on each line that it drops down???

other wise i'm happy with the layout.

Next issue is just getting just the "commands completed" and having it update live as the bacth file runs.

cheers again guys, i really appreciate help and patience with me :)
 
Last edited:
cheers,

I've tried vbCrLf but that only seems to add an extra []!!

the line in question reads,

formstatus.statuslist.items.add (std_out.readline + vbCrLf))

I have also tried just Cr, Lf, and chrW(10)

but all to no avail!!!
 
so i managed to get shot of the [],

I removed everything but the (std_out.readline)

and it worked!!!

I still need to remove the commands run and only display the results, "live"

does anyone know how this can be done?

I've been thinking that it might be easier if i get shot of the batch file all together and just write the commands in to vb it's self.

That leaves me with another problem...

I'm unsure of to run these commands in vb, how do you load a command prompt and execute commands?
 
Back
Top Bottom