I havea a UserControl1 (in witch I have an Label1) in Form1. I want to catch the MouseDown event from Label and send it like it was from UserControl.
I do:
Public Class UserControl1
Shadows Custom Event MouseDown As MouseEventHandler
AddHandler(ByVal value As MouseEventHandler)
AddHandler Label1.MouseDown, value
End AddHandler
RemoveHandler(ByVal value As MouseEventHandler)
RemoveHandler Label1.MouseDown, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As MouseEventArgs)
'RaiseMouseEvent(Me, e) ??? '
End RaiseEvent
End Event
End Class
However, when I set in the Form1 the UserControl
Private Sub UserControl11_MouseDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles UserControl11.MouseDown
' here I have "Label", BUT want "UserControl" '
MessageBox.Show(sender.GetType.Name)
End Sub
One detail.. I want that the event should be only on the label, not on the whole userControl.
Why don’t you just handle the event “old school” and delegate it, instead of creating a custom event? Like so:
' In the user control: '
Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) _
Handles Label1.MouseDown
OnMouseDown(e)
End Sub
Now when you handle the UserControl.MouseDown event in your form, the sender of the event will be the user control instance.
If you only want to capture clicks on the label (instead of on the whole user control) then you can override OnMouseDown to test from where the click originates:
Private m_MouseDownFromLabel As Boolean = False
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles Label1.MouseDown
m_MouseDownFromLabel = True
OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
If m_MouseDownFromLabel Then
m_MouseDownFromLabel = False
MyBase.OnMouseDown(e)
End If
End Sub
This should be safe in the face of race conditions since there’s only one UI thread.
By the way: RaiseMouseEvent cannot be used here since the first argument would have be the MouseDown event property. However, this property cannot be accessed from derived classes, only inside the Control class itself. I don’t know why RaiseMouseEvent isn’t private itself, instead of being protected, when it can’t be used from derived classes anyway.
Related
Hi I've migrated VB6 code to VB.Net where in VB6 one of the functionality uses modal dialogue which allows user to copy few text from parent form. But in Vb.Net ShowDialog doesn't allow user to copy anything as you guys know it just disables the parent form.
My question is, Is there a way I can enable parent form or else minimize child form to copy few text from parent form?
please don't suggest to use show instead of ShowDialog because I want to achieve this only using ShowDialog.
This VB6 Code.
Form.Show vbModal, objParent
migration wizard has below code
Form.ShowDialog
The answer may be one of design, instead of a technical workaround to .ShowDialog(). Let's take your parent form, for example, with text that may be copied for pasting within a popup modal form. I don't know the data in your parent form, so let's call it a Widget.
Public Class Widget
Public Property ID As Integer = 0
Public Property TextThatMayBeCopied As String = String.Emtpy
End Class
In your parent form's code, you would load this data into a Widget object from a database, a file, whatever.
Private _widget As Widget = Nothing
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Assume we want the Widget with ID of 123
_widget = MyFunction.WhichLoadsWidgetDataAndReturnsWidgetObject(123)
DisplayData()
End Sub
Private Sub DisplayData()
txtID.Text = _widget.ID
txtTextThatMayBeCopied.Text = _widget.TextThatMayBeCopied
End Sub
Private Sub btnShowDialog_Click(sender As Object, e As EventArgs) Handles btnShowDialog.Click
_widget.TextThatMayBeCopied = txtTextThatMayBeCopied.Text.Trim
Dim f As New MyShowDialogForm(_widget)
f.ShowDialog
End Sub
Your target form MyShowDialogForm would take in it's own constructor an object of type Widget:
Private _widget As Widget = Nothing
Public Sub New(widget As Widget)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_widget = widget
End Sub
You can now access the data passed to form MyShowDialogForm via the _widget object, for example, in a button click event for btnCopyText, or however you need.
The key takeaway here is to use a method of exchanging data within different forms. Typically it becomes very messy code to use the Form classes themselves as the encapsulation for data. Instead, use classes for encapsulating data and moving it around your app.
'For example we have 2 form, form1 (main) and form2 (child)
'This Form as Main
Public Class Form1
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'if you don't have timer in child form, you must click again this button
'after you form back awhile to this form as main form
Form2.ShowDialog()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
Me.Dispose()
End Sub
End Class
'This Form as Child
Public Class Form2
Private Sub btnToMainAWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToMainAWhile.Click
'you hide this current form to caller form, you must back here again,
'if not this form always active in background
'or if you have timer1 here with enabled property =false here, you can add this:
'Timer1.Interval = 10000
'Timer1.Enabled = True
Me.Hide()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
Me.Dispose()
End Sub
'if you have timer1 and you will wait for many seconds back to main form, add this:
'Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' Timer1.Enabled = False
' Me.ShowDialog()
'End Sub
End Class
In Microsoft Access, when you click on a label, the textbox associated with that label gets the focus. As far as I can tell, VB.NET does not have this same functionality. I know I can always add something in to the click event of the label, like so...
TextBox1.Focus()
But I have dozens of fields on the form, and it would make it so much easier if I did not need to add this to the click event of each label.
I guess it would be possible to make an event for all labels that forces a tab to the next control, and assuming that I have the Tab indices set up correctly, then this would work. The problem would occur when adding new fields to the form, in which case all tab indices would need re-verified.
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click, Label2.Click
'code to tab to next field...
End Sub
Is there any easier way?
First, set the controls' TabIndex orders on your form then use this code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls
If TypeOf c Is Label Then AddHandler c.Click, AddressOf Label_Click
Next
End Sub
Private Sub Label_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.SelectNextControl(sender, True, True, True, True)
End Sub
End Class
Now whenever you click on a label, the following control in the order will be focused.
How about creating a dictionary where the label is the key and the control to focus is the value, then add a Click event handler to all of the label in the Dictionary. Everytime you add a label/control 'all' you need to do is add another KeyValuePair to the Dictionary
Simple Example:
Public Class Form1
Protected Friend DicLabelToControl As Dictionary(Of Label, Control)
Protected Friend Sub InitLabelDic()
DicLabelToControl = New Dictionary(Of Label, Control) From {{Label1, TextBox1}, {Label2, TextBox2}}
End Sub
Protected Friend Sub AddClickEventsToLabels()
For Each lb As Label In DicLabelToControl.Keys
AddHandler lb.Click, AddressOf HandleLabelClick
Next
End Sub
Private Sub HandleLabelClick(sender As Object, e As EventArgs)
DicLabelToControl(CType(sender, Label)).Focus()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitLabelDic()
AddClickEventsToLabels()
End Sub
End Class
I got many textboxes (about 10) in a form. I want the text in the textbox to be higlighted whenever it gets foucs. The code for that looks like:
Private Sub txtBillNo_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBillNo.GotFocus
HoverText(txtBillNo)
End Sub
Private Sub HoverText(ByRef ctrl as TextBox)
ctrl.SelectAll()
End Sub
It works perfectly, but I though I could do some code optimization here. Since, I have about 10 textboxes (and many other forms containing several textboxes), I have to HoverText(TextBox) in every Private Sub.. Handles TextBox.GotFocus for every textbox in each form.
I look for any form event (or any other way) that is trigerred when focus is given to another control (textbox) within the form, either by MouseClick or TAB so that HoverText(TextBox) is needed to be written only once for a form.
You can list all your textboxes in the Handles clause:
Private Sub atextbox_GotFocus(ByVal sender As System.Object, ByVal e As _
System.EventArgs) _
Handles txt1.GotFocus, _
txt2.GotFocus, _
txt3.GotFocus, _
(...remaining text boxes..)
txt9.GotFocus, _
txt10.GotFocus
Or you can add handlers to all textboxes when loading the form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each t In Me.Controls.OfType(Of TextBox)()
AddHandler t.GotFocus, AddressOf HoverText
Next
End Sub
Private Sub HoverText(ByVal sender As System.Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).SelectAll()
End Sub
The .NET way to alter the behavior of a class is to derive a new one. That's well supported by VB.NET and Winforms as well. For any class derived from Control, you can intercept an event by overriding the protected OnXxx event where Xxx is the event name. You should be using the Enter event btw. Use Project + Add Class and make the code look like this:
Public Class MyTextBox
Inherits TextBox
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
Me.Select(0, Me.Text.Length)
MyBase.OnEnter(e)
End Sub
End Class
Compile. Go back to your form and note that you now have a new control on the top of the toolbox. Drag it on the form and note how it now behaves the way you want it, without writing any code or event handler in your form at all.
I have a custom text box control which validates input (striped out unwanted chars). This works fine apart from when I also want to do further processing on an implementation of the control.
Example I have 3 "specialTextbox"s on a form. sText1, sText2 and sText3. sText1 & sText2 work as as intended. However, I need to make changes on the forum when the value of sText3 is changed, so I have a handler in the form to handle the ctext changed event:
Private Sub sText3(sender As Object, e As EventArgs) Handles sText3.TextChanged
'do some stuff here
End Sub
However this routine appears to override the OnTextChanged method of the custom text box. I have tried includeing a call to MyBase.OnTextChanged, but this still doesn't cascade up and no matter what I do I can't seem to get the text box to do its validation duties.
Must be something really simple, but I'm stumped!
Here is a class which overrides textbox
Public Class restrictedTextBox
Inherits Windows.Forms.TextBox
Protected validChars As List(Of Char)
Public Sub New(ByVal _validChars As List(Of Char))
MyBase.New()
validChars = _validChars
End Sub
Public Sub setValidChars(ByVal chrz As List(Of Char))
validChars = chrz
End Sub
Protected Overrides Sub OnTextChanged(e As System.EventArgs)
MyBase.OnTextChanged(e)
Dim newValue As String = ""
For Each c As Char In Me.Text.ToCharArray
Dim valid As Boolean = False
For Each c2 As Char In validChars
If c = c2 Then valid = True
Next
If valid Then newValue &= c.ToString
Next
Me.Text = newValue
End Sub
End Class
Here is a form which has a a custom textbox
Public Class frmNewForm
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOK.Click
MessageBox.Show("the text from the restricted text is: " & txtRestricted.Text)
End Sub
End Class
Here is a form with a custom text box, which implements the TextChanged event
Public Class frmNewForm2
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOK.Click
MessageBox.Show("the text from the restricted text is: " & txtRestricted.Text)
End If
Private Sub txtRestricted_TextChanged(sender As Object, e As EventArgs) Handles txtRestricted.TextChanged
'now that I have implemented this method, the restrictedTextBox.OnTextChanged() doesn't fire - even if I call MyBase.OnTextChanged(e)
'just to be completely clear. the line of code below DOES get executed. But the code in restrictedTextBox.vb does NOT
lblAwesomeLabel.Text=txtRestricted.Text
End Sub
End Class
It fires, but probably not the way you are implementing it.
Your sample code does not have an empty constructor for the textbox, which means you are most likely not using the designer when you are adding the textbox to the form.
But your form shows it was created by the designer:
Private Sub txtRestricted_TextChanged(sender As Object, e As EventArgs) _
Handles txtRestricted.TextChanged
End Sub
That's not possible with your posted code. If you are creating "new" controls programmatically, then you need to wire up the events programmatically, too.
Drop the handler and just leave the stub:
Private Sub txtRestricted_TextChanged(sender As Object, e As EventArgs)
'yada-yada-yada
End Sub
then when you create a new textbox, wire it up:
txtRestricted = new restrictedTextBox(myCharsList)
AddHandler txtRestricted.TextChanged, AddressOf txtRestricted_TextChanged
Me.Controls.Add(txtRestricted)
I wish in my code to handle the case when the red X at the upper right of the form is clicked. To this end I consulted this and created an event handler thus:-
Private Sub DoFoo (sender As System.Object, e As System.EventArgs) _
Handles Me.FormClosing
' Do things
End Sub
but I have found (from setting breakpoints) that on certain forms this event handler is not invoked when the red X is clicked, whereas on others it is. The forms are all of type System.Windows.Forms.Form but naturally are different in most respects. Does anyone know what might be causing this and what to do about it?
Edit
In answer to Vitor's question, the form that isn't working is created thus:-
If my_form Is Nothing Then
my_form = New MyForm(parameters)
my_form.Title = "Contour Plot Options"
Else
my_form.BringToFront
End If
my_form.Show
Those that are behaving as expected are created like this:-
If my_working_form Is Nothing Then
my_working_form = New MyWorkingForm
End If
my_working_form.Show
I can't see any Visible property to set or clear anywhere.
Your parameters aren't quite right. A FormClosing event has a FormClosingEventArgs argument:
Private Sub DoFoo(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
If (e.CloseReason = CloseReason.UserClosing) Then
End If
End Sub
You can inspect the e variable for the property `CloseReason', which would include a UserClosing enum, which means the user closed the form.
Each form should handle its own FormClosing event. Instead of subscribing to the event, I find it better to just override it like this:
Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
If (e.CloseReason = CloseReason.UserClosing) Then
End If
MyBase.OnFormClosing(e)
End Sub
If you are instantiating your form you need to remember to AddHandler for the event you want to subscribe to.
my_form = New MyForm(parameters)
my_form.Title = "Contour Plot Options"
AddHandler my_form.Closing, AddressOf MyForm_Closing
'...
Sub MyForm_Closing(s As Object, ByVal e As FormClosingEventArgs)
'...
End Sub
Of course, to avoid memory leaks you should do it like this:
'global code
Private myFormClosingEventHandler As FormClosedEventHandler = Nothing
'...
Private Sub CreateForm
my_form = New MyForm(parameters)
my_form.Title = "Contour Plot Options"
myFormClosingEvent = New FormClosedEventHandler(AddressOf MyForm_Closing)
AddHandler my_form.Closing, myFormClosingEventHandler
'...
End Sub
Sub MyForm_Closing(s As Object, ByVal e As FormClosingEventArgs)
RemoveHandler my_form.Closing, myFormClosingEventHandler
'...
End Sub
Alternatively you can have it all pre-initalized for you by doing this in your class instead:
Private WithEvents my_form1 As Form = New Form()
Private WithEvents my_form2 As Form = New Form()
'... etc.
Now you can add in your code handlers using the Handle keyword as usual without using the AddHandler and RemoveHandler.
Protected Sub my_form1_Closing(s as object, e as FormClosingEventArgs) _
Handles my_form1.Closing
'...
End Sub