如何在运行时评估包含对 Windows 窗体控件的引用的字符串? [英] How To Evaluate a String Containing References to Windows Forms Controls At Runtime?

查看:24
本文介绍了如何在运行时评估包含对 Windows 窗体控件的引用的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VB.Net 中,如何评估字符串中引用的控件的值?假设您有一个包含一些字母数字内容的文本框 N,并且它在这样的字符串变量中被引用:

How can you, in VB.Net, evaluate the value of controls referenced in a string? Suppose you have a textbox N containing some alphanumeric content and it were referenced in a string variable like this:

Dim s As String = """Hello "" & N.Text & ""!"""

我希望能够重新评估代码中其他地方的 s 变量的内容(在那里没有直接知道 N 的存在),例如,像这样:

I'd like to be able to reevaluate the contents of the s variable elsewhere in the code (where there's no direct knowledge of the existence of N), for example, like this:

Dim e As String = Eval(s)

这在 VB.Net 中是不可能的;没有这样的 Eval 函数可用.我在网上看到了一些几乎合理的解决方案,例如使用旧的 COM ScriptControl:

This isn't possible with VB.Net; no such Eval function is available. I've seen some nearly plausible solutions online such as using the old COM ScriptControl:

Private Shared _scriptControl As MSScriptControl.ScriptControl = New ScriptControl()
_scriptControl.Language = "VBScript"
Dim e As String = _scriptControl.Eval(s)

但是,对 Windows 窗体控件的 N.Text 属性的引用与 ScriptControl 对象无关,因此会引发错误.

However, the reference to the N.Text property of a Windows Forms control is alien to the ScriptControl object, and therefore it throws an error.

是否有其他不需要购买第三方组件的快速修复方法?

Is there any other quick fix that won't require the purchase of a third-party component?

推荐答案

但是,对 Windows Forms 的 N.Text 属性的引用control 与 ScriptControl 对象不同,因此它抛出一个错误....我只提到了 ScriptControl 的想法来抵御可能涉及我提到的方法的答案,因为我已经尝试过,不想浪费我们的时间.

However, the reference to the N.Text property of a Windows Forms control is alien to the ScriptControl object, and therefore it throws an error. ... I only mentioned the ScriptControl idea to fend off answers that might involve that approach I mentioned since I already tried it and don't want to waste our time.

您可以将文本框添加到脚本控件.首先,使用N"的名称获取对N"的引用.不确定您是否已经获得了控件的名称;如果没有,您可能需要解析您的字符串以获取名称.然后使用 Controls.Find() 获取引用并将其传递给 AddObject().现在可以按预期评估您的字符串:

You can add the TextBox to the Script Control. First, get a reference to "N" using it's Name. Not sure if you've got the name of the control by itself already; if not, you may need to parse your string to get the name. Then use Controls.Find() to get a reference and pass that to AddObject(). Now your string can be evaluated as expected:

Private Shared _scriptControl As MSScriptControl.ScriptControl = New ScriptControl()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    _scriptControl.Language = "VBScript"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ctlName As String = "N"
    Dim ctl As Control = Me.Controls.Find(ctlName, True).FirstOrDefault
    If Not IsNothing(ctl) Then
        Try
            _scriptControl.AddObject(ctlName, ctl)
        Catch ex As Exception
        End Try

        Dim code As String = """Hello "" & N.Text & ""!"""
        Dim result As String = _scriptControl.Eval(code)
        MessageBox.Show(result)
    End If
End Sub

这篇关于如何在运行时评估包含对 Windows 窗体控件的引用的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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