How to use GetTempPath in VB.net - vb.net

I am not sure what I am doing wrong.
I viewed the Developer help page for this function but it doesn't have any examples
GetTempPath Help
Rght now I just want to print the temp directory to a msgbox to make sure I am doing it right. Then I will use it to write to a file
I am new to VB.net and am more familiar with C
Here is my code:
Imports System
Imports System.IO
Imports System.Collections
Public Class Form1
Public Shared Function GetTempPath() As String
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
...
...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tempFolder As String
tempFolder = GetTempPath()
MsgBox(tempFolder)
End Sub
The ellipses just mean there is code there that is unnecessary for the question

You need to remove your Function declaration for GetTempPath. This is causing you to use your function, not the System.IO.Path version. Since Path.GetTempPath is a Shared Function, you call it via Path.GetTempPath().
Your code should look like:
Imports System
Imports System.IO
Imports System.Collections
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
...
...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tempFolder As String
tempFolder = Path.GetTempPath()
MsgBox(tempFolder)
End Sub

Related

Using threading in VB.net

I'm having trouble using threading in a VB.net application I'm writing. I have a really simple version here to illustrate the problem I'm having.
It works perfectly if I load my form and have the VNC control connect to my VNC server as a single thread application using this code:
Imports VncSharp
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not RemoteDesktop1.IsConnected Then
Dim Host As String = "10.0.0.1"
RemoteDesktop1.Connect(Host)
End If
End Sub
End Class
In order to make the connection occur in the background, this is the code I've tried using the backgroundworker control. I followed the information in this article to handle referencing the controls on the form using an instance, but I still get an error when it tries to use the VNCSharp control (RemoteDesktop1) to connect to a VNC server at the line:
Instance.RemoteDesktop1.Connect(Host)
The error is:
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'RemoteDesktop1' accessed from a thread other than the thread it was created on.'
Here's the code:
Imports VncSharp
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form1
'Taken from https://stackoverflow.com/questions/29422339/update-control-on-main-form-via-background-worker-with-method-in-another-class-v
Private Shared _instance As Form1
Public ReadOnly Property Instance As Form1
Get
Return _instance
End Get
End Property
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
_instance = Me
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
System.Threading.Thread.Sleep(1000)
If Not Instance.RemoteDesktop1.IsConnected Then
Dim Host As String = "10.0.0.1"
Instance.RemoteDesktop1.Connect(Host)
'Connect()
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox("BG1 complete")
End Sub
End Class
I figured it out - one part of the code in the post I linked to on threading held the answer: I needed to use Invoke() to reference the form RemoteDesktop control:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
System.Threading.Thread.Sleep(1000)
If Not Instance.RemoteDesktop1.IsConnected Then
Me.Instance.Invoke(Sub()
Dim Host As String = "10.61.41.7"
Me.RemoteDesktop1.Connect(Host)
End Sub)
End If
End Sub
instead of just putting Instance.RemoteDesktop1.Connect(Host)

Visual Basic won't change regkey value

So, I've been working on my program for 3 hours now (I'm new to VB),
so I've made the program to change the desktop wallpaper when a button is clicked, and it works fine, I want that as soon as you run the program as an administrator, that it changes the OEMBackground value is changed from 0 to 1,
but it won't work, why?
Imports System.Environment
Imports Microsoft.Win32
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices
Public Class Form_Main
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
My.Computer.Audio.Play(My.Resources.Theme, AudioPlayMode.BackgroundLoop)
Dim ChangeValue As Integer
ChangeValue = 1
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background", "OEMBackground", ChangeValue)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Filepath As String
Filepath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Microsoft\Windows\Themes\TranscodedWallpaper.jpg"
My.Resources.Monika.Save("TranscodedWallpaper.jpg", Imaging.ImageFormat.Jpeg)
If My.Computer.FileSystem.FileExists(Filepath) Then
My.Computer.FileSystem.DeleteFile(Filepath)
End If
My.Computer.FileSystem.MoveFile("TranscodedWallpaper.jpg", Filepath)
End Sub
End Class

