【问题标题】:How to refresh form after selecting value from combobox从组合框中选择值后如何刷新表单
【发布时间】:2015-02-13 06:29:41
【问题描述】:

我想知道在从组合框中选择另一个值后是否可以刷新当前的窗体,以便在其他几个文本框中显示该项目的详细信息?

所以我的桌子看起来像 表名:程序 程序 ID 程序名称 程序描述 1 T1 描述1

这是我使用 atm 的代码

Dim connection As New SqlClient.SqlConnection
    connection.ConnectionString = "pathway"
    Dim dr As SqlDataReader
    Dim prognamedesc As String
    Dim filetypetxt As String

    Dim prognamecombo As String
    Dim filetypecombo1 As String
    Dim command As New SqlCommand
    Dim querycommand As New SqlCommand
    connection.Open()
    'THIS SECTION LOADS DATA FROM THE TABLES'
    Try

        command.Connection = connection
        command.CommandType = CommandType.Text
        command.CommandText = "select program_name,filetype from program order by program_name; select * from filetype"
        querycommand.Connection = connection
        querycommand.CommandType = CommandType.Text
        querycommand.CommandText = "select program_name,program_desc , filetype from program where program_name like" & FiletypeComboBox1.SelectedItem & ""

        dr = command.ExecuteReader

        While dr.Read()

            prognamecombo = dr(0)
            Program_nameComboBox.Items.Add(prognamecombo)

        End While

        dr.NextResult()
        While dr.Read()
            filetypecombo1 = dr(0)
            FiletypeComboBox1.Items.Add(filetypecombo1)
            FiletypeComboBox1.SelectedItem = filetypecombo1
        End While
        dr.NextResult()
        While dr.Read()
            filetypetxt = dr(0)
            FiletypeLabel1.Text = filetypetxt

        End While
        dr.NextResult()
        While dr.Read()
            prognamedesc = dr(0)
            Program_descTextBox.Text = prognamedesc
        End While

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try





    connection.Close()

我想知道使用当前代码是否可行?

【问题讨论】:

  • 你所追求的可能是一个事件过程。更改组合框选择后触发的过程。应该有一个事件你可以查到它叫做_Changed() Combobox 控件的事件。
  • 它是我创建另一个私有子并将结果传递到输出区域的地方吗?据我了解,一旦我从组合框中选择了一个项目,页面就需要使用与组合框中的项目相关联的信息进行刷新。我尝试过重新查询,但不知何故它根本没有出现。 me.refresh 也不做任何事情。
  • Google it :) 有大量关于 VBA 事件过程的信息和教程
  • 在我决定问之前已经在 Google 上待了几个小时。我在 Youtube 上看过一个例子,但是它需要我改变程序的整个结构。
  • 我在想这是否可行 检测是否已单击组合框 以某种方式发送新查询并显示结果。

标签: sql vb.net vba localdb


【解决方案1】:

要实现这一点,您必须做两件事,首先将所有代码放在一个方法中,然后将其命名为 RefreshForm() 之类的名称

public void RefreshForm()
{
  // your code and binding goes here
}

第二步是在组合框上使用选定的索引更改事件,您只需调用包含所有绑定代码的方法。

【讨论】:

  • 对不起,我很不擅长这个。放置在表单加载时立即加载的子代码中的大量代码的当前位置。那么我是否只是将其复制并放置在该 refreshform() 类中,然后将选定的索引更改事件放置在主加载区域中?
  • 非常感谢!有效!现在,当我在组合框中添加新条目时,我必须找到一种重新查询页面的方法。
  • 您不需要加倍工作,在加载事件时调用相同的函数 RefreshForm()。在这种情况下,您可以在一个地方维护所有内容
猜你喜欢
  • 1970-01-01
  • 2012-05-01
  • 2016-01-30
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 2018-02-22
  • 2014-01-07
相关资源
最近更新 更多