Should I rename my button event handler name? - naming-conventions

When using Visual Studio (though ideally this can apply to the generic case) and double click on a button I've created, the event handler code that is auto generated uses the following signature:
Protected Sub btnSubmitRequest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmitRequest.Click
End Sub
Is it best practice to leave the name of this method as is, or should it be renamed to something a little more descriptive, such as SubmitNewEmployeeRequest?

Create another method called SubmitNewEmployeeRequest. In btnSubmitRequest_Click, call SubmitNewEmployeeRequest. That is the most logical separation of duties.

Also, if you change the name of the button in the IDE before creating the handlers, the handlers get better default names. The name of your button would currently be btnSubmitRequest, you could change it to be more specific as btnSubmitNewEmployeeRequest and then generate the handler.
You should name your controls, and keep the naming consistent between the control and handlers.
I would generally name them within the context of usage, that is if your in the
Employee Request form, then the button need only be named SubmitRequest.
Do Stuff to Directory components form, then the button should probably be more descriptive like SubmitNewEmployeeRequest.

Well, personally I leave it so that I can see quickly that it is an event handler, specifically a click event handler. However I would be inclined to just have one line of code there that calls (in this case, your) SubmitNewEmployeeRequest because this may also be called from some context menu as well, or fired in response to some other event.

Related

Nothing works anymore after building project in vb.net

I was going to build my project when i noticed i didn't put an icon,
and since i couldn't access the icon value of the original form because i used a theme i C+X the container and access it from the grey form, change it, C+V the container, built the project.
Nothing changed, all the name of the buttons and stuff are the same, but i feel like nothing is connected to the code anymore, i don't know what happened, i just recently got re-interested into coding, and i have no idea what to do, i tried some things but nothing worked, so here i am, desperate (i spent 3 days on this, i'm REALLY starting from bottom)
link to the project: http://www.mediafire.com/file/2zrbe32lzpx2qhz/SchedulerProjectVBNET.rar
Thanks in advance
It sounds like you have lost all the Handles clauses off your event handlers. As an example, if you add a Button to your form and double-click it, you will get a Click event handler like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Note the Handles clause at the end, which is what connects the code to the event of the control. If you cut that Button from your form to the Clipboard then it no longer exists as part of the form, so that Handles clause will be automatically removed. If you paste the Button back onto the form, the Handles clause is not restored, so the method no longer handles the event.
If that's what has happened - which you can easily check just by looking at the code - then you need to restore all those Handles clauses. You can do that manually, i.e. write them all yourself in the code window, or you can have the designer regenerate them for you. To do the latter, select a control in the designer, open the Properties window, click the Events button, select the desired event and then select the appropriate method from the drop-down list.
Note that you can also double-click an event there to generate a new event handler, which can be useful for handling events other than the default event. You can generate a handler for the default event simply by double-clicking the control.

sub functions vs event, what is the difference?

I went through events in Vb.net and i found they are really an amazing feature..but still confused about how to use them effectively.
The real doubt is that the functions and sub could be effective alternative for events. Whatever i do with events i can manage to do it via functions and methods. Wherever there is Raisevent, i can substitute it with a function or procesure. so then, what is the real benefit of events and in which cases i can use them instead of functions and procedures?
Let's see if you understand it with an example. You have the Button control. It defines a lot of events (Click,MouseDown,Keypress...). When you put a button in a Form, you decide there what are the events you want to manage. So in the form you can do something when a button is clicked, or not doing anything.
Imagine if this was done with regular methods. In that case, the button should have a reference to the form where the button is, and the form should have a Click method, is it using it or not.
The event driven programing makes very easy to define some Events to signal when something happens in a class, and is the class that creates the instance the one that decides if that event is relevant or not.
thank you for clarification, I got the point. The point I Missed was that : event is fired in the same class, but execution is done in a different class.
I mean, when I define a class, I can put RaiseEvent somewhere to recognize something, but I correlate the event with a sub in a different class.
so, in such a class called Wallet I can put a statement like :
If Dollars > 5 Then
RaiseEvent Above5()
End If
and in such different class called AllWallet :
Dim WithEvents myWallet as new Wallet
Sub myWallet_Above5() Handles myWallet.Above5
MsgBox("Dollars are more than 5")
End Sub
otherwise, I mean if events are fired in the same class they are defined in , the sub and functions can substitute events.
thanks to all

vb.net winform from a usercontrol call a function on the parent form

I don't know how to put it right, but I have a vb.net winform app in which I want to use a customcontrol so I can re-use the logic on multiple forms. I know how to set values in this control from the parent (using properties in the control). But now I want to call a specifiek function on the parent form. Im my case LoadData() which is a procedure of the parentform. How can I do this?
I know I can reference the parent form by using Me.ParentForm in the usercontrol. But I cannot call the LoadData() procedure in the parentform.
Any help? This is a winforms app, not a ASP.NET app.
T.I.A.
[Edit]
I could solve my problem using this example found right here. This is working fine
First, a UserControl is for reusing GUI logic, so I hope that is what you meant. If you are trying to reuse non-GUI logic you might want to create some stand-alone classes for that.
Second, it is generally bad design to call back up to the parent form in the way you describe because it makes the UserControl less reusable and it creates an overly tight binding between the two. You should, if at all possible, push the data down into the UserControl instead.
If you can find no way for the Form to push the data down (perhaps because it is based upon a UI interaction within the UserControl), you have a couple of other options. The first is to wrap up the data-loading behavior into an object that can be passed to the UserControl during initialization, and the the UserControl can access the LoadData method on it as needed. Another approach is to have the UserControl define a set of Events that can be used to request external data from the form. I like to use the "Query" prefix on these kinds of events. So when a fetch button is tapped on your UserControl, it raises the "QueryData" event, and the form that is handling that event responds by populating a data container of some sort that is part of the custom EventArgs.
Either of these can be expanded upon if you need more assistance.
Upon re-reading the question, I it looks like perhaps my approach is over kill. I was under the impression that the LoadData was a method in the Form that loaded data into the UserControl. If it is just a simple call then focus on the event portion of my answer and ignore the data container portion.
I would add an event to your control and handle it on the form the control is part of.
In your control, put something like this:
Public Event LoadData(ByVal sender As Object, ByVal e As System.EventArgs)
And in your form, you can have something like this:
Private Sub UserControl1_LoadData(sender As Object, e As EventArgs) Handles UserControl1.LoadData
'...Your code here
End Sub

About Dynamic Form Design Generating

I'm working in a windows application. using VB.NET 1.1
I have an empty form, and I want to generate my design in load session (not in form_load event! but in my form's constructor)
So I know I must generate my components in constructor, but I don't know how to generate button events. I mean I haven't any button in design mode, and these are generating in run-time mode. so how I set button events in this session?
And if you have a better solution for run-time design generation, give it to me. thanks ;)
A few things.
You may add controls at practically any point. It can be in the constructor, the Load, in response to another event, or when a custom method is called.
You can wire up events by using AddHandler myButton.Click, AddressOf Button_Click. You'll have to define the Button_Click event handlder and it will need to have the appropriate signature, which for a button click is (sender as object, e as EventArgs).
When in doubt, temporarily add a real control to you form and go to the hidden designer code (MyForm.vDesigner.vb) and see what is generated as a sample. Copy that code and move it into the main populating code. Then delete the control.
Good luck!

Access event in a user control created in code behind?

