Move Panel around Panel with Background Image - windows

I'm trying to build a windows application in VB.Net as part of the application I need to be able to place markers on and image but I'm having a large number of problems.
I have a panel which I have assigned the image I want to place markers on as the background and then I add custom panels to the panel.
The custom panel containing an image (the marker) and a label (the marker name) as I though moving just the panel would be easier than moving the marker image and name label.
I've tried assigning event handlers to the MouseDown, MouseUp and MouseMove to each of the custom panels but every time I try and move the panel it goes all over the place and leaves black boxes everywhere.
Dim image As New System.Drawing.Bitmap(/* Path */)
pnlPreviewPanel.BackgroundImageLayout = ImageLayout.Stretch
pnlPreviewPanel.BackgroundImage = image
pnlPreviewPanel.Controls.Clear()
For Each item As Marker In mMarkers
Dim panel as New CustomPanel
AddHandler panel, AddressOf DeviceMouseDown
AddHandler panel.Controls(0).MouseDown, AddressOf DeviceMouseDown
AddHandler panel.Controls(1).MouseDown, AddressOf DeviceMouseDown
AddHandler panel.MouseUp, AddressOf DeviceMouseUp
AddHandler panel.Controls(0).MouseUp, AddressOf DeviceMouseUp
AddHandler panel.Controls(1).MouseUp, AddressOf DeviceMouseUp
AddHandler panel.MouseMove, AddressOf DeviceMouseMove
AddHandler panel.Controls(0).MouseMove, AddressOf DeviceMouseMove
AddHandler panel.Controls(1).MouseMove, AddressOf DeviceMouseMove
pnlPreviewPanel.Controls.Add(panel)
Next
Private Sub DeviceMouseDown(ByVal pSender As Object, ByVal pEventArgs As MouseEventArgs)
Dim control As Control = pSender
If Not control.GetType() = GetType(CustomPanel) Then
control = control.Parent
End If
mSelectedPanel = CType(control, CustomPanel)
End Sub
Private Sub DeviceMouseUp(ByVal pSender As Object, ByVal epEventArgs As MouseEventArgs)
If Not mSelectedDevicePanel Is Nothing Then
mSelectedDevicePanel = Nothing
End If
End Sub
Private Sub DeviceMouseMove(ByVal pSender As Object, ByVal pEventArgs As MouseEventArgs)
If Not mSelectedDevicePanel Is Nothing Then
Dim x As Integer = pEventArgs.Location.X - pnlPreviewPanel.Location.X
Dim y As Integer = pEventArgs.Location.Y - pnlPreviewPanel.Location.Y
mSelectedDevicePanel.Location = New Point(x, y)
End If
End Sub

Related

VSTO Outlook Visual Basic - Handle Mail Item Closure

I'm creating an Outlook Add-In, and am struggling to detect and handle the event when a mail item is closed. The inspectors_NewInspector function is triggered sucessfully (as I can obtain the subject and sender of the email), but I cannot get the mailItem_close() function to be called at all.
Public Class ThisAddIn
Public WithEvents inspectors As Outlook.Inspectors
Public WithEvents mailItem As Outlook.MailItem
Private Sub ThisAddIn_Startup() Handles Me.Startup
inspectors = Me.Application.Inspectors
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
If Inspector.CurrentItem.size > 0 And Inspector.CurrentItem.class = 43 Then
mailItem = Inspector.CurrentItem
Dim mSubject As String = mailItem.Subject
Dim mFrom As String = mailItem.SenderEmailAddress
Dim mTime As String = mailItem.ReceivedTime
'MsgBox(mSubject)
End If
End Sub
Private Sub mailItem_Close()
MsgBox("closing")
End Sub
It seems you have copied the code from VBA to a VB.NET add-in. In add-ins you need to use the AddHandler method to attach an event handler programmatically, see How do I create an event handler for a programmatically created object in VB.NET? for more information.
Note, each time the NewInspector event is fired the mailItem is overwritten, so the previous item instance is lost and can't fire events:
Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
If Inspector.CurrentItem.size > 0 And Inspector.CurrentItem.class = 43 Then
mailItem = Inspector.CurrentItem
Dim mSubject As String = mailItem.Subject
Dim mFrom As String = mailItem.SenderEmailAddress
Dim mTime As String = mailItem.ReceivedTime
'MsgBox(mSubject)
End If
End Sub
Private Sub mailItem_Close()
MsgBox("closing")
End Sub
Instead, you need to maintain a list or array of items, so each time the NewInspector event is fired you will add an item to the list to keep the reference alive, so the events will be fired.
You may find the Implement a wrapper for inspectors and track item-level events in each inspector article helpful.
With huge thanks to Eugene, the solution was indeed to use AddHandler.
Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
If Inspector.CurrentItem.size > 0 And Inspector.CurrentItem.class = 43 Then
inspectorEvents = TryCast(Inspector, Outlook.InspectorEvents_Event)
AddHandler inspectorEvents.Close, AddressOf inspectorEvents_Close
End If
End Sub
Public Sub inspectorEvents_Close()
Dim currentInspector As Outlook.Inspector
currentInspector = Application.ActiveInspector
mailItem = currentInspector.CurrentItem
Dim mSubject As String = mailItem.Subject
Dim mFrom As String = mailItem.SenderEmailAddress
Dim mTime As String = mailItem.ReceivedTime
MsgBox("closing" & mSubject)
End Sub
I'm occasionally getting multiple close handlers firing when a mail message is closed, but I should be able to get around that by maintaining an array / list of inspector ids and ensuring only one is created per mail item.

