如何在VBNET中使用动态变量

时间:2013-01-05 23:56:06

标签: vb.net visual-studio visual-studio-2012 dynamic-variables my.resources

如何将按钮的文本用作资源名称?

我的尝试:

Public Sub LlamadaButton(ByVal sender As Object, ByVal e As System.EventArgs)
   Dim ButtonN As Button = CType(sender, Button)
   Dim ResourceName = ButtonN.Text
   ReadDelimitedText(My.Resources._(ResourceName), ";")     ' The problem: My.Resources._(ResourceName)
   End Sub

错误:预期的标识符

3 个答案:

答案 0 :(得分:3)

使用My.Resources.ResourceManager.GetString方法。

Dim ResourceName = ButtonN.Text
ReadDelimitedText(My.Resources.ResourceManager.GetString("_" & ResourceName), ";")     

答案 1 :(得分:1)

<script runat="server">
Public Sub LlamadaButton(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim ButtonN As Button = CType(sender, Button)
  Dim ButtonText = ButtonN.Text
  MsgBox(ButtonText)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button Bob" />
    </div>
  </form>
</body>
</html>

这是什么意思?,点击按钮会在消息框中显示文本,尝试更改按钮文本并再次单击

答案 2 :(得分:1)

假设您的资源文件名为Resource1.resx,并且其中的字符串为String1,您应该可以像这样访问它:

My.Resources.Resource1.String1