I'm trying to create a user control in my code behind, and then respond to events in that control. Presumably because the control doesn't exist at compile time, Visual Studio can't compile the handler subroutine I created to catch my control's event. Importantly, I want to decide the type of control at runtime (which is why I'm not just hard-coding it).
[before going on, the controls work correctly, including events and event handlers when used in the 'normal' way of creating the controls in XAML. I want to create the control instances in code behind so I can avoid duplicating pages that are 99% identical]
This 'works' (but doesn't give me the flexibility I need):
Public WithEvents AnswerPanel As MyControls.ScrollerControl
... (and the initialisation in the New() sub):
AnswerPanel = New MyControls.ScrollerControl
ItemStack3.Children.Add(AnswerPanel)
AddHandler AnswerPanel.GuessMade, AddressOf CheckAnswer
... (this is the handler sub responding to a custom event in the ScrollerControl)
Public Sub CheckAnswer(answer As String) Handles AnswerPanel.GuessMade
With the code above everything works as I expect: the control is created at runtime and its event is handled correctly.
What I want to achieve is to be able to choose a different user control when I initialise my control (e.g. ScrollerControl2, ScrollerControl3, etc.) I can create the controls this way by changing the first line to:
Public WithEvents AnswerPanel As UserControl
But once that change is made I can no longer reference the custom event in my handler as (presumably) the compiler sees it as a generic UserControl, which doesn't include my custom GuessMade event. The compiler errors on the event name and tells me it doesn't exist.
I'm sure I'm doing something wrong here. I think it's a theory/concept issue rather than my code.
Am I on the right track or going about this in the wrong way?
If I am reading this right, you have a user control that fires an event and you want the parent page to catch that even? If so, you need to raise the event, which will cause the event to bubble to the the parent. IE:
Partial Class user_controls_myControl
Inherits System.Web.UI.UserControl
Public Event DataChange As EventHandler
End Class
This creates a control with a public event called DataChange. Now, if you look at the code in the parent page that instantiates the user control, you will see that it has an event called "OnDataChange". Just like an onCLick event, you can assign this a method in the parent page. Now, you just need to raise the event in the user control. This can be added in some event in the control, like a button click or radio button change event:
RaiseEvent DataChange(Me, New EventArgs)
This takes two objects, the sender and event arguments. Typically I pass ME, which is the user control. This is great because you can use reflection to get all the controls public properties. You can also use this to cast objects to your control type. I rarely pass event arguments but you certainly could.
I answered a similar question here: Handling events of usercontrols within listview
If this is not what you had in mind, let me know
EDIT: To add a user control dynamically and attach the event:
First, in the page that will be using the control, you will need to add a place holder:
<asp:PlaceHolder ID="placeholder1" runat="server"></asp:PlaceHolder>
as well as a reference to the user control at the head of the page (depending on how the page is setup, you may not need this. If you get a page directive error, remove it):
<%# Reference="" Control="~/user_controls/myControl.ascx"%>
In the parent page, you can then create a user control and add it to the place holder. You must declare the user control with events like this:
Private WithEvents myNewControl As New user_controls_myControl
then, in some method you can add it to the page like this:
Dim getPh As New PlaceHolder
'create an instance of the user control
newMyControl = CType(LoadControl("~/user_controls/myControl.ascx"), user_controls_myControl)
'get a handle on the place holder
getPh = me.placeHolder1
'add the user control to the place holder
getPh.Controls.Add(newMyControl)
Then, make sure you have event method:
Protected Sub myEvent(ByVal sender As Object, ByVal e As EventArgs) Handles myNewControl.DataChange
End Sub
So, if you added the RaiseEvent to the user control like I suggested earlier, this should work for you.
I have an answer to this now. As I suspected I was sort of thinking about the problem from the wrong angle.
In a nutshell I was trying to raise an event from my user controls, but I needed to be raising the events in the base class and calling that from my user controls.
So my base class (which my user controls inherit from), now contains the following:
Public Event GuessMade(answer As String)
Protected Sub RaiseGuessEvent(answer As String)
RaiseEvent GuessMade(answer)
End Sub
Then, in my user control(s), when I need to raise the event, I simply call the RaiseGuessEvent sub like this:
Me.RaiseGuessEvent(CurrentValue)
And additionally, I had to remove the event from my subclasses/user controls, of course.