使用VB.NET非常生疏 [英] Very rusty using VB.NET
问题描述
我不确定我做错了什么,但现在好了。
最近我下载了一个名为Active Query Builder的控件的试用版。我查看了一个名为GeneralDemo.vbproj的项目并开始查看其结构。
它包含一个很好的可视化基本程序来启动应用程序,所以我重复使用它。 br />
I am not sure what I am doing wrong, but here it goes.
Recently I downloaded a trial version of a control called Active Query Builder. I looked at one project called GeneralDemo.vbproj and started looking at its structure.
It contained a nice visual basic program to start the application, so I reused it.
'Nice Start up routine from Active Query Builder
Imports System.Threading
Imports System.Windows.Forms
Friend NotInheritable Class Start
Private Sub New()
End Sub
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread> _
Friend Shared Sub Main()
' Catch unhandled exceptions for debugging purposes
AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf CurrentDomain_UnhandledException
AddHandler Application.ThreadException, _
AddressOf Thread_UnhandledException
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
'The following is were we start the application
Application.Run(New AccessConnectionForm())
End Sub
Private Shared Sub CurrentDomain_UnhandledException(sender As Object, _
e As UnhandledExceptionEventArgs)
Dim exception As Exception = TryCast(e.ExceptionObject, Exception)
If exception IsNot Nothing Then
Dim exceptionDialog As New ThreadExceptionDialog(exception)
If exceptionDialog.ShowDialog() = DialogResult.Abort Then
Application.[Exit]()
End If
End If
End Sub
Private Shared Sub Thread_UnhandledException(sender As Object, _
e As ThreadExceptionEventArgs)
If e.Exception IsNot Nothing Then
Dim exceptionDialog As New ThreadExceptionDialog(e.Exception)
If exceptionDialog.ShowDialog() = DialogResult.Abort Then
Application.[Exit]()
End If
End If
End Sub
End Class
演示程序还包含不同数据库系统的不同连接形式。为了好玩,我想放置一个图像来向我展示我尝试连接的数据库。所以我在表单上放了一个图片框,然后我尝试将.PNG文件加载到图片框中。
The demo program also contained different connection forms for different database systems. For fun, I wanted to place an image to show me to which database I was trying to connect. So I placed a picture box on the form and I TRIED to load the .PNG file into the picture box.
Imports System.Data.OleDb
Imports System.Windows.Forms
Partial Public Class AccessConnectionForm
Inherits Form
Public strAppPAth As String = Application.StartupPath
Public ConnectionString As String = ""
Public Sub New()
InitializeComponent()
picDB.Image = Image.FromFile(strAppPAth & "MS-Access.png")
End Sub
Private Sub btnConnect_Click(sender As Object, _
e As EventArgs) Handles btnConnect.Click
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0"
ConnectionString += ";Data Source=" & txtDB.Text
ConnectionString += ";User Id=" & txtDB.Text
ConnectionString += ";Password="
If txtDB.Text.Length > 0 Then
ConnectionString += txtDB.Text & ";"
End If
' check the connection
Using connection As New OleDbConnection(ConnectionString)
Me.Cursor = Cursors.WaitCursor
Try
connection.Open()
Catch ex As System.Exception
MessageBox.Show(ex.Message, "Connection Failure.")
Me.DialogResult = DialogResult.None
Finally
Me.Cursor = Cursors.[Default]
End Try
End Using
End Sub
Private Sub btnBrowse_Click(sender As Object, _
e As EventArgs) Handles btnBrowse.Click
If openFileDialog1.ShowDialog() = DialogResult.OK Then
txtDB.Text = openFileDialog1.FileName
End If
End Sub
End Class
我在strAppPath变量上遇到错误。我试图让程序可以从任何地方执行,它无法识别我当前存储图像的Resources文件夹。
我尝试过:
我唯一添加的是:
I get an error at the strAppPath variable. I am trying to make the program executable from anywhere and it does not recognize the Resources folder where I am currently storing the image.
What I have tried:
The only thing I added was:
Public strAppPath As String = Application.StartupPath
在Access Connection表单的顶部。和 -
At the top of the Access Connection form. and -
picDB.Image = Image.FromFile(strAppPAth & "MS-Access.png")
在表单启动时的Public Sub New过程中。
有人能指出我做错了吗?
谢谢你,
In the Public Sub New procedure when the form is started.
Can some one point out what I did wrong?
Thank you,
推荐答案
查看您创建的路径
picDB.Image = Image.FromFile(strAppPAth&MS-Access.png )
strAppPAth是否有正确的斜杠?
使用System.IO.Path.Combine(string ,string)将路径组合到文件名。 Combine()将正确修复斜杠。
Look at the path you create in
picDB.Image = Image.FromFile(strAppPAth & "MS-Access.png")
Does strAppPAth have the correct slashes?
Use System.IO.Path.Combine(string, string) to combine the path to the filename. Combine() will properly fix the slashes.
我已经弄清楚我做错了什么。
1.右键单击解决方案资源管理器中解决方案的名称,然后单击属性。
2.单击资源选项卡,添加解决方案所需的资源。
3.在您需要资源的表格中,在可能的情况下,图片框添加以下代码。
I have figured out what I did wrong.
1. Right click on the name of your solution in solution explorer and click on Properties.
2. Click on the Resources Tab and add the resources your solution requires.
3. In the form where you need the Resource, in may case a picture box add the following code.
PictureBox1.Image = My.Resources.<Image Name>
知道它必须简单,但是我的编码技巧因缺乏使用而萎缩。
Knew it had to be something simple, but my coding skills have atrophied from lack of use.
这篇关于使用VB.NET非常生疏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!