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

Vb.net 如何关闭父窗体并打开子窗体?,vb.net,Vb.net,嘿,伙计们,以前我只是隐藏父窗体,但现在当我尝试从父文件读取时,它说不能,因为它已经在进程中运行。我遵循了一些教程,它说当所有表单都关闭时,转到项目属性并让应用程序停止运行 但现在,因为我这样做了,它说找不到目录,可能是因为我正在读取来自父窗体的输入。无论如何,这是我的密码 Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Pro

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

但现在,因为我这样做了,它说找不到目录,可能是因为我正在读取来自父窗体的输入。无论如何,这是我的密码

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

编辑:

这是我点击按钮时得到的

下面是classSelection子项:

    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

表示找不到部分文件路径。

如果要读取封闭父窗体中的输入文本框,必须声明public var

在项目中创建新模块。。再加上这个

public sLogin as String
在隐藏或关闭frmLogin之前。。加上这个

sLogin =  txtUser.Text
因此,您可以使用

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

matzone给了你一个很好的提示。要准确检查路径,只需使用变量添加MessageBox:

Dim writePath1 As String
Dim writeFile1 As StreamWriter

writePath1 = "C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & sLogin
If MessageBox.Show(writePath1, "Continue ?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
    writeFile1 = New StreamWriter(File.OpenWrite(writePath1))
    ' ...
    writeFile1.Close() ' Very important ! Adrian pointed it out.
End If
^^如果有效,您可以放弃对话框测试,或者用一些测试代码替换它,比如if File.Exists(…)

但是,我不明白您是要关闭父窗体还是隐藏父窗体。不一样! 关闭父窗体将放弃对父窗体成员的任何访问,包括txtUser.Text

如果要关闭父窗体,则子窗体不应是要关闭的父窗体的子窗体,或者必须隐藏父窗体:

frmLogin.Hide() ' Not frmLogin.Close()
MyChildForm.TxtUserFile = Me.txtUser.Text
' Me.Close() ' This will definately KILL Form1 (the parent one)
Me.Hide() ' Always use Hide() until you'll terminate your Application
MyChildForm.Show()
MyChildForm.LoadFile()
如果关闭frmLogin,将无法访问frmLogin.txtser,或者使用matzone提供的sLogin。或者,您应该将frmLogin.txtUser.Text值传递给ChildForm的自定义属性

Imports System.IO
Public Partial Class ChildForm1
    ' Inherits System.Windows.Form
    ' ...
    Private _txtUserFile As String

    Public WriteOnly Property TxtUserFile() As String
        Set(ByVal NewFileName As String)
            _txtUserFile = NewFileName
        End Set
    End Property

    Public Sub LoadFile()
        Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & txtUserFile))
        ' ...
        writeFile1.Close()
    End sub
    ' ...
End Class
然后在父窗体中使用此选项:

frmLogin.Hide() ' Not frmLogin.Close()
MyChildForm.TxtUserFile = Me.txtUser.Text
' Me.Close() ' This will definately KILL Form1 (the parent one)
Me.Hide() ' Always use Hide() until you'll terminate your Application
MyChildForm.Show()
MyChildForm.LoadFile()
^^但这也不是一个好代码!你的问题还不清楚(至少对我来说)
“仍然说找不到路径的一部分”,然后检查路径

  • 该文件实际存在吗
  • 路径是否包含小故障?(使用提供的MessageBox测试)
  • 您的帐户可以访问该目录吗?(Windows配置和帐户级别)
    • 好吧

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

      例如,我可以通过提供一个空字符串[“”]作为的值来复制您的异常,该值可以是:

      frmLogin.txtUser.Text ' = ""
      ' or 
      sLogin ' = ""
      ' or
      txtUserFile ' = ""
      
      事实上,我得到了“找不到路径的一部分…”异常,因为StreamWriter无法读取/写入文件,因为我没有为该文件提供有效的文件名。由于filename参数是一个空字符串“”,因此为StreamWriter提供的路径只是表示一个目录,而不是一个文件,因此引发了一个异常

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

      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)
          ' ...
      ' ...
      
      另外,创建streamwriter并在代码的另一部分/方法中使用它也不是一个好主意。你永远不会知道,如果有一天,你会改变你的代码,并忘记使之间的链接

      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两次,显然也会出现异常。。我不知道你的代码的目的是什么,也不知道它是如何工作的,它看起来像一个游戏。。但我会使用File.Exist(..)检查,在之前创建文件,如果没有,则将其放入Try/Catch中,以检查我是否最终没有写入该目录的管理员权限。否则,请编写一个代码,允许用户将文件读/写到自定义文件夹。还有,你有:

      Application.StartupPath
      
      ^^非常有用,比如:

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

      经过两周的编码,我通常会忘记我把那些“C:\blabla..”或“D:\gnagna\”放在哪里,或者什么类实际使用这些绝对引用路径。自从我在另一台计算机上使用Win7的那一天起,我就放弃了这种获取目录的方法,我用这种方法开发的所有应用程序都注定要失败……

      哪一种是父窗体。。还是?frmLogin是父窗体@马特森真正的问题是什么。。关闭父窗体还是读取输入窗体父窗体?我想关闭父窗体并从父窗体的文本框中读取输入@MatzoneOn当您使用
      writeFile1
      写入文件后,是否调用
      writeFile1.Close()
      ?仍然说它找不到路径的一部分。我正在WriteUserDataClassNope中声明它仍然是一样的@Matzone找不到路径的一部分。。你从哪里得到路径?例外的确切措辞是什么?你所说的,似乎不是OpenWriteI的一个记录在案的例外。我在最初的问题中添加了一些东西。你能查一下吗@fsintegral@Programming16:添加了与显示或隐藏表单无关的新答案。。很抱歉,回复太晚,我是stackoverflow的新手,没有注意到通知。。