如何关闭父窗体并打开子窗体?

如何关闭父窗体并打开子窗体?

问题描述:

嘿伙计们,在我只是隐藏父表单之前,但是现在当我尝试从父文件中读取时,它说它不能,因为它已经在一个进程中运行.我遵循了一些教程,它说要转到项目属性,并在所有表单关闭时让应用程序停止运行.

Hey guys before I was just hiding the parent form, but now when I try to read from the parent file it says it can't because it's already running in a process. I followed some tutorial and it said to go to the project properties and have the application stop running when all the forms are closed.

但是现在因为我这样做了,它说找不到目录可能是因为我正在读取父表单的输入.无论如何这是我的代码

But now since I did that it says the directory can't be found probably because I am reading the input from the parent form. Anyways here is my code

Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" + frmLogin.txtUser.Text))

我应该怎么做?

    Private Sub btnHunter_Click(sender As System.Object, e As System.EventArgs) Handles btnHunter.Click
    selection = "Hunter"
    writeData.classSelection()
End Sub

这是我点击按钮时的样子.

This is what I have when the button is clicked.

这是 classSelection 子:

Here is the classSelection sub:

    Public Sub classSelection()
    If frmClass.selection = "Hunter" Then
        writeFile1.WriteLine(frmClass.selection)
    End If
    If frmClass.selection = "Gatherer" Then
        writeFile1.WriteLine(frmClass.selection)
    End If
    If frmClass.selection = "Farmer" Then
        writeFile1.WriteLine(frmClass.selection)
    End If
    writeFile1.Close()
End Sub

错误指向这一行:

If frmClass.selection = "Hunter" Then

说找不到文件路径的一部分.

Saying part of the file path cannot be found.

好吧!

事实上,问题可能出在其他地方.

In fact, the problem could be somewhere else.

例如,我能够通过提供一个空字符串 [""] 作为以下任一值来重现您的异常:

For example, I was able to reproduce your exception by providing an empty string [""] as the value of, either :

frmLogin.txtUser.Text ' = ""
' or 
sLogin ' = ""
' or
txtUserFile ' = ""

事实上,我收到了找不到路径的一部分..."异常,因为 StreamWriter 无法读取/写入文件,因为我没有提供该文件的有效文件名.由于文件名参数是一个空字符串",为 StreamWriter 提供的路径只是代表一个目录而不是一个文件,并引发了异常.

In fact, I get the "Could not find a part of the path..." exception because the StreamWriter couldn'd read/write to a File, as I didn't provided a valid FileName for that file. As the filename parameter was an empty string "", the provided path for StreamWriter was just representing a directory instead of a file and an exception was raised.

现在,您应该在构建 StreamWriter 的新实例之前检查您是否有一个有效的路径,以确保您实际上指向的是一个文件?

Now, you should check wether you have a valid path before building a new instance of StreamWriter to get sure you are actually pointing to a File ?

Dim writeFile1 As StreamWriter
Dim MyEntirePath As String = "C:\Users\...\Accounts\" + frmLogin.txtUser.Text
MessageBox.Show(MyEntirePath) ' would be enough to be sure your path is correct

' Some test code here...
If everythingOK then ' create the StreamWriter...
    writeFile1 = New StreamWriter(MyEntirePath)
    ' ...
' ...

此外,创建流编写器并在代码的另一部分/方法中使用它并不是一个好主意.你永远不知道如果有一天,你会改变你的代码,而忘记在

Also, it's not a good idea to create your streamwriter, and use it in another part/method of your code. You never known if one day, you'll change your code, and forget to make the link between

Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" + frmLogin.txtUser.Text))

' plus
Private Sub btnHunter_Click(sender As System.Object, e As System.EventArgs)
    ...
End Sub

' plus
Public Sub classSelection()
    ...
    writeFile1.Close()
End Sub

^^ 太多这里和那里"...

如果您尝试单击 btnHunter 两次,显然也会出现异常.Exist(..) 检查,如果没有,则先创建文件,然后将其放入 Try/Catch 以检查我是否最终没有写入该目录的管理员权限.否则,请编写允许用户将文件读/写到自定义文件夹的代码.而且,你有:

You'll obviously also get an exception if you try to click btnHunter twice.. I don't know what is the purpose of your code nor how it works, it looks like a game.. But I would use File.Exist(..) checks, create the file before, if none, and put that in a Try/Catch to check if I eventually don't have administrator rights to write to that directory. Otherwise, make a code that allow user to read/write files to a custom folder. Andalso, you have :

Application.StartupPath

^^ 非常有用,比如:

^^ Very usefull, like :

Dim MyFilePath As String = Application.StartupPath + "\Datas\MyText.txt"

经过两周的编码后,我通常会忘记将那些C:\blabla.."或D:\gnagna\"放在哪里,或者哪些类实际上使用了这些绝对引用路径.自从我在另一台计算机上转移到 Win7 的那一天起,我很久以前就放弃了这种获取目录的方式,而我使用这种方法开发的所有此类应用程序都注定要失败......

After two weeks of coding, I usually forget where I put those "C:\blabla.." or "D:\gnagna\" or what classes actually uses those absolute reference paths. I've dropped this way of getting directories long ago since the day I moved to Win7 on another computer and all such applications I developped using that approach was doomed...