Hey guys. I need some help. I'm making a Text to Speech program. I've made a couple versions of them and they all work pretty good. But the new version I'm making has an icon that displays in the tray. I need to know how to call my Rich Text Box in my form, from a different class. Like this:

Public Class SpeakMenuItem

    Inherits MenuItem

    ' Constructor...
    Public Sub New()

        Text = "Text to Speech"

    End Sub

    ' OnClick...
    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)

        Dim strText As String
        strText = (RichTextBox1.Text)
        Dim objVoice = CreateObject("SAPI.SpVoice")
        objVoice.Speak(strText)
        If RichTextBox1.Text() = "" Then
            MsgBox("You need to put text in the textbox before I speak it!", MsgBoxStyle.OkOnly, "Robot")
        End If

    End Sub

End Class

Can anyone help? Thanks in advance!

Recommended Answers

All 12 Replies

I didn't understand what's your problem/doubt.

i have a program. i made a tray icon to go on the bottom right of the screen. i does and when i click on it it shows a close button and a text to speech button. the close button works, and im trying to find a way to call the richtextbox from the text to speech button class. so i can put speech in the box then press the tray menu item and convert it from text to speech. i allready thought of the code to convert it, and it works, and the code to show the menu item on the tray, works, but i need the call the richtextbox from the menu item class in order to convert the text.

Can't you bring up a form with the text when clicking the button?

yes i allready did but i also want there to be a button on the tray.

Pass a reference to the RTB in in custom menu item's constructor or add a RTB property to it.

Private RTB as RichTextBox
Public Sub New(RTB as RichtextBox)
    Me.New()
    Me.RTB = RTB
End Sub

or

Private _RTB As RichTextBox
Public Property RTB() As RichTextBox
    Get
        return _RTB
    End Get
    Set (value as RTB)
        _RTB = Value
    End Get
End Property

Then use RTB as your reference in your code

ok. i have a form. there is a rich text box on it called rtbText. now i also have a tray item for it. when i click it it show a text to speech button, a seperator, and an exit button. there is a class for each menu item except the seperator. this is Class CloseMenuItem:

Public Class CloseMenuItem
    Inherits MenuItem

    ' Constructor...
    Public Sub New()
        Text = "Close"
    End Sub

    ' OnClick...
    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        Application.Exit()
    End Sub
End Class

and this is Class SpeakMenuItem:

Public Class SpeakMenuItem

    Inherits MenuItem

    ' Constructor...
    Public Sub New()

        Text = "Text to Speech"

    End Sub

    ' OnClick...
    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)

        Dim strText As String
        strText = (rtbText.Text)
        Dim objVoice = CreateObject("SAPI.SpVoice")
        objVoice.Speak(strText)
        If rtbText.Text() = "" Then
            MsgBox("You need to put text in the textbox before I speak it!", MsgBoxStyle.OkOnly, "Robot")
        End If

    End Sub

End Class

when i say:

strText = (rtbText.Text)

or:

If rtbText.Text() = "" Then

it has an error line under rtbText.Text. that error says that "'rtbText' is not declared. It may be inaccesable due to its protection level." im trying to get it to recognize rtbText on my form. ive tried to change the modifier property of the rtbText to Public but it doesn't help. so i need help on how to make it accesable.

try

Dim strText As String
strText = Form1.RichTextBox1.Text
If strText = "" Then
    MsgBox("You need to put text in the textbox before I speak it!", MsgBoxStyle.OkOnly, "Robot")
Else
    Dim objVoice = CreateObject("SAPI.SpVoice")
    objVoice.Speak(strText)
End If

Change 'Form1' to the name of your form. By default controls are declared as Friend. You should be able to call it from any where in your project by qualifying it with the form name.

thx. i shouldv'e marked this as solved before u answered cause i made up that code about 2 days ago but that is the code i used. thx.

but now i have another problem. when i have the code:

Public Class SpeakMenuItem

    Inherits MenuItem

    ' Constructor...
    Public Sub New()

        Text = "Text to Speech"

    End Sub

    ' OnClick...
    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)



        Dim strText As String
        strText = (frmMain.rtbText.Text)
        Dim objVoice = CreateObject("SAPI.SpVoice")
        objVoice.Speak(strText)
        If frmMain.rtbText.Text() = "" Then
            MsgBox("You need to put text in the textbox before I speak it!", MsgBoxStyle.OkOnly, "Robot")
        End If

    End Sub

End Class

and i click the button, it comes up with the msgbox saying i need to put text in the textbox. i put the text in and it shows up. i know why. its cause when i click the button it thinks that the variable strText is the text in the textbox when the form loaded. can someone please tell me what the code is the refresh the button so it know what the text in the textbox is?

First off you've kind of got things a little out of order. You're calling the speech object before you check if there is any text. If you look at the code I gave you, you'll see simple way of doing it in the proper order.

The way it's coded, strText will only get a value if the button is clicked, and it will get the current text from frmMain.rtbText. If you try my code it should work.

If not, perhaps you're over thinking things by overriding the click event. You can easily add a handler for the click event in the form class, that will do the same thing.

i did your code it didnt work. i changed back modifier on rich text box to friend it didnt work. heres code for button now:

Public Class SpeakMenuItem

    Inherits MenuItem

    ' Constructor...
    Public Sub New()

        Text = "Text to Speech"

    End Sub

    ' OnClick...
    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)



        Dim strText As String
        strText = Form1.RichTextBox1.Text
        If strText = "" Then
            MsgBox("You need to put text in the textbox before I speak it!", MsgBoxStyle.OkOnly, "Robot")
        Else
            Dim objVoice = CreateObject("SAPI.SpVoice")
            objVoice.Speak(strText)
        End If

    End Sub

End Class

heres code in form for calling tray menu items:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

        ' create a new context menu...
        Dim menu As New ContextMenu()

        ' add a speak button...
        menu.MenuItems.Add(New SpeakMenuItem())

        ' add a separator...
        menu.MenuItems.Add("-")

        ' add an exit button...
        menu.MenuItems.Add(New CloseMenuItem())

        ' tell the tray icon to use this menu...
        NotifyIcon1.ContextMenu = menu

    End Sub

and heres a picture of the form running when the tray icon is clicked:

Robot_Talk_Tray_for_Daniweb1

When you say it's not working, you'll have to be more specific. Is nothing happening, is the wrong thing happening, is there an error?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.