vb code works c# doesn't - howcum?

form1.vb works
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object,
                       ByVal e As System.EventArgs) Handles MyBase.Load

        ' Create the list to use as the custom source. 
        Dim MySource As New AutoCompleteStringCollection()
        MySource.AddRange(New String() _
                            {
                                "January",
                                "February",
                                "March",
                                "April",
                                "May",
                                "June",
                                "July",
                                "August",
                                "September",
                                "October",
                                "November",
                                "December"
                            })

        ' Create and initialize the text box. 
        Dim MyTextBox As New TextBox()
        With MyTextBox
            .AutoCompleteCustomSource = MySource
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            .Location = New Point(20, 20)
            .Width = Me.ClientRectangle.Width - 40
            .Visible = True
        End With

        ' Add the text box to the form. 
        Me.Controls.Add(MyTextBox)
    End Sub
End Class

Open in new window

form1.cs doesn't work
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Create the list to use as the custom source.  
            var source = new AutoCompleteStringCollection();
            source.AddRange(new string[]
                            {
                        "January",
                        "February",
                        "March",
                        "April",
                        "May",
                        "June",
                        "July",
                        "August",
                        "September",
                        "October",
                        "November",
                        "December"
                            });

            // Create and initialize the text box. 
            var textBox = new TextBox
            {
                AutoCompleteCustomSource = source,
                AutoCompleteMode =
                                  AutoCompleteMode.SuggestAppend,
                AutoCompleteSource =
                                  AutoCompleteSource.CustomSource,
                Location = new Point(20, 20),
                Width = ClientRectangle.Width - 40,
                Visible = true
            };

            // Add the text box to the form.
            Controls.Add(textBox);
        }
    }
}

Open in new window

My thinking is that it is in the form_load event that isn't being called
LVL 90
David Johnson, CD, MVPOwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
My thinking is that it is in the form_load event that isn't being called
What gives you that idea? Have you debugged through? If so, is the handler not being invoked? If so, then how did you add the handler? Adding the method alone isn't enough; you have to actually associate the handler to the event. You either do this via the designer and the "Events" tab (in the Properties window), or you use the add-handler syntax.
Éric MoreauSenior .Net ConsultantCommented:
VB has the Handles statement that links the method to the event. C# doesn't have this syntax.

In your C# constructor, add this line (after InitializeComponent();):

this.Load += Form1_Load();

Open in new window

käµfm³d 👽Commented:
Isn't that what I said?
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

David Johnson, CD, MVPOwnerAuthor Commented:
public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load();
        }
Error      1      No overload for method 'Form1_Load' takes 0 arguments      D:\Documents\Visual Studio 2015\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs      12      26      WindowsFormsApplication2
Éric MoreauSenior .Net ConsultantCommented:
my mistake, remove the ()

this.Load += Form1_Load;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerAuthor Commented:
Thank you I learned something today
käµfm³d 👽Commented:
In an effort to provide better answers in the future, was there something about my post that was not clear?
David Johnson, CD, MVPOwnerAuthor Commented:
the add handler you pointed to was for webforms and I was using a winform (windows form). I was using sample code from msdn which had both the VB and the C# version.. the VB version worked like a charm but the C# version didn't work at all
käµfm³d 👽Commented:
No, the add handler syntax is for both. I overlooked the bit about the article being described as "ASP.NET," but the syntax is exactly the same. Admittedly, the bit about "new EventHandler" could be claimed to be different, but the reality is that Eric's code does exactly the same thing, it just does the "new EventHandler" piece behind the scenes.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.