Home > database >  problem with textbox TextChanged inside userControl (C# code to vb.net)
problem with textbox TextChanged inside userControl (C# code to vb.net)

Time:11-17

I'm trying to create a custom textbox with a userControl, my problem lies in the part of setting the default event of the userControl to handle the textChanged of the textbox. I currently have the userControl with only one textbox.

In c # I have this code that works perfectly (UserControl3.cs file):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace custom_controls
{
    [DefaultEvent("_TextChanged")]
    public partial class UserControl3 : UserControl
    {
        public UserControl3()
        {
            InitializeComponent();
        }

        public event EventHandler _TextChanged;

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            if (_TextChanged != null)
            {
                _TextChanged.Invoke(sender, e);
            }
        }
    }
}

But I have not been able to translate it correctly to vb.net where I need the control. Any help or suggestion is greatly appreciated.

This is the code resulting from the conversion from C # to VB.net:

<DefaultEvent("_TextChanged")>
Public Class UserControl3

Public Event _TextChanged As EventHandler

    Private Sub textBox1_TextChanged(sender As Object, e As EventArgs) Handles textBox1.TextChanged
        If _TextChanged IsNot Nothing Then 'error here: _TextChanged 
            _TextChanged.Invoke(sender, e) 'error here: _TextChanged 
        End If
    End Sub
End Class

Error image:

enter image description here

CodePudding user response:

You can't raise events the same way in vb and c#. vb has a keyword RaiseEvent to do that

<DefaultEvent("_TextChanged")>
Public Class UserControl3

    Public Event _TextChanged As EventHandler

    Private Sub textBox1_TextChanged(sender As Object, e As EventArgs) Handles textBox1.TextChanged
        RaiseEvent _TextChanged(sender, e)
    End Sub
End Class
  • Related