Search code examples
vb.netvisual-studio-2022

VB.net adding a button event handler


I have just downloaded visual studio 2022 and trying to get familiar with it to write using vb.net. I've written million of lines of code in VBA.

  1. I added a button to the main form and under its properties under the flash icon and under ACTION select click and give the handler a name "btn1_click"...but then what? how and where do I write the handler? Tutorials say doubled click that name field which I did no less than 90 times before I posted the question. I tried dbl clicking on the blank field, after typing a name, on the title to the left, on the drop down arrow... NONE WORK

pressing F7 shows the class code for the form only. I assumed VS would generate the interface line. Do I have to type it all out manually? in VBA the "private sub etc etc" and "end sub" is provided when I click "view code" on the button.

  1. anytime I do not click or type anything in VS the window pops up saying "Value cannot be null"? what can it be referring to? In fact it won't even let me save and exit, so I have to lose all edits to be able to exit.

Solution

  • Ok, you don't mention or note what type of project you are attempting to use here.

    I have to assume "webforms", since the fact of using a property sheet suggest web forms.

    Before you continue, make sure you installed the additional bits and parts for webforms. Webforms are now considered "legacy", but they are a fantastic bridge technology for those coming from desktop and VBA + Access land.

    Because VS is now a "monster" massive application? One that allows console apps, desktop apps, web-based apps and more?

    Well, due to this "massive" number of types of projects etc., then not all features and templates and so called "workflows" are installed by default (too big!!!).

    So, first up, make sure you installed the webforms workflows.

    That is from vs go tools->get tools and features.

    The on right side, make sure these are selected:

    Under the ASP.NET and web development tree, make sure you selected all these:

    enter image description here

    I would also select under "individual components" the Microsoft RDLC Report Designer.

    Ok, so assuming all of the above?

    Then let’s drag a label onto the form, then a button.

    You can then double click on the button, and it will jump you to the click event in code behind. However, you can also use the property sheet (like you do say in VB desktop, or even in MS-Access).

    So, say then this:

    enter image description here

    So, the resulting markup on the page is this:

            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <br />
            <asp:Button ID="Button1" runat="server" Height="57px" Text="Button" Width="121px" />
    

    And if I hit F5 to run (just like in VBA), then the result is this:

    enter image description here

    Ok, so let's do a second example.

    Let’s place the numbers from 1 to 10 into our label control above.

    So, in our code behind, we can do this:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        For i = 1 To 10
            Label1.Text = Label1.Text & "<br/>" & i
        Next
    
    End Sub
    

    And thus we now get this (f5) to run:

    enter image description here

    So, a few things:

    I would suggest you try some tutorials. While MUCH of this is VERY similar to VBA (And Access), there are more moving parts in web land. A LOT of moving parts!

    Once you get up to speed, get the hang of this?

    Then you may well want to start using some of the newer technologies.

    So, web forms are great way to "jump" into asp.net web land. And Web Forms is a great "transition" technology in which good amounts of your current concepts in code can and will work. And you don't need a lot of HTML or any JavaScript skills to get up and running with Web Forms.

    As noted, Webforms is now considered older technology, but the time is now or never if you going to jump into web land.

    Without Webforms, then the learning curves for the newer technologies are huge, steep, and you be rather unproductive. So, do think and consider that Webforms is a stepping stone to enter the web land in terms of development.

    Edit: Perhaps this is for windows desktop, and not web based?

    As noted, it is possible that the given question is in regards to WinForms (windows forms), and not web based.

    However, it turns out that the process is much the same for windows forms as it is for the above Web Forms example. So, either way, just make sure this is a windows forms application (not WPF), and if this is web based, then make sure this is a Web Forms application and .net framework (not .net core). Either choice is fine and as above shows, even if this was/is a Web Forms application the standard approach to adding a click event to that button should work.

    So, for Windows forms, then this would work:

    And in palce of using the property sheet, I'll double click on the button, but both approaches can be used (ie: click on the click event in the property sheet, or a double click on the button - both will take you to code behind and the correct code stub.

    enter image description here