Custom MsgBox without form activate being fired - vb6

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

Related

VB6 How to return a string value from form2 to form 1

in my project there are two forms:
first form i named it frmSettings , i will use text boxes to save values in INI file.
second form i named it frmSelectFolder , i had included with DirListBox and 2 Command buttons
as shown in attached image above in Settings form i have 8 text boxes and 8 command buttons to browse for folder path that it will be selected from frmSelectFolder
how to use frmSelectFolder for all text boxes without duplicating this form per each command button to return DirlistBox Control value ?
Here is some sample code for secondary frmSelectFolder form
Option Explicit
Private m_bConfirm As Boolean
Public Function Init(sPath As String) As Boolean
Dir1.Path = sPath
Show vbModal
If m_bConfirm Then
sPath = Dir1.Path
'--- success
Init = True
End If
Unload Me
End Function
Private Sub cmdOk_Click()
If LenB(Dir1.Path) = 0 Then
MsgBox "Please select a path!", vbExclamation
Exit Sub
End If
m_bConfirm = True
Visible = False
End Sub
Private Sub cmdCancel_Click()
Visible = False
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode <> vbFormCode Then
Cancel = 1
Visible = False
End If
End Sub
Here is how to call Init method above from primary frmSettings
Option Explicit
Private Sub cmdStartupPath_Click()
Dim sPath As String
Dim oFrmSelector As New frmSelectFolder
sPath = txtStartupPath.Text
If oFrmSelector.Init(sPath) Then
txtStartupPath.Text = sPath
txtStartupPath.SetFocus
End If
End Sub
Private Sub cmdDownloadPath_Click()
Dim sPath As String
Dim oFrmSelector As New frmSelectFolder
sPath = txtDownloadPath.Text
If oFrmSelector.Init(sPath) Then
txtDownloadPath.Text = sPath
txtDownloadPath.SetFocus
End If
End Sub
Here is a link to a complete sample project for you to research: SelectFolder.zip

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

How to display selected features in ArcGIS Identify Dialog using Visual Studio 2010/ArcObjects?

I'm brand new to ArcObjects SDKs and am struggling. I have a Python Add-in performing a query to select records (works great)and now trying to call the identify dialog via an .NET addin button that displays the identify dialog box to show attributes of the selected records. Below is the code I have at this point. I currently have the identify dialog displaying, but no records appearing. I know I need to input the selected records somewhere....but not sure where. Any thoughts would be appreciated. (I'm using Visual Studio/Microsoft Visual Basic 2010 and ArcGIS 10.2.1)
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Public Class Identify_Button
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Dim pMxDoc As IMxDocument
Dim activeView As IMap
Public Sub DoIdentify(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal x As System.Int32, ByVal y As System.Int32)
pMxDoc = My.ArcMap.Application.Document
activeView = pMxDoc.FocusMap
If activeView Is Nothing Then
Return
End If
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
Dim identifyDialog As ESRI.ArcGIS.CartoUI.IIdentifyDialog = New ESRI.ArcGIS.CartoUI.IdentifyDialogClass
identifyDialog.Map = map
'Clear the dialog on each mouse click
identifyDialog.ClearLayers()
Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay
Dim display As ESRI.ArcGIS.Display.IDisplay = screenDisplay ' Implicit Cast
identifyDialog.Display = display
Dim identifyDialogProps As ESRI.ArcGIS.CartoUI.IIdentifyDialogProps = CType(identifyDialog, ESRI.ArcGIS.CartoUI.IIdentifyDialogProps) ' Explicit Cast
Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = identifyDialogProps.Layers
enumLayer.Reset()
Dim layer As ESRI.ArcGIS.Carto.ILayer = enumLayer.Next
Do While Not (layer Is Nothing)
identifyDialog.AddLayerIdentifyPoint(layer, x, y)
layer = enumLayer.Next()
Loop
identifyDialog.Show()
End Sub
Public Sub New()
End Sub
Protected Overrides Sub OnClick()
DoIdentify(activeView, 300, 100)
End Sub
Protected Overrides Sub OnUpdate()
Enabled = My.ArcMap.Application IsNot Nothing
End Sub
End Class
Give the code below a try. This is executed from the OnClick event from an ArcMap command button that Inherits from BaseCommand. It displays the selected features in the map in the identifyDialog just as you were needing it to except I used AddLayerIdentifyOID() instead of AddLayerIdentifyPoint() although both should work.
Public Overrides Sub OnClick()
'VBTest.OnClick implementation
Try
Dim mxDoc As IMxDocument = m_application.Document
Dim map As ESRI.ArcGIS.Carto.IMap = mxDoc.FocusMap
Dim layer As ESRI.ArcGIS.Carto.ILayer
Dim flayer As ESRI.ArcGIS.Carto.IFeatureLayer
Dim featSelection As ESRI.ArcGIS.Carto.MapSelection = map.FeatureSelection
Dim feat As ESRI.ArcGIS.Geodatabase.IFeature = featSelection.Next()
Dim count As Int16 = 0
While feat IsNot Nothing
count += 1 ' flag for clearing layers
flayer = New ESRI.ArcGIS.Carto.FeatureLayer()
flayer.FeatureClass = feat.Class
layer = flayer
DoIdentify(layer, feat.OID, (count = 1))
feat = featSelection.Next()
End While
Catch ex As Exception
' handle error
MsgBox("Error: " + ex.Message)
End Try
End Sub
Private Sub DoIdentify(ByVal layer As ESRI.ArcGIS.Carto.ILayer, ByVal OID As Int32, Optional ByVal clearLayers As Boolean = True)
If layer Is Nothing Or OID <= 0 Then
Return
End If
Dim pMxDoc As IMxDocument = m_application.Document
Dim activeView As ESRI.ArcGIS.Carto.IActiveView = pMxDoc.ActiveView
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
Dim identifyDialog As ESRI.ArcGIS.CartoUI.IIdentifyDialog = New ESRI.ArcGIS.CartoUI.IdentifyDialogClass()
Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay
Dim display As ESRI.ArcGIS.Display.IDisplay = screenDisplay
identifyDialog.Map = map ' REQUIRED
identifyDialog.Display = display ' REQUIRED
' Clear the dialog
If clearLayers Then
identifyDialog.ClearLayers()
End If
' Add our selected feature to the dialog
identifyDialog.AddLayerIdentifyOID(layer, OID)
' Show the dialog
identifyDialog.Show()
End Sub

