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:
sorry i'm very new to this.

Thanks for the reply.

Could you please be a bit more specific???

cheers again
 
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?
 
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.

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

?
 
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!!
 
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?
 
due to the nature of the commands that the batch file runs, (and my experience with vb.net) it'll be better to just get vb to run them and then pull the output back in.

I've managed to get it working but with one side-effect. It now appears that my listbox is only displaying the first line of the batch file.

For example, if i get the batch file to run dir on the root of my PC all i get displayed is "volume c:\ has no label"

I have;

Code:
dim i as integer
for i = 0 to 15
the display to listbox code here
next i

but this just multiplies the "volume c:\ has no label" 15 times on different lines.

any idea how i can get it display the rest of the dir command??

cheers again for the help.
 
so,

use while instead of for....

Ok. I've tried but tbh my knowledge is very limited.

I've googled but i'm not seeming to get anywhere.

Could you post just a small bit of code explaining how it would benefit me???

big ask i know. :-)

cheers dude
 
cheers,

so in that 'do somthing, i'd need to keep the loop going untill std_out has finished.

right?

that way it would keep reading untill the whole thing was finished and then display??

cheers again
 
hey cool,

I think i've got it,:p
Code:
While (Not std_out.EndOfStream)
            Formstatus.statuslist.HorizontalScrollbar = True
            Formstatus.statuslist.Items.Add(std_out.ReadLine)
            Formstatus.Show()
        End While
displays,

status-1.jpg





this works in the sense that i get all of the information writen inside the batch file, inculding the commands and thier results.

can this be filtered out so only the results ("commands completed") show up without outputting to a txt file and then back in again??

thanking you!!!:D
 
Last edited:
cheers for replying,

I have done as suggested but in stead of "command completed" I've replaced it with "1 files(s) copied." as this is what this batch file does. (just using as a test to get this form working before applying to others)

This doesn't throw any errors once run but doesn't display anything in the status box.

I have also attached the std_err that i have to a messagebox but that isn't showing any errors either.

Is this process just picking up the last line of the batch file or should it display the "1 file(s) copied." if it's present in the batch file output?

cheers again for the help and your patience dude!
 
Last edited:
not to worry,

I've decided to write the dos commands in to vb, and get each line as it runs to display, giving me the "live" output i require.

because i have an idea but need help I'll start a new thread!!!

cheers for all your help!

blastman
 
Back
Top Bottom