Trouble with OnLoad Sub (Visual Basic)

I have some trouble with declaring a default path file on startup.
Everytime I run the program, it's saying that pathFile is null.
Does someone know what I need to change in my code?
Imports System
Imports System.IO
Imports System.Text
Public Class GlobalVariables
Public Shared pathFile As String
End Class
Public Class Form1
Protected Overridable Sub OnLoad(e As EventArgs)
GlobalVariables.pathFile = My.Computer.FileSystem.SpecialDirectories.Desktop
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
' create or overwrite the file
Dim fs As FileStream = File.Create(GlobalVariables.pathFile)
' add text to file
Dim info As Byte() = New UTF8Encoding(True).GetBytes(rtbText.Text)
fs.Write(info, 0, info.Length)
fs.Close()
End Sub
End Class
Thanks in advance!
- Xaaf Code
Instead of trying to override OnLoad (which would be Overrides instead of Overridable), I would handle the load event:
Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
GlobalVariables.pathFile = My.Computer.FileSystem.SpecialDirectories.Desktop
End Sub
You could probably just set the value where pathFile is declared instead:
Public Class GlobalVariables
Public Shared pathFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop
End Class

How to handle an button click even from a .dll

Could someone please guide me as to how I would get this actually working? Currently it gives me errors about WithEvents - although simplified it shows the form - but I have no clue what that actually means. It's an toolbox I'm making just to allow the user to better interact with some of my other code.
All I need is the form visuals/guts to be custom, but then the code to be done within my application (which doesn't have visual editing capabilities).
Is this a case where I need to use interfacing/partial classes/inheritance, or can this easily be accomplished with just some minor tweaking to what I have?
(Form created in visual studio form designer and then changed to a class library. Application code written in Autodesk Inventor "rule" environment)
Thanks!
Application code:
AddReference "C:\Users\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\bin\Release\SectionSymToolBox.dll"
Imports System.Windows.Forms
Public Class SectionSymRule
'Public dlg As New System.Windows.Forms.For
Public Shared ToolBox As New SectionSymToolBox.SectionSymToolBox
Dim WithEvents EClass As New EventClass
Sub Main()
ToolBox.Show()
End Sub
End Class
Form code:
Public Class SectionSymToolBox
Private Sub Main()
End Sub
Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Swap Symbols
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Flip Symbol
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Flip Text
End Sub
Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'<
End Sub
Public Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'>
End Sub
End Class
Is this what you are looking for?
AddHandler ToolBox.Button1.Click, AddressOf HandlerMethodHere
Where HandlerMethod is a method that has the same signature as Button1.Click so for example:
Private Sub HandleButton1Click(sender As Object, e As EventArgs)
'Code here
End Sub
Obviously, replace Button1 with the name of the button.

HALCON error #8721: List of bar code models is empty

I have an error in my simple project. I use vb.net 2008 and the full error text is:
[HALCON error #8721: List of bar code models is empty at HALCONXLib.HBarCodeXClass.FindBarCode(HImageX Image, Object CodeType, Object& DecodedDataStrings)]
And my code is:
Option Strict Off
Option Explicit On
Imports HalconDotNet
Imports Microsoft
Imports System.Text
Imports System.IO
Imports System.Media
Imports System
Imports Microsoft.VisualBasic
Imports HALCONXLib
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim htu As HTupleX = New HTupleX
Dim reader As HBarCodeX = New HBarCodeX()
Dim Image As HImageX = New HImageX
Call Image.ReadImage("d:\barcode\image1.JPG")
Dim regions As HRegionX = New HRegionX
Try
regions = reader.FindBarCode(Image, "EAN-13", htu)
Catch ex As Exception
RichTextBox1.Text = ex.ToString
End Try
End Sub
End Class
I guess the call to
void HBarCodeX.CreateBarCodeModel(
[in] VARIANT GenParamNames, [in] VARIANT GenParamValues)
is missing to actually create the Reader.