1 contextmenu for 59 labels (MouseDown Events) - vb.net

i got 59 labels (they describe places in a storage system). With a context menu i want to lock or unlock each of those places for the user.
So in my contextmenu i got two items: Lock and Unlock.
If one label (place) is locked by the user it should send an bit to a plc (but that is not my main problem), and the same for unlock.
Is there any opportunity to do a loop, to find out on which label the contextmenu has been opened? (MouseDown Event)
Here my code so far:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.ContextMenuStrip = ContextMenuStrip1
End Sub
Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown, Label2.MouseDown, Label3.MouseDown, Label4.MouseDown, .............. , Label59.MouseDown
ContextMenuStrip1.Tag = sender
End Sub
Private Sub LockToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LockToolStripMenuItem.Click
'send bit to PLC (sender)
End Sub
Private Sub UnlockToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UnlockToolStripMenuItem.Click
'send bit to PLC (sender)
End Sub
End Class
Sorry for the bad english.

Related

Trouble switching between projects/forms in vb.net

I am working on a project in Visual Studio Community 2019 using VB.Net. I am developing a user interface wherein users start on one form (a sort of index, call it form 1) and select from their the next form they want to go to (say form 2). This part is working fine. The trouble is that after users finish in form 2, they should be directed back to form 1 to continue from here, and I can't get this to happen. I need somehow to make the main menu a public shared form so that the other projects can access it but I can’t figure out how to do that.
Here are the relevant lines from form 1:
Public Class frmMainMenu
Public Shared inst As frmMainMenu
Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
inst = Me
Timer1.Start()
Me.Show()
End Sub
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles
btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
formNew.Show()
Timer1.Stop()
Me.Hide()
End Sub
And here are the relevant lines from form 2. I want lines 23 and 24 to return the user to the main menu, but it doesn't work.
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
Me.Show
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles
btnMainMenu.Click
inst.frmMainMenu.Show()
inst.frmMainMenu.Timer1.Start()
Me.Close()
End Sub
I appreciate any help you can offer!
From my comments above:
When you create your instance of your second form called "formNew",
use the AddHandler command to wire up its FormClosed event to a method
in the main form. Then in the second form you can just call Me.Close()
and the method in the main form will fire where you can call
Me.Show()..
In main menu:
Public Class frmMainMenu
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
Timer1.Stop()
Me.Hide()
formNew.Show()
End Sub
Private Sub formNew_FormClosed(sender As Object, e As FormClosedEventArgs)
Me.Show()
Timer1.Start()
End Sub
End Class
In frmGeologyMenu:
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
End Class
In VB.NET, your startup form is always the default instance of its type. That means that you can simply use the type name to refer to your startup form anywhere in your app. If you use Me.Hide() in your startup form then frmMainMenu.Show() can be used anywhere to redisplay it. This would not be the case in C# as default form instances are a VB-specific feature. See here for more info.

Access Main thread data from the Background thread

I´m using a backgroundworker to handle processing of data while the user is still free to for instance click another button that aborts the process.
However, the code in backgroundworker requires several pieces of data, for instance it needs to know whether a radio button is checked.
Is there a way to access data in another thread from the background thread? Or should I create global variables that hold this information?
Public Class Test
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Content = "Working..."
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If RadioButton1.IsChecked Then
MsgBox("It works")
End If
End Sub
End Class
You can pass the RadioButton checked state to the RunWorkerAsync method like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync(RadioButton1.Checked)
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim checked As Boolean = CBool(e.Argument)
If checked Then
MessageBox.Show("It works")
End If
End Sub
Let me know if you need to be checking the RadioButton continuously from inside a loop, as that would be different.

How to Block Key Board input in VB.net

I want to block the key board input when the form starts, but it is not working, please check my code:
Public Class Form2
Public Declare Function BlockInput Lib "User32" (ByVal fBlock As Integer) As Integer
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim name As String
name = "12345"
If (name = TextBox1.Text) Then
Me.Close()
End If
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Visible = True
TextBox1.Focus()
End Sub
Private Sub TextBox2_Click_1(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox2.MouseClick
BlockInput(1)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
End Class`
I used "groupbox.enabled = False" in order to stop program users from editing anything within the groupbox on my form. You can then enable the groupbox again providing certain conditions are met. Another method I've used in my programming for VB.net was setting the read-only property (I used to to auto-populate the system date to prevent users from entering false dates) I don't know if either of those methods might help you?

Changing the background color for Form2

i have two RadioButtons one for Light Blue and the other for Ghost White and a button to show the next Form (Form2)
i want to be able to check on a Radio Button and the backcolor of form2 changes to the checked Radio Button
this is what i have on my coding so far
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
Private Sub rbLightBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbLightBlue.CheckedChanged
If rbLightBlue.Checked Then
SecondForm.BackColor = (Color.LightBlue)
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGhostWhite.CheckedChanged
If rbGhostWhite.Checked Then
SecondForm.BackColor = (Color.GhostWhite)
End If
End Sub
The problem am having is making the background color change on Form2.
Any answer for this question will be very helpful.
I am not sure what you are doing, it probably has to do with how you are creating your SecondForm, this code does work, see if it helps you narrow it down.
Public Class Form1
Dim SecondForm As Form2 = New Form2
Private Sub rbLightBlue_CheckedChanged(sender As Object, e As EventArgs) Handles rbLightBlue.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.LightBlue
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(sender As Object, e As EventArgs) Handles rbGhostWhite.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.GhostWhite
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
End Class

Reacting to third-party application form events using vb.net

I would like to know how I should code my VB.net application to react to the form load event in a third-party application (also written in VB.net)
To test, I have created two basic programs, one with two forms (program A) and one (program B) that attempts to the listen to program A's appropriate form load event. I have tried using WithEvents but it does not get fired when Program's A second form loads.
Here is the code for Program A:
Public Class StartPage
Public WithEvents loadtimer As New System.Windows.Forms.Timer
Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
loadtimer.Interval = 1000
loadtimer.Enabled = True
loadtimer.Start()
End Sub
Private Sub loadtimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles loadtimer.Tick
loadtimer.Stop()
SystemStatus.Show()
End Sub
End Class
Public Class SystemStatus
Inherits StartPage
Private Sub StartPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = "This is the form that I want to listen for the load event"
Me.loadtimer.Enabled = False
End Sub
End Class
Here is the code for Program B:
Imports Program_A
Public Class ListeningForm
Dim WithEvents testlisten As New Program_A.SystemStatus
Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
Label1.Text = "SystemStatus form loaded"
End Sub
Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Waiting for SystemStatus load event..."
End Sub
End Class
I am quite new when it comes to programming so maybe this is not even possible or I just haven't been understanding what I've been reading. In any case please enlighten me as to what my next course of action should be.
Thanks very much in advance,
theoleric
This will do what your asking.
Imports Program_A
Public Class ListeningForm
Dim WithEvents testlisten As New SystemStatus
Private Sub testlisten_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles testlisten.Load
' now this event will fire
Label1.Text = "SystemStatus form loaded"
End Sub
Private Sub ListeningForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Waiting for SystemStatus load event..."
' the load event will not fire until you call this
testlisten.Show()
End Sub
End Class