Trouble converting C# code to VB.NET

Marc Menzel 61 Reputation points
2022-04-08T22:45:32.093+00:00

I found some C# code on a website that I am having trouble converting to VB.NET Here is the website link: C# - How to color different words with different colors in a RichTextBox while a user is writing and raise an event when that colored text is clicked - Stack Overflow

The Code is as follows:

public class WordsEventArgs : EventArgs
{
    private string m_word;
    public WordsEventArgs(string word) { m_word = word; }
    public string Word { get { return m_word; } set { m_word = value; } }
}

public delegate void WordsEventHandler(object sender, WordsEventArgs e);
public event WordsEventHandler WordClicked;

protected void OnWordClicked(WordsEventArgs e) => WordClicked?.Invoke(this, e);

this.WordClicked += new WordsEventHandler(this.Word_Click);

public class ColoredWord
{
    public string Word { get; set; }
    public Color WordColor { get; set; }
}

public List<ColoredWord> ColoredWords = new List<ColoredWord>();

public void FillColoredWords()
{
    ColoredWords.Add(new ColoredWord { Word = "SIMPLE", WordColor = Color.Goldenrod });
    ColoredWords.Add(new ColoredWord { Word = "COLORED", WordColor = Color.Salmon });
    ColoredWords.Add(new ColoredWord { Word = "TEXT", WordColor = Color.DarkCyan });
    this.listBox1.DisplayMember = "Word";
    this.listBox1.DataSource = ColoredWords;
}

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    int currentPosition = richTextBox1.SelectionStart;

    if (e.KeyChar == (char)Keys.Space && currentPosition > 0 && richTextBox1.Text.Length > 1) {
        int lastSpacePos = richTextBox1.Text.LastIndexOf((char)Keys.Space, currentPosition - 1);
        lastSpacePos = lastSpacePos > -1 ? lastSpacePos + 1 : 0;

        string lastWord = richTextBox1.Text.Substring(lastSpacePos, currentPosition - (lastSpacePos));
        ColoredWord result = ColoredWords.FirstOrDefault(s => s.Word == lastWord.ToUpper());

        richTextBox1.Select(lastSpacePos, currentPosition - lastSpacePos);
        if (result != null) {
            if (richTextBox1.SelectionColor != result.WordColor) { 
                richTextBox1.SelectionColor = result.WordColor;
            }
        }
        else {
            if (richTextBox1.SelectionColor != richTextBox1.ForeColor) { 
                richTextBox1.SelectionColor = richTextBox1.ForeColor;
            }
        }
        richTextBox1.SelectionStart = currentPosition;
        richTextBox1.SelectionLength = 0;
        richTextBox1.SelectionColor = richTextBox1.ForeColor;
    }
}

private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
    if (richTextBox1.SelectionColor.ToArgb() != richTextBox1.ForeColor.ToArgb()) {
        try {
            int wordInit = richTextBox1.Text.LastIndexOf((char)32, richTextBox1.SelectionStart);
            wordInit = wordInit > -1 ? wordInit : 0;
            int wordEnd = richTextBox1.Text.IndexOf((char)32, richTextBox1.SelectionStart);
            string wordClicked = richTextBox1.Text.Substring(wordInit, wordEnd - wordInit) + Environment.NewLine;
            OnWordClicked(new WordsEventArgs(wordClicked));
        }
        catch (Exception) {
            //Handle a fast DoubleClick: RTB is a bit dumb.
            //Handle a word auto-selection that changes the `.SelectionStart` value
        }
    }
}

private void Word_Click(object sender, WordsEventArgs e)
{
    textBox1.AppendText(e.Word);
}

I am specifically having trouble converting

this.WordClicked += new WordsEventHandler(this.Word_Click);

This code would subscribe to the event as described on the website.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Petrus 【KIM】 456 Reputation points
    2022-04-11T07:32:37.723+00:00

    Follow code must exist in a class function (class constructor)

    this.WordClicked += new WordsEventHandler(this.Word_Click);

    =>
    public YourClassName() {
    this.WordClicked += new WordsEventHandler(this.Word_Click);
    }

    Then use this site to convert C# to VB.NET

    https://converter.telerik.com/

    1 person found this answer helpful.
    0 comments No comments

  2. Jiachen Li-MSFT 26,506 Reputation points Microsoft Vendor
    2022-04-11T07:00:19.84+00:00

    Hi @Marc Menzel ,
    As karenpayneoregon replied, you should use the following code to associates an event with an event handler in VB.

    AddHandler WordClicked, AddressOf Word_Click  
    

    If you have other questions, you can also ask here, we will try to help you.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments