phoonix

hi, I have a problem when I try to use 2 forms. I think is declare my form wrong but I don't find the problem. I made frmMain en frmInstall.

Another problem is that I want to make a new directory. I there a function possible in visual basic express edition for doing this

thanks



Re: Visual Basic Express Edition making a new directory and using 2 forms

Derek Smyth

Hi,

Could you post some code that demonstrates how your declaring and working with your forms.

You can use System.IO.Directory class to create a directory. The code below demonstrates how you check that the directory doesn't already exist, how to create it, and when it is created how you can then get a reference to the created directory (a DirectoryInfo object)

Code Snippet

If System.IO.Directory.Exists("C:\MyDir") = False Then

Dim dir As System.IO.DirectoryInfo

dir = System.IO.Directory.CreateDirectory("C:\MyDir")

End If






Re: Visual Basic Express Edition making a new directory and using 2 forms

phoonix

Code Snippet

Dim frmInstall As New frmMain

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

frmInstall.Show()

Me.Hide()

End Sub

I used this code.

Another question: can you check how many lines there are in a textfile

thanks





Re: Visual Basic Express Edition making a new directory and using 2 forms

Feng Chen - MSFT

Hi phoonix,

Till now, you haven't tell us what exactly problem you met. Coule you please cover your problem about the two forms in more detail

And you can count the number of lines in a specified file like the code below:

Code Snippet

Private Sub btnTest_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As String = "D:\Transfer\1.txt"
Dim reader As StreamReader = File.OpenText(fileName)
Dim lines As Integer = 0
Do While (Not reader.ReadLine Is Nothing)
lines += 1
Loop
Console.WriteLine("There are {0} lines in file - {1}", lines, fileName)
End Sub

Hope this can help you!

Thanks!






Re: Visual Basic Express Edition making a new directory and using 2 forms

phoonix

http://farm2.static.flickr.com/1418/544420809_9d7a541f6d_o.jpg

this is a screenshot of the error I get.





Re: Visual Basic Express Edition making a new directory and using 2 forms

bdbodger

If the form creates an instance of it's self with that line at the top first thing it will do in the new form is the same thing over and over again each new form creating an new instance of the original form . I think you need to create a separate form and create an instance of that form .



Re: Visual Basic Express Edition making a new directory and using 2 forms

phoonix

But how is it possible to do that How do you make a 'seperate' form. I made a form, just by clicking on new - windows form. Isn't that a separate form And how do you open then that form, without any errors



Re: Visual Basic Express Edition making a new directory and using 2 forms

Derek Smyth

Hi,

That was an excellent error you had, bdbodger is right you have an infinite loop, I have never seen an infinite loop caused that way before, it's a nice example. Your right with how your creating the form your just making frmMain create another frmMain at a time that causes an infinite loop. It's like you create form A which creates another form A that creates another form A that creates another form A that creates another form A never ending. If you were to instead create form A that creates form B then the error wont happen.

If you wanted frmMain to create another frmMain then you can do it like this...

Code Snippet

Public Class frmMain

Dim main as frmMain

Public Sub New()

InitalizeComponents 'automatically generated do not remove

main = new frmMain()

main.show()

End Sub

End Class

What you've experienced with this error is a programming technique called recursion. With recursion you can get a method to call itself, it's a techinque you use when you need to deal with complex data structures that have an unknown number of items, like visiting all files in all sub directories. The only problem with rescursion is infinite loops, and thats what has happened to you here.






Re: Visual Basic Express Edition making a new directory and using 2 forms

bdbodger

You can also have form A create a new form B and call it form C and never actually show form B or the original of form B just show form C . Form B would remain the way you designed it so you can create new instances of form B each one a copy of the original without any changes from the way it was designed . You would be working with form A and form C in your code and form B would not contain any code that creates it's self that would be in form A .



Re: Visual Basic Express Edition making a new directory and using 2 forms

phoonix

The code works now, but I don't succeed in getting the second form in front of the first. How is it possible to do that



Re: Visual Basic Express Edition making a new directory and using 2 forms

bdbodger

set the topmost property in the form or perhaps set the focus to the new form or if needed use the showdialog method .