通用函数/子从模块填充vb.net组合框 [英] generic function/sub to fill a vb.net combobox from a module

查看:158
本文介绍了通用函数/子从模块填充vb.net组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在vb.net 2010的模块中使用泛型例程来填充组合框。我将子变量formname.comboboxname传递给2,运行查询,这是我无法去的地方进一步。我需要这样做,因为我有很多形式和组合框来填充,认为在一个模块中的通用方法会很好。



我可能会分解并为每个组合框执行一个数据集,但这当然应该起作用,并且将是我的首选。



在表单代码中,我使用--- loadComboBoxOrHelp(FrmArchive.cmbSrcType)调用它,这是在我的模块中,一些代码仅用于显示我迄今尝试的内容。当然,如果我取消注释,那么CTYPE上面的行会起作用,但不是通用的。正如你可以从函数的名字中猜出的,我想填充一个组合框或帮助文本字段,但是我还没有完成那个代码,我假设一旦组合框部分工作,我可以猜出文本框部分。该查询将是相同的,只是一个文本框,而不是组合框。



CTYPE是我的错误,我试图从模块做到这一点,因此它非常通用,如果我把它放在表格代码当然是有效的。 * *我拿出了SQL,因为它混淆了这个问题。我现在用loadComboBoxOrHelp(Me,cmbSrcType)来调用它。

  Public Sub loadComboBoxOrHelp(ByVal formName,ByVal boxName)
尝试
CType(formName.Controls(boxName),ComboBox).Items.Add(ABC)'myReader.GetString(lookupValue)。ToString)
Catch ex例外
MessageBox.Show(检索表查找记录时出错......& ex.Message,Lookups Table)
最后
如果conn.State = ConnectionState.Open然后conn.Close()
结束尝试
结束小组

结束模块

$ b $我认为你可以直接将ComboBox传递给函数,但如果你真的想使用一个字符串,那么你需要找到一种方法来检索 $ b

  Public Sub loadComboBoxOrHelp 中的 Form 和组合实例(ByVal boxName)
Dim words = boxName.Split(。)
Dim formName = Appl ication.OpenForms(words(0))
Dim cmbBox = formName.Controls(words(1))
......

作为一个侧面说明。使用参数化查询来构建sql命令,而不是字符串连接。另外,如果你完全控制了传递给sql命令的内容,最好避免引用/取消引用值

  conn.Open()
myCommand.Connection = conn
Dim query =SELECT name,lookupValue_
& FROM lookups_
& WHERE name = @bxname
myCommand.CommandText = query
myCommand.Parameters.AddWithValue(@ bxname,boxName)
myReader = myCommand.ExecuteReader()


trying to fill a combobox using a generic routine in a Module in vb.net 2010. I am passing to the sub the variable formname.comboboxname and splitting it into 2, running a query and this is where I can't go further. I need to do this because I have quite a few forms and comboboxes to fill, thought a generic method in one module would be great.

I may break down and do a dataset for each combobox but this certainly should work and would be my preferred choice.

in the form code I call it with --- loadComboBoxOrHelp("FrmArchive.cmbSrcType") and this is in my module, some code is commented just to show what I have tried so far. And of course the line above the CTYPE works if I uncomment it but then its not generic. As you can guess from the name of the function, i want to fill a combobox OR a help text field but I have not done that code yet, I assume once the combobox part works I can guess out the text box part. The query will be the same, just a text box instead of combobox.

The CTYPE is my error, I am trying to do this from a module so its very generic, if I put it in the forms code of course it works. ** I took out the SQL as it was confusing the issue. I am now calling it this with loadComboBoxOrHelp(Me, cmbSrcType)

Public Sub loadComboBoxOrHelp(ByVal formName, ByVal boxName)
    Try
        CType(formName.Controls(boxName), ComboBox).Items.Add("ABC") 'myReader.GetString("lookupValue").ToString)
    Catch ex As Exception
        MessageBox.Show("Error while retrieving records on table Lookups..." & ex.Message, "Lookups Table")
    Finally
        If conn.State = ConnectionState.Open Then conn.Close()
    End Try
End Sub

End Module

解决方案

Well, I think you could pass directly the ComboBox to the function, but if you really want to use a string then you need to find a way to retrieve the Form and combo instance from the name passed in

Public Sub loadComboBoxOrHelp(ByVal boxName)
    Dim words = boxName.Split(".") 
    Dim formName = Application.OpenForms(words(0))  
    Dim cmbBox = formName.Controls(words(1))
    ......

As a side note. Use a parameterized query to build sql commands, not string concatenation. Also if you are in full control of what is passed to the sql command it is better to avoid the quoting/unquoting of values

    conn.Open()
    myCommand.Connection = conn
    Dim query = "SELECT name, lookupValue " _
                & "FROM lookups " _
                & "WHERE name = @bxname"
    myCommand.CommandText = query
    myCommand.Parameters.AddWithValue("@bxname", boxName)
    myReader = myCommand.ExecuteReader()

这篇关于通用函数/子从模块填充vb.net组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