how i can solve : cross thread access problem in visual basic coding? - vb.net

please check the attached picture.
i want to receive data from another application to Visual studio windows form through serial port and apply some conditions to the received data as shown in the picture, but unfortunately i got this error and i tried more to fix it but in vain.
I'm asking your kind help to over ride this problem and edit my code.
Thanks
Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim value As Byte = SerialPort1.ReadLine()
If (((value) & 2) <> 0) Then
Label4.Visible = True
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open()
End Sub

#Devcon: Don't use Invoke as it leads often to deadlocks, always use BeginInvoke instead (and I would use the methods and properties of the target control and not of the form, but that usually does not matter).
With Label4
If .InvokeRequired Then
.BeginInvoke(Sub() .Visible = True)
Else
.Visible = True
End If
End With

I think you cannot access label4 directly when using Serial Port Reading.
Try replacing Label4.Visible = True with:
if Me.InvokeRequired then
Me.Invoke(Sub() Label4.Visible = True)
else
Label4.Visible = True
end if

Related

Visual Basic FolderBrowser returning no value

I was looking to memorizing a folder path to my.settings indefinitely.
I tried
Private Sub CartellaTessutiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CartellaTessutiToolStripMenuItem.Click
FolderBrowserDialog1.ShowDialog()
End Sub
Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles FolderBrowserDialog1.Disposed
My.Settings.CartellaTex = FolderBrowserDialog1.SelectedPath
My.Settings.Save()
MsgBox(My.Settings.CartellaTex.ToString)
End Sub
My.settings.CartellaTex setting is set to String.
I am sure no value is returned as by setting up a msg box at the end of the text such message does not show.
All the functions in my app that rely on My.settings.CartellaTex go in error.
I have read somewhere that FolderBrowser struggles when something called "threads" are involved, but this code ran perfectly yesterday night. Keep in mind I am using Visual Studio on Parallels for Mac but again this used to work hours ago. I am really new to vb.net and this is as much knowledge I have. Please be patient.
Try this:
Private Sub CartellaTessutiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CartellaTessutiToolStripMenuItem.Click
If FolderBrowserDialog1.ShowDialog = DailogResult.OK Then
My.Settings.CartellaTex = FolderBrowserDialog1.SelectedPath
My.Settings.Save()
MsgBox(My.Settings.CartellaTex.ToString)
End If
End Sub

Get input for the Windows (Home) key pressed in VB.NET

So far, in my code I have written the following:
Private Sub KeyBox_Keypressed(sender As Object, e As EventArgs) Handles KeyBox.Keypress
If e.Keychar = "key.home" Then
nint_startmenu = True
leftpanel_startmenu.Visible = True
End If
End Sub
But it did not work. Please help me do this.
NOTE: This is open-source and free

VB.net Printform cant find path

So I am working on a form which need to be printed. I want to end up with a PDF file using Foxit PDF printer. The problem is that I cant figure out how to get the selected path as file location so I keep getting an the Path cannot be null.
error. Where in the code should I put my filelocation when using the Printform?
In this code the foldername is the location where I want to print.
Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click
Dim folderDlg As New FolderBrowserDialog
Dim foldername As String
folderDlg.ShowNewFolderButton = True
If (folderDlg.ShowDialog() = DialogResult.OK) Then
foldername = folderDlg.SelectedPath
Dim root As Environment.SpecialFolder = folderDlg.RootFolder
End If
PrintForm1.Print()
End Sub
Edit:
Actually deleted part of the code and still getting the same error (first part wasnt doing anything to start with I know that). All I am using now is:
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click
PrintForm1.Print()
End Sub
Also microsoft help database about Printform isnt helping since I have done exactly what it says and still getting the Path is Null error
Edit 2:
So I am using this code now and it is working.
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click
PrintDialog1.PrinterSettings = PrintForm1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then PrintForm1.PrinterSettings = PrintDialog1.PrinterSettings
With Me.PrintForm1
.PrintAction = Printing.PrintAction.PrintToPreview
Dim MyMargins As New Margins
With MyMargins
.Left = 10
.Right = 10
.Top = 10
.Bottom = 10
End With
.PrinterSettings.DefaultPageSettings.Margins = MyMargins
.Print()
End With
End Sub
but as soon as I try to set which area it should print I get the following error: "Printing is not a member of powerpacks". I tried using the following code according to microsoft this is the way it should work.. I have no clue where the error comes from
.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
You don't need a path to use printform. Printform just prints what you see on the screen to your default printer. You need to install "Visual Basic PowerPacks" to use this command. More explanation you may find here:
https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/printing/how-to-print-a-form-by-using-the-printform-component
To preview your print you don't need to use printdialog and all this. You just click on printform1 in the designer,to get up the properties window of printform1. In printaction you choose PrintToPreview. Thats all its needed.
These are all the lines I need:
Public Class Form1
Private Sub Exit_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
Private Sub Print_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintForm1.Print()
End Sub
End Class

