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

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

Related

Can the columns of a new row being added to a DataGridView control be referred to by name..?

I'm creating a Windows Forms 2.0 app in Visual Studio 2008. The form has a DataGridView control which will have rows programmatically added to it with the overload Add(ByVal ParamArray Values()), as shown:
dgv.Rows.Add (var1, var2, var3, varEtc)
Is there a way to refer to the cells by name rather than by relying on their order..?
The dgv will have many columns, and referring to them by order will be confusing as I develop the app. It would be much easier to refer to them by some name or index string.
Unfortunately the DataGridView classes are many and vast, and I don't know which direction to go in. I have a half-baked idea to create each row object first, configure it, and and then add it to the collection, as shown below:
Dim dgvr as DataGridViewRow = New DataGridViewRow
...more code needed...
dgvr.SomeProp.ID = var1
dgvr.SomeProp.NameF = var2
dgvr.SomeProp.NameL = var3
dgvr.SomeProp.Etc = varEtc
dgv.Rows.Add (dgvr)
I'm sure this isn't the only way, and not yet even a functional one. Can I make this work..? What other ways are there..? Anything better..?
The link in the comment from Mick on
my OP set me on the road to what I was hoping for - which turned out to be a few steps beyond what I originally conceived.
What I ended up with was a class extension of DataGridViewRow. It adds a the new extension function GetCellByColumnCaption, which does exactly that: it returns a DataGridViewCell object corresponding to the caption of the cell's column in the DataGridView control.
Doing it this way removes the control names from being an issue. Now I can simply refer to the cells in new rows by the most visible identifier they have: the column header text.
Here's a working example...
Public Class Form1
Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
) _
Handles Me.Load
'Remove the new blank row.
dgv.AllowUserToAddRows = False
End Sub
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) _
Handles Button1.Click
Dim newRowIdx As Integer = dgv.Rows.Add()
Dim newRowObj As DataGridViewRow = dgv.Rows.Item(newRowIdx)
newRowObj.GetCellByColumnCaption("ID").Value = "123"
newRowObj.GetCellByColumnCaption("Name").Value = "Bob"
newRowObj.GetCellByColumnCaption("Etc").Value = "Red"
End Sub
End Class
Module ClassExtensions
'Note: The Extension() attribute requires .Net 3.5 or above.
<System.Runtime.CompilerServices.Extension()> _
Friend Function GetCellByColumnCaption( _
ByVal dgvr As DataGridViewRow, _
ByVal colCaption As String _
) _
As DataGridViewCell
For Each cell As DataGridViewCell In dgvr.Cells
Dim hdrText = LCase(cell.OwningColumn.HeaderText)
colCaption = LCase(colCaption)
If hdrText = colCaption Then
GetCellByColumnCaption = cell
Exit Function
End If
Next
GetCellByColumnCaption = Nothing
End Function
End Module

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 put a number in a text box using a button in visual basic application forms

I am trying to make a calculator on visual basic application forms and I have most of the codes sorted. How do I make it so when I press the number button, it puts the number in the text box. It also needs to be able to work so if I pressed 1 then 2 then 3, it appears a 123.
Just set the SelectedText() property of the TextBox to the Text() property of your button.
For example:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox1.SelectedText = Button1.Text
TextBox1.Focus()
End Sub
If you make all the buttons fire the same handler, then it becomes:
Private Sub AllButtons_Click(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, _
Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button0.Click
Dim btn As Button = DirectCast(sender, Button)
TextBox1.SelectedText = btn.Text
TextBox1.Focus()
End Sub

visual basic 2010 copy paste subroutine

I am trying to write 2 simple subroutines in VB 2010 that could be used with toolstripbutton contorls for multiple textboxes in a form. I know simple copy paste could be done using the textbox1.Copy() and TextBox1.Paste() methodes. What i am trying to do is write a common subroutine which could be used on any textboxes in the form not just one particular textbox.
My codes are below, I know there are errors in it, just wondering how it could be achieved. Any help would be highly appreciated. Thanks.
Public Class Form1
Private Sub copytext()
Dim txt As Control
If TypeOf txt Is TextBox Then
Clipboard.Clear()
Clipboard.SetText(txt.SelectedText)
End If
End Sub
Private Sub pastetext()
Dim txt As Control
If TypeOf txt Is TextBox Then
txt.Text = Clipboard.GetText
End If
End Sub
Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCopy.Click
Call copytext()
End Sub
Private Sub mnuPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPaste.Click
Call pastetext()
End Sub
End Class

Save Windows Form Size

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

Resources