vb.net winforms how raise a custom event to a different control - vb.net

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

Related

Which event on a Control level is equivalent to Form.Load event?

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.

Handle Control in UserControl click

I made a user control, with a picture box and 2 labels. Whenever i click on any of the controls in the user control in a form, the user control click event does not happen. How can i do for when i click any of the labels or the picturebox the usercontrol.click event happens too?
The labels are "label1" and "label2", The picture box is named "pic1".
The usercontrol's name is "Tile".
Please Help!
I have tried something like event handling, using addhandler but it confuses me a little...
Public Event ItemClicked()
`Private Sub Pic1_Clicked(sender As Object, e As EventArgs) Handles Pic1.Click
RaiseEvent ItemClicked()
End Sub`
But i'm stuck there, i don't know what else to do.

VB.NET Windows Forms Variables Passing

I have got a windows form with a Combobox and two datepickers objects in it. I want upon user selection of the values to pass the variables values to another windows form.
All I've seen is how to do this with showDialog method on an instance of the class, however this doesn't work for me as the user has to select a user from Combobox and pick the date range and click on search button.
A quick straight forward help will be appreciated as my time is running close to the deadline.
Thanks.
you can do something like this
1- creation of public event, singleton approach
Public Class EventClass
Private Sub EventClass()
End Sub
Public Shared Sub Invoke(sender as object, value as object)
RaiseEvent OnValueChange(sender,value) ' be sure OnValueChange is not nothing first
End Sub
Public Shared Event OnValueChange(sender as object,value as object)
End Class
2- raising the event, in the form that containing the combobox and datepicker
Handle the event of the combo on selected index changed
inside the event of combo raise the event as
EventClass.Invoke(ComboBox1,ComboBox1.SelectedValue)
and in the case of DateTimePicker use
EventClass.Invoke(DateTimePicker1,DateTimePicker.Date)
3- in the form that you want to pass the values to
public sub EventClass_OnValueChange(sender as object, value as object) handles EventClass.OnValueChange
' do your code here but be sure that this form was created before the form that contains the invoke or it will not be fired
end sub
hope this will help you

Update Combobox

I have a Textbox where you can write a Subject into it and press a button to send the new subject into a database. My combobox shows all subjects in the database and you can choose one and delete it. All programmed with Entity Framework. The Combobox is filled with values during Form.Load-Event.
When I write a new entry in the textbox the combobox has to refresh coz I want the possibility to delete the new entry as well.
I tried
cmb.refresh,
cmb.update
both not working, probably because the code to fill the comboboxes is in the form.load event. So i tried to refresh the form with
me.update
but this is not working too. Any idea how I can update (refill) my combobox?
Okay so take all the code in your Form.load event
Put this code into a new method called UpdateCbo.
Private Sub UpdateCbo()
// your form.load code
End Sub
Then in your form.load event, call this new method.
Public Sub FormLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
UpdateCbo()
End Sub
Then also if you want the combobox to refresh when you add a new entry, you can either hook UpdateCbo up to a button click event or the textchanged event of your combo box.
e.g:
Private Sub Cbo_TextChanged(sender As System.Object, e As System.EventArgs) Handles cbo.TextChanged
UpdateCbo()
End Sub
How do you fill and bind values to the combobox? You can take that code out into a separate function "fillComboBox", and call that upon filling the textbox.
If you post a code sample I can show you what I mean.

VB.Net WinForms UserControl

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.