Visual Studio - Perform a event action for "X" number of checkboxes?

I have 20 checkboxes.
This is the event for the checkbox nÂș1 :
Public Sub C1CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles C1CheckBox1.CheckedChanged
If C1CheckBox1.Checked = True Then My.Settings.box1_selected = "Y" Else My.Settings.box1_selected = "N"
Dim checkedpath1 = C1CheckBox1.Text
End Sub
I know I can add all the checkboxes in the "handles", but my number of checkboxes is undetermined and this is what I want to do:
(Pseudocode)
Public Sub ALL_THE_CHECKBOXES_CheckedChanged(sender As Object, e As EventArgs) Handles ALL_THE_CHECKBOXES.CheckedChanged
If ANY_CHECKBOX.Checked = True
My.Settings.boxNUMBER_OF_THIS_SELECTED_CHECKBOX_selected = "Y"
Else
My.Settings.boxNUMBER_OF_THIS_SELECTED_CHECKBOX_selected = "N"
End If
Dim checkedpathNUMBER_OF_THIS_SELECTED_CHECKBOX = C1CheckBoxNUMBER_OF_THIS_SELECTED_CHECKBOX.Text
End Sub
I need to generate a event that handles an undetermined number of checkboxes,
I need to do the same action if any of the checkboxes is selected, but only in that selected checkbox.
Basically I want to remember in the settings which checkboxes was selected and which not...
UPDATE:
At form load I create ALL the form checkboxes with this code:
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim List As Integer = 0
Dim posy As Integer = 10
filesystem = CreateObject("Scripting.FileSystemObject")
ThisDir = filesystem.GetFolder(My.Settings.folderpath)
For Each folder In ThisDir.Subfolders
List = List + 1
posy = posy + 20
Dim newCheckBox As New CheckBox()
Panel1.Controls.Add(newCheckBox)
newCheckBox.Name = "checkbox" & List.ToString()
newCheckBox.Text = folder.name
newCheckBox.Location = New Point(10, posy)
Next
End Sub
That creates a checkboxes named "checkbox1", "checkbox2", "checkbox3", etc...
All checkboxes are inside of another control. might be form, panel whatever. So you can cycle through these checkboxes and manually assign the eventhandler for each checkbox you find
Revised Sample code:
You need a setting of type StringCollection named MyCBs - or you can use whatever name you like, just make the necessary changes to the code.
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Save()
End Sub
Private Sub AnyCB_CheckedChanged(sender As Object, e As EventArgs)
Dim cb = DirectCast(sender, CheckBox)
If cb.Checked AndAlso Not My.Settings.MyCBs.Contains(cb.Name) Then
My.Settings.MyCBs.Add(cb.Name)
ElseIf Not cb.Checked AndAlso My.Settings.MyCBs.Contains(cb.Name) Then
My.Settings.MyCBs.Remove(cb.Name)
End If
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If My.Settings.MyCBs Is Nothing Then My.Settings.MyCBs = New Collections.Specialized.StringCollection
For Each s In My.Settings.MyCBs
DirectCast(Me.Controls(s), CheckBox).Checked = True
Next
For Each cb In Me.Controls.OfType(Of CheckBox)()
AddHandler cb.CheckedChanged, AddressOf AnyCB_CheckedChanged
Next
End Sub
This sample saves the checked status of the checkboxes in my.settings inside a StringCollection. If a CB is checked it's name is added to the collection, if it's unchecked, it's name is removed. This means on startup you can simply check which CB name is in the collection and sets its checked stats to true. Finally you use AddHandler to execute the code for any CB contained in your Form.
Note that there's NO error handling inside the code, which means that this is your part ...

Resources