Problems adding a picture in checkbox in checkedchanged event

Good morning everyone.
I´m trying to put an image in a checkbox when its status changes (checkedchanged event) and then, make a screenshot with this checkbox changed status, but, when event is fired, and screenshot is done, picture does not appear before next code executes.
I´m wondering if is there any way to make it like vb after update event.
Does anyone know how can I make it?
Thanks!!
My code:
Private Sub CheckBox_accept_terms_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_accept_terms.CheckedChanged
Try
If CheckBox_accept_terms.Checked Then
CheckBox_accept_terms.Image = My.Resources.cancelar
If moveTmpPic_finalPic() Then
If agreement_screenshot() Then
If generate_xml() Then
If generate_zip() Then
send_email_agreement = False
Common_functions.savedCapture = New Saved_capture_form
Common_functions.savedCapture.Show()
Me.Close()
End If
End If
End If
End If
End If
Catch
End Try
End Sub
I also tried:
Private Sub CheckBox_accept_terms_BackgroundImageChanged(sender As Object, e As EventArgs) Handles CheckBox_accept_terms.BackgroundImageChanged
Try
If CheckBox_accept_terms.Checked Then
If moveTmpPic_finalPic() Then
If agreement_screenshot() Then
If generate_xml() Then
If generate_zip() Then
send_email_agreement = False
Common_functions.savedCapture = New Saved_capture_form
Common_functions.savedCapture.Show()
Me.Close()
End If
End If
End If
End If
End If
Catch
End Try
End Sub
Private Sub CheckBox_accept_terms_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_accept_terms.CheckedChanged
CheckBox_accept_terms.BackgroundImage = My.Resources.cancelar
End Sub
You can add an Update() call, or a Refresh() call, to see if that helps.
Try:
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
CheckBox1.BackgroundImage = My.Resources.garbage
CheckBox1.Update()
'or use refresh
CheckBox1.Refresh()
End Sub
Update makes the control "update", and should force show your image.
If your problem is that the image is not displaying properly, it may be the layout of your background image. Stretch will make the image fit into the size of your control (rather than stay full-size).
CheckBox1.BackgroundImageLayout = ImageLayout.Stretch

Expression is not a method

Hello Coders Im trying to code a Anti virus and the following problem was here!
Im trying to add MsgBoxStyle.YesNo and will the problem in this...
So im trying to add a Question for delete file or no with MsgBox.
My code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If My.Settings.ifTmpR = True Then
For Each aF In Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp)
Try
MsgBoxStyle.YesNo()
MsgBox("Biztosan törli: " + aF)
IO.File.Delete(aF)
MsgBox("Temp kiürítése: " + aF)
MsgBox(aF + "kitötrölve")
Catch ex As Exception
End Try
Next
End If
If My.Settings.ifTmpR = False Then
Try
MsgBox("Kérlek kapcsold be a TEMP Queryt!")
Catch ex As Exception
End Try
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
My.Settings.ifTmpR = True
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = False Then
My.Settings.ifTmpR = False
End If
End Sub
End Class
And what the problem?
Help?
So you're coding an Anti-Virus application but you seem to have no basic knowledge about how to use methods and parameters.
You are supposed to include MsgBoxStyle.YesNo as one of the parameters to the MsgBox() method. You cannot call it like you do since it's simply an Integer value.
As Steve suggested you should use MessageBox.Show() instead, due to that MsgBox only exists for backwards compability.
The first parameter is the message you want it to show, and the second parameter is what title the window should have. They're just plain strings.
The third parameter however is the one you pass to tell the method what buttons to include in the MessageBox, and it should be passed like this:
MessageBox.Show("I am a message", "I am a title", MessageBoxButtons.YesNo)
This will show a message box with the buttons "Yes" and "No" inside it.
Now to get use out of this you should put your MessageBox in an If-statement, otherwise the application won't care if you press either Yes or No.
If MessageBox.Show("Are you sure you want to delete this file?", "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
'Delete code here.
End If
There. Now I suggest that you would have started with something like a "Hello World"-application instead of an Anti-Virus application, as Anti-Virus is pretty much harder than just showing a simple MessageBox.