Save Windows Form Size - windows

I'm developing a piece in VB.NET. Inside my primary form, I'm creating a new form to use as a dialog. I was wondering if there was a way to, upon the close of the new dialog, save it's size settings for each user (probably in a file on their machine, through XML or something?)

you can save it to the settings file, and update it on the 'onclosing' event.
to make a setting goto Project Properties ->settings -> then make a setting like 'dialogsize' of type system.drawing.size.
then do this in your dialog form:
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(ByVal userSize As Size)
InitializeComponent()
Me.Size = userSize
End Sub
Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
MyBase.OnClosing(e)
My.Settings.DialogSize = Me.Size
My.Settings.Save()
End Sub
do something like this to check and use the setting:
Dim dlg As MyDialogWindow
If My.Settings.DialogSize.IsEmpty Then
dlg = New MyDialogWindow()
Else
dlg = New MyDialogWindow(My.Settings.DialogSize)
End If
dlg.ShowDialog()

Although this is for C#, it will help with VB.Net as well.

You can also add a new setting to your application (size) and set it to system.drawing.size
Then, you make sure you save the current size to settings on close.
Private Sub myForm_FormClosing(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
My.Settings.size = Me.Size
My.Settings.Save()
End Sub
and on load you apply the size you have saved in settings
Private Sub myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
' if this is the first time to load the form
' dont set the size ( the form will load with the size in the designe)
If Not My.Settings.size.IsEmpty Then
Me.Size = My.Settings.size
End If
End Sub

Here's a solution that I found online that seems to work rather well for me.
Some of the previously mentioned solutions weren't working for me as expected. Depending on where my form was positioned at the time of closing the form wouldn't get repositioned back to that exact location when I would load it again.
This solution seems to do the trick by taking into account some other factors as well:
You need to set up these two setting under Project Properties -> settings: WindowLocation and WindowSize like so:
Then create the following function:
Private Sub LoadWindowPosition()
'Get window location/position from settings
Dim ptLocation As System.Drawing.Point = My.Settings.WindowLocation
'Exit if it has not been set (X = Y = -1)
If (ptLocation.X = -1) And (ptLocation.Y = -1) Then
Return
End If
'Verify the window position is visible on at least one of our screens
Dim bLocationVisible As Boolean = False
For Each S As Screen In Screen.AllScreens
If S.Bounds.Contains(ptLocation) Then
bLocationVisible = True
Exit For
End If
Next
'Exit if window location is not visible on any screen
If Not bLocationVisible Then
Return
End If
'Set Window Size, Location
Me.StartPosition = FormStartPosition.Manual
Me.Location = ptLocation
Me.Size = My.Settings.WindowSize
End Sub
Next, you'll need to add code to your form's load and closing events like so:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadWindowPosition()
End Sub
Private Sub frmMain_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
My.Settings.WindowLocation = Me.Location
My.Settings.WindowSize = Me.Size
End Sub
I hope that helps.

You can also do this using the UI provided by the VB.NET IDE itself. In the properties pane for a form, look under the item called "(Application Settings)" and then under "Property Binding." You can bind just about every property of the form (including size and location) to a settings value for that application.

As it turns out, I found a way to do this using the System.IO.IsolatedStorage

Related

vb - edit specific cell on double click and set read only after

I am developing simple app in visual basic and what I want to do is:
On load whole DataGridView1 is ReadOnly
Next if user will double click on cell, then it turns edit mode for that cell
I try to do this by:
Private Sub CellDouble(ByVal sender As Object,
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellDoubleClick
DataGridView1(e.ColumnIndex, e.RowIndex).ReadOnly = False
DataGridView1.BeginEdit(e.RowIndex)
End Sub
But it doesnt even react. (function is triggered but code not working)
Final step is to Set Read only back to that cell after edit
I simply did:
Private Sub CellDouble(ByVal sender As Object,
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellDoubleClick
DataGridView1.BeginEdit()
End Sub
and it's working fine...

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

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

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 ...

Enabling CTRL+Z property in masked edit textbox using VB.Net

I am using a masked edit text box in my windows application that was developed by using vb.net.
In normal text boxes (CTRL+Z- to revert back to original value) is working fine. But In case of Masked Edit Textboxes its not working fine.
Can any one please help me about this.
This ctrl+Z should provide the functionality as same as normal textbox.
You can use a variable to store the current text by programming the Leave event and check during the KeyDown event for the combination of Control+Z:
Dim oldText As String = ""
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MaskedTextBox1.KeyDown
If e.Control AndAlso e.KeyCode = Keys.Z Then MaskedTextBox1.Text = oldText
End Sub
Private Sub MaskedTextBox1_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MaskedTextBox1.Leave
oldText = MaskedTextBox1.Text
End Sub

Resources