I have two UserControls already loaded how to change TextBox text property on a UserControl from the other loaded one.
Lets say your user controls are named UserControl1 and UserControl2. Unless UserControl1 has a reference to UserControl2 it can't directly make changes to it. In that situation the one solution is to allow the form or parent control to handle making the change, by adding an event to UserControl1 and handling it on the form.
In UserControl1:
'Define an Event the form can handle at the class level
Public Event SomePropertyUpdated()
Then in whatever method you need it to be in, when you would want to change the textbox on the other control raise your event:
RaiseEvent SomePropertyUpdated()
In the form:
'The sub that is called when the second control needs updated
Public Sub UpdateTextBoxes()
UserControl2.Textbox1.text = userControl1.Property
End Sub
In the load event of the form Add the handler for your created event:
AddHandler UserControl1.SomePropertyUpdated, AddressOf UpdateTextBoxes
In the closed Event of the form remove the handler for the event:
RemoveHandler UserControl1.SomePropertyUpdated, AddressOf UpdateTextBoxes
That is one of a few ways to handle the situation. The specifics of what you are trying to do usually dictates what method to use.
Related
I am trying to create multiple custom controls in my application, For example:
Public Class CbsDataGridView
Inherits DataGridView
'...
Private Sub CbsDataGridView_Enter(sender As Object, e As EventArgs) Handles MyBase.Enter
'Code here omitted, not related to the question.
LoadGrid()
End Sub
Private Sub LoadGrid()
'Code here omitted, not related to the question.
End Sub
'...
End Class
Public Class CbsDateTimePicker
Inherits DateTimePicker
Private Sub CbsDateTimePicker_Enter(sender As Object, e As EventArgs) Handles MyBase.Enter
'Code here omitted, not related to the question.
End Sub
End Class
When adding these controls to a new empty form. Here's the two scenarios I've faced:
Scenario 1:
- Drag And Drop CbsDateTimePicker into the form
- Drag And Drop CbsDataGridView into the form
- Run Application - Load The New Form
- CbsDateTimePicker_Enter event fires.
- CbsDataGridView_Enter event doesn't fire.
Scenario 2:
- Drag And Drop CbsDataGridView into the form
- Drag And Drop CbsDateTimePicker into the form
- Run Application - Load The New Form
- CbsDataGridView_Enter event fires.
- CbsDateTimePicker_Enter event doesn't fire.
I think I miss-understood the Enter event of Controls.
What I am looking for is an event that acts like the Form.Load event which will fire when the form containing the control loads.
Is there a direct way to implement this functionality? Or should I be looking for another way?
Enter event will fire whenever the control activates by using mouse or keyboard. Also whenever you set the active control of the form by code, the event will fire.
Controls don't have a Load event similar to Load event of the Form, but they have a virtual OnCreateControl which is called when the control is first created. You can override it and add custom logic to that method, or even raise a custom Load event for your control.
Is there a way to override a forms button click event from a parent form?
For example, here's kind of what I have going on...
Dim newForm As New FormB
newForm.someproperty1 = True
newForm.someprpoerty2 = False
newForm.Show(Me)
I need to know what button was clicked when newForm is closed. I would normally use 'ShowDialog', but I can't since newForm needs to open up a DIFFERENT form that can't open as a Dialog... long story short, without a complete rewrite of our entire software, i can't use the ShowDialog.
Anyways, is there anyway that I can add button click events from another form? Something like...
Dim newForm As New FormB
newForm.btn1.click = SomeNewFunctionOrSubroutine()
newForm.btn2.click = SomeOtherFunctionOrSub()
newForm.Show(Me)
I've been looking at events and eventhandlers, but they aren't making too much sense...
Any tips?
You could call AddHandler on your button in FormB (provided that its Modifier property is Public)
' This code is inside your Parent form
AddHandler newForm.btn1.click, AddressOf Me.SomeOtherFunctionOrSub
' This code is inside your Parent form
Protected Sub SomeOtherFunctionOrSub(sender as Object, e As EventArgs)
......
End Sub
Adding an Handler is a common practice to intercept the actions on a different form (For example, you probably will need also to intercept the Form.Closing event in case you want to have just one instance of newForm open at any time.)
An other approach (more complex) is subscribing to a custom event and requires the cooperation of FormB code. But in this scenario it doesn't seem to be necessary.
Is there a simple way to Activate the form when any controls in the form is clicked like datagirdview, panel, menustrip, button, textbox, label, etc....
it happens that my project can show many different form and it's hard for me to activate one form when it's on the back of the active form. I need to clicked the border of the form to activate it.
most likely your problem arises because your form is MDI child.. you'll have to click the menu bar to activate the form.. but if you have a borderless form, clicking on controls wont activate the form.. again, this usually happens on MDI but it shouldnt happen on a regular winform.. unless you have other running events in the background that interferes with the process.
You question is not clear at all, and I don't know what you are trying to archieve, but for executing something with a click event you have to add the handler for every control.
If you are declaring it not dinamically just:
Private Sub ControlsClick(sender As Object, e As EventArgs) _
Handles Panel1.Click, Button1.Click, TextBox2.Click ' etc.
Me.Activate 'Or Whatever
End Sub
You have to add the handler for each control. The same if you do it dinamically:
Private Sub InitializeClickHandlers(sender As Control, Optional bChilds As Boolean = True)
For Each elem As Control In sender.Controls
AddHandler elem.Click, AddressOf ControlsClick(elem, New EventArgs)
If bChilds AndAlso elem.Controls.Count > 0 Then
Call InitializeClickHandlers(sender)
End If
Next
End Sub
Then, for every control in the form, you call it like: Call InitializeClickHandlers(Me)
Or, for every control inside a panel: Call InitializeClickHandlers(Panel1)
I have a Button in UserControl1.
I am using UserControl1 in Form1.
I want to handle Button's Click event in Form1.
I tried to do same via:
AddHandler userControl1.Button1.Click, AddressOf Button1_Click
And:
Public Sub Button1_Click(ByVal sender As Object, ByVal args As EventArgs) Handles userControl1.Button1.Click
End Sub
but getting error.
Create your event on the UserControl:
Public Class UserControl1
Public Event UC_Button1Click()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RaiseEvent UC_Button1Click()
End Sub
End Class
And then use the new event:
AddHandler userControl1.UC_Button1Click, AddressOf Button1_Click
Or you can simply define it like this on the UserControl and access to it from outside (not recommended):
Public WithEvents Button1 As System.Windows.Forms.Button
And then:
AddHandler uc.Button1.Click, AddressOf Button1_Click
I found this very confusing until I placed the "AddHandler" in the main program (not the UserControl).
So for clarification, the steps to ensure that you can "sense" in the main program an event which has occurred in a user control is:
Make sure any object in your user control for which you wish to alter properties (such as PictureBox.Image or TextBox.Text) has its modifier property set to "Public".
This is the only way you can allow other programs to alter properties.
For events which your want recognised (e.g. "Click","DblClick" etc.) place at the top of your User Control code one declaration line for a Public Event. As an example,
Public Event UC_MySub(.....parameters.....)
"UC_" is a prefix I use to help highlight that it is defined in a "UserControl".
The rest of the name ("MySub") can be anything and does not need to relate to the Click Event in any way. You could even call it "CreamedCheese" if you want!
Include any parameters you like the the definition of the public event, again they can be any type of name. These will be passed to the main program when you click on the User Control's object.
Now, Going to the event which runs when you "click" on a GroupBox (in this case), you kick off that public event as shown here:
Private Sub GroupBox_Click(sender As Object, e As EventArgs) Handles GroupBox1.Click
RaiseEvent UC_MySub(....Paramaters.....)
End Sub
You have to ensure the parameters passed in this call to the public event are the same in number (and type) as each of the parameters defined in the Public Event declaration itself.
NOW, you rebuild your User Object, then go to your main program.
In the "Load" routine of your main Form, add the following line FOR EACH OBJECT of the user defined object you are using.
For example, in my program I have 4 instances of my UDO (User Defined Object).
So I have added (assuming my UDO is named "MyUDO"):
AddHandler MyUDO1.UC_MySub, AddressOf SwapHands 'This is my sub accepting the values from the public event
AddHandler MyUDO2.UC_MySub, AddressOf SwapHands
AddHandler MyUDO3.UC_MySub, AddressOf SwapHands
AddHandler MyUDO4.UC_MySub, AddressOf SwapHands
The "SwapHands" routine is defined in my main program and accepts the parameters stored in the UC Public event. The "AddressOf" points your resident subroutine.
The only thing to ensure across all these definitions is that you have the same number of parameters (if you have any) in each case, in the same order, and are of the same type).
(Each parameter can be a different type but must "line up" in types declared in each
definition). (e.g. 1 Boolean, 1 String, another string). In the definitions and calls, there have to be (in this case) 3 parameters of "Boolean, String, String" - in that order.
Now when you run the program and click on the GroupBox (or whatever you have used) in your UDO (User defined object) (on any one of the four objects in this case), you will kick off the routine stored in your main program.
Hard to explain, but after taking HOURS to fathom how this works, I thought I would leave my comments in case others are experiencing the same confusion.
I have a made a custom datagridview with custom column objects. One of the columns in an edit button column which has a text box and a button to the right of it. This is a user control. What I am having problems with is when the user presses the button on the control I want an event to be raised on the datagridview which contains this column. I am doing this in winforms and vb.net. If anyone knows how to raise an event from the user control column which has the button to the datagridview which contains the column then it would be greatly appreciated.
Thanks
In your custom control, simply declare an event that the control will raise:
Public Class MyUserControl
Public Event MyEvent as EventHandler
...
'Somewhere in you user control code, you want to raise the event
RaiseEvent MyEvent(Me, EventArgs.Empty) 'using empty args for demo purposes
End Class
Then, in your form, you simply declare an event handler as usual:
Private Sub MyUserControl_MyEvent(sender as Object, e as EventArgs) Handles MyUserControl.MyEvent
... 'Whatever you need to do in reaction to the event
End Sub
Hope this helps