how to run Cmd command(chkdsk) on selected drive in a dataGridView

I am building a program in visual-studio to check and fix drive using the cmd chkdsk command
I have been able to display the list of drive available is a dataGridview
I want to run chkdsk command on any selected drive(by the user) please how do I achieve that successfully. Furthermore I want the process and result of the chkdsk to be displayed on a textbox.
problem is the drives are displaying but when one is select and i click on the button fix is dose not execute(i.e fix the drive and show result&process in the textbox) Any help/code/suggestion will b appreciated, please note this is note a console application. My code are on is posted here as requested. i added the subs were i feel the problem could be from. , but if you want me to add more of my other subs i will.
Public Class Form2
Private binder As New BindingSource
Private drives As New BindingList(Of DriveInfo)
Private psi As ProcessStartInfo
Private cmd As Process
Private Delegate Sub invokeWithString(ByVal text As String)
Dim BackgrounndWorker1 As New BackgroundWorker
Private Shadows Sub OnClick(sender As Object, e As EventArgs) Handles btnfix.Click
If TypeOf sender Is Button Then
Dim btn = DirectCast(sender, Button)
btn.Enabled = False
If btn.Equals(btnfix) Then
Ddrives.Enabled = False
pgb.Visible = True
BackgroundWorker1.RunWorkerAsync(New List(Of DriveInfo)(From drive As DriveInfo In cldrive.CheckedItems Select drive))
End If
End If
End Sub
Private Sub OnDoWOrk(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If TypeOf sender Is BackgroundWorker Then
Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
If e.Argument IsNot Nothing AndAlso TypeOf e.Argument Is IEnumerable(Of DriveInfo) Then
Dim drives As IEnumerable(Of DriveInfo) = DirectCast(e.Argument, IEnumerable(Of DriveInfo))
For Each drive As DriveInfo In drives
Try
cmd.Kill()
Catch ex As Exception
End Try
TextBox1.Text = ""
psi = New ProcessStartInfo("Chkdsk /f")
Dim systemcoding As System.Text.Encoding =
System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
With psi
.UseShellExecute = False
.RedirectStandardError = True
.RedirectStandardOutput = True
.RedirectStandardInput = True
.CreateNoWindow = True
.StandardOutputEncoding = systemcoding
.StandardErrorEncoding = systemcoding
End With
cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
AddHandler cmd.ErrorDataReceived, AddressOf Async_data_received
AddHandler cmd.OutputDataReceived, AddressOf Async_data_received
Try
cmd.Start()
cmd.BeginOutputReadLine()
cmd.BeginErrorReadLine()
Catch ex As System.ComponentModel.Win32Exception
End Try
Next
End If
End If
End Sub
Private Sub Async_data_received(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
Me.Invoke(New invokeWithString(AddressOf Sync_output), e.Data)
End Sub

Add handler to runtime-created control

I found in StackOverflow some vb.net code written by "PaRiMaL RaJ". i want the same think but i must convert this code to work on VB6.
can you help me please???
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' creating control
Dim btn1 As Button = New Button()
Dim btn2 As Button = New Button()
btn1.Parent = Me
btn1.Name = "btn1"
btn1.Top = 10
btn1.Text = "Btn1"
btn2.Parent = Me
btn2.Name = "btn2"
btn2.Top = 50
btn2.Text = "Btn2"
'adding handler for click event
AddHandler btn1.Click, AddressOf HandleDynamicButtonClick
AddHandler btn2.Click, AddressOf HandleDynamicButtonClick
End Sub
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
If btn.Name = "btn1" Then
MessageBox.Show("Btn1 clicked")
ElseIf btn.Name = "btn2" Then
MessageBox.Show("Btn2 Clicked")
End If
End Sub
The easiest way is to use a control array. Add a button to your form in the designer and set its Index property to 0. You can hide the button if you don't want it to be visible.
Then, when you want to dynamically add more buttons at run-time, just use the Load statement.
For example, if your button was named Command1:
Load Command1(1) ' Create a new button
Command1(1).Move 50, 50, 1500, 500 ' Set its size and position
Command1(1).Caption = "New Button" ' Give it a caption
Command1(1).Visible = True ' Make it visible
The button will use the same event handlers as your original button:
Private Sub Command1_Click(Index As Integer)
' Print the caption of the clicked button...
Debug.Print Command1(Index).Caption
End Sub

Custom MsgBox without form activate being fired

I have developed a custom MsgBox that works fine in almost every way. The only problem is that when the MsgBox closes the parent form runs the Form_Activate code. A normal MsgBox does not run that code (again).
I know i could add a boolean variable to Form_Activate to check if it has fired already, but that's not the best solution when you have a dozen forms.
So, is there a way to not run Form_Activate after closing my custom MsgBox? Does the MsgBox form need to be of some special type or something? I tried all BorderStyles but that doesn't make any difference.
Are you using another form to make custom MsgBox?
You shouldn't use directly other form to show a custom messagebox.
You should create an Activex control, and Activate event won't fire again when the MsgBox is closed.
Within the control you can use a form if you want it. (Probably just have to place your code inside an ActiveX control project and use it in your forms)
I use it that way.
This is a custom MsgBox example using Activex Control, with a test form too.
http://www.codeguru.com/code/legacy/vb_othctrl/2166_CustomMsgBox.zip
I created a Class for a Custom MsgBox.
Public Class CustomMsgBox
'Creates the Main form
Dim Main As New Form
'Creates the buttons
Dim Btn1, Btn2, Btn3 As New Button
'Creates the label
Dim Lbl As New Label
'Creates the Output variable
Dim Output As Integer = 0
Private Sub Load()
'Btn1 properties
Btn1.Location = New Point(168, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Btn2 properties
Btn2.Location = New Point(87, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Btn3 properties
Btn3.Location = New Point(6, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Lbl properties
Lbl.Location = New Point(12, 19)
Lbl.AutoSize = True
Lbl.AutoEllipsis = True
'Main form properties
Main.Size = New Size(211, 129)
Main.AutoSize = True
Main.AutoSizeMode = AutoSizeMode.GrowOnly
Main.ShowIcon = False
Main.Controls.Add(Btn1)
Main.Controls.Add(Btn2)
Main.Controls.Add(Btn3)
Main.Controls.Add(Lbl)
'Adds Handlers to the buttons
AddHandler Btn1.Click, AddressOf btn1_Click
AddHandler Btn2.Click, AddressOf btn2_Click
AddHandler Btn3.Click, AddressOf btn3_Click
End Sub
Function CstMsgBox(ByRef Msg As String, ByRef Title As String, ByRef B1 As String, Optional ByRef B2 As String = Nothing, Optional ByRef B3 As String = Nothing) As Integer
'Runs the Load() Sub
Load()
'Sets the values
Lbl.Text = Msg
Btn1.Text = B1
Btn2.Text = B2
Btn3.Text = B3
Main.Text = Title
'Checks if there is a value set to Btn2 and Btn3
If Btn2.Text = Nothing Then
Btn2.Hide()
End If
If Btn3.Text = Nothing Then
Btn3.Hide()
End If
'Shows the MsgBox
Main.Show()
'Waits until a button is pressed
Do Until Output <> 0
Application.DoEvents()
Loop
'Closes the MsgBox
Main.Close()
Return Output
End Function
Private Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 1
Output = 1
End Sub
Private Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 2
Output = 2
End Sub
Private Sub btn3_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 3
Output = 3
End Sub
End Class
You can use it by typing this:
Dim CMB As New CustomMsgBox
CCMB.CstMsgBox('MSG, 'TITLE, 'BUTTON1, 'Optional: BUTTON2, 'Optional: BUTTON3)
OR
Dim CMB As New CustomMsgBox
Select Case CMB.CstMsgBox('MSG, 'TITLE, 'BUTTON1, 'Optional: BUTTON2, 'Optional: BUTTON3)
Case 1
'Code to execute when button1 is pressed
Case 2
'Code to execute when button2 is pressed
Case 3
'Code to execute when button3 is pressed
End Select

Programmatically adding event handler to array of radio buttons

Trying to add event handlers to radio buttons that are created on user input, looking at this and this as examples.
However I get an error that answerOptions is not an event.
Create the radio buttons
Private answerOptions(n) As RadioButton
...
Private Sub showQuestion(n As Integer)
For i = 0 To answerOptions.Length - 1
answerOptions(i) = New RadioButton
AddHandler answerOptions, AddressOf Me.Radios_Click
With answerOptions(i)
' --------- SET TEXT, LOCATION ETC.
End With
Me.Controls.Add(answerOptions(i))
Next
End Sub
Planning on then handling events with
Private Sub Radios_Click(sender As Object, e As EventArgs) Handles answerOptions.checked
End Sub
I want things to happen when the radios are checked. Don't want to use checkboxes as I want to limit one selection at a time.
Try this in your form or page
Private Sub ClickButton(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Rbtn As RadioButton
Rbtn = CType(sender, RadioButton)
MsgBox(Rbtn.Text)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim Rbtn As New RadioButton()
For i = 0 To 19
Rbtn = New RadioButton()
With Rbtn
.Name = "RBtn" & i
.Text = .Name
.Checked = False
.Left = 20
.Top = (i * 20)
.Visible = True
Me.Controls.Add(Rbtn)
AddHandler Rbtn.Click, AddressOf ClickButton
End With
Next
End Sub

Resources