Windows 窗体无法从我的数据库获取更新?

发布于 2024-10-31 19:51:19 字数 1370 浏览 1 评论 0原文

我最近才开始摆弄 Visual Basic Express 和 Sql 数据库。

我已经成功启动并运行了一个数据库,并且可以从中查询信息。我什至创建了一个表单,可以将新条目添加到我使用的表中。

第一个表单有一个组合框,其中列出了我的表中的玩家姓名。 Form2 允许您向表添加新名称,但我添加的任何内容都不会立即在 Form1 中更新。我必须重新启动程序才能看到新条目。即便如此,这些新条目似乎也不是永久性的,因为它们最终会消失。

我的 Form1 代码:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim db = New PlayerTestDataContextDataContext()
    Dim PlayerList = From List In db.Players
                     Select List.PlayerName

    For Each PName In PlayerList

        cbPList.Items.Add(PName)

    Next

End Sub


Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

    frmNewPlayer.Show()

End Sub

End Class

Form2 代码:

   Private Sub btnCPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCPlayer.Click

    Dim db = New PlayerTestDataContextDataContext()

    If txtNewPlayer.Text = "" Then
        lbWarning.Text = "Please enter a name!"
    Else
        Dim Plyr As New Player With {
            .PlayerName = txtNewPlayer.Text}
        db.Players.InsertOnSubmit(Plyr)
        db.SubmitChanges()
        Me.Close()

    End If

End Sub

不确定这里出了什么问题...任何帮助都会受到赞赏。如果我在这里忽略了一个明显的答案,请原谅我,我不确定我需要寻找什么。

Ive only recently starting fiddling with Visual Basic Express and Sql Databases.

Ive managed to get a database up and running, and can query information from it. I have even created a form that can add a new entry to the table im using.

The first form has a ComboBox that list the PlayerNames in my table. Form2 allows you to add a new name to the table, but anything I add isnt immediately updated in Form1. I have to relaunch the program to see the new entries. Even then, these new entries dont seem to be permanent as they eventually dissappear.

The code I have for Form1:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim db = New PlayerTestDataContextDataContext()
    Dim PlayerList = From List In db.Players
                     Select List.PlayerName

    For Each PName In PlayerList

        cbPList.Items.Add(PName)

    Next

End Sub


Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

    frmNewPlayer.Show()

End Sub

End Class

The code for Form2:

   Private Sub btnCPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCPlayer.Click

    Dim db = New PlayerTestDataContextDataContext()

    If txtNewPlayer.Text = "" Then
        lbWarning.Text = "Please enter a name!"
    Else
        Dim Plyr As New Player With {
            .PlayerName = txtNewPlayer.Text}
        db.Players.InsertOnSubmit(Plyr)
        db.SubmitChanges()
        Me.Close()

    End If

End Sub

Not sure what is going wrong here...any help is appreciated. If I have overlooked an obvious answer around here forgive me, Im not sure what I need to be looking for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半枫 2024-11-07 19:51:19

那应该可以解决问题。但你需要读一些书...

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click    
    if frmNewPlayer.ShowDailog() == DialogResult.Ok
        Dim db = New PlayerTestDataContextDataContext()
        Dim PlayerList = 
        From List In db.Players                     
        Select List.PlayerName  
        ' 
        cbpList.Items.Clear()
        '
        For Each PName In PlayerList
            cbPList.Items.Add(PName)    
        Next
    end if
End Sub

That should do the trick. But you need to do some reading ...

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click    
    if frmNewPlayer.ShowDailog() == DialogResult.Ok
        Dim db = New PlayerTestDataContextDataContext()
        Dim PlayerList = 
        From List In db.Players                     
        Select List.PlayerName  
        ' 
        cbpList.Items.Clear()
        '
        For Each PName In PlayerList
            cbPList.Items.Add(PName)    
        Next
    end if
End Sub
爱你不解释 2024-11-07 19:51:19

您实际上从未告诉 Form1 更新其列表。

这就是现在发生的情况:

  1. Form1_Load
    • 填充您的玩家列表。
  2. 用户按下 btnCreate (btnCreate_Click)。
    • btnCreate 启动 frmNewPlayer(我假设是“Form2”)。
  3. 用户按下 btnCPlayer (btnCPlayer_Click)。
    • btnCPlayer 将播放器插入列表中。
    • btnCPlayer 关闭 Form2。

至此,您已经完成了操作流程,但 Form1 从未收到添加新 Player 的提示。也没有任何代码可以使列表重新加载,除非 Form1 启动(Form_Load 事件/方法仅在第一次加载时调用)。

您需要(选择一个):

  • 将 Form2 作为“模态”窗口启动,这意味着它会阻止 Form1 执行任何操作,直到其关闭。这将允许您在 Form2 关闭后检查其内的更新。
  • Form2 通知 Form1 它已添加玩家(“事件”)的一种方式。
  • 设置一些东西来刷新列表(可能使用“计时器”)。

模态方法是最简单的。

You never actually tell Form1 to update its list.

This is what is happening now:

  1. Form1_Load
    • Populates your list of Players.
  2. User presses btnCreate (btnCreate_Click).
    • btnCreate launches frmNewPlayer (which I assume is "Form2").
  3. User presses btnCPlayer (btnCPlayer_Click).
    • btnCPlayer inserts the player in the list.
    • btnCPlayer closes Form2.

At this point, you are done with your flow of operation, but Form1 has never been tipped off that a new Player has been added. Nor is there any code that would make the list reload except when Form1 launches (the Form_Load event/method is only called for the first time it is loaded).

You need (pick one):

  • To launch Form2 as a "Modal" window, which means that it blocks Form1 from doing anything until it's closed. This would allow you to check for an update within Form2 after it's closed.
  • A way for Form2 to notify Form1 that it has added a player ("Event").
  • Setup something to refresh the list (probably using a "Timer").

The modal approach would be the easiest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文