新建一个项目,添加现有项
从“添加现有项”对话框中选择上一小节编译出来的resource.dll,
窗口设计界面:
具体代码:
1、窗体代码引用System.Reflection命名空间:
2、建立两个窗体级的变量用来记录使用的字符串和图片的编号:
Dim picIndex As Integer Dim strIndex As Integer
3、当窗体载入的时候,使编号为1
Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load picIndex = 1 strIndex = 1 End Sub
Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim myResource As Assembly myResource = Assembly.LoadFile(Application.StartupPath & "\" & "resource.dll") Dim myResourceManager As System.Resources.ResourceManager myResourceManager = New System.Resources.ResourceManager("resource.Resource1",myResource) Dim myPic As Bitmap Dim resourceName As String resourceName = "Pic" & picIndex If picIndex = 1 Then picIndex = 2 Else picIndex = 1 myPic = myResourceManager.GetObject(resourceName) PictureBox1.Image = myPic End Sub
5、字符串资源的调用
Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click Dim myResource As Assembly myResource = Assembly.LoadFile(Application.StartupPath & "\" & "resource.dll") Dim myResourceManager As System.Resources.ResourceManager myResourceManager = New System.Resources.ResourceManager("resource.Resource1",myResource) Dim myString As String Dim resourceName As String resourceName = "String" & strIndex If strIndex = 1 Then strIndex = 2 Else strIndex = 1 myString = myResourceManager.GetString(resourceName) TextBox1.Text = myString End Sub
运行结果如图:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录