开发者

Text is selected in the text box

When I load the form where some text has been given to text box. All the text in that textbox is highlighted. I want vb not to load it this way. How to fix i开发者_如何学Got. Thanks Furqna


You could set the tab index on your textbox to something else so that it's not the lowest index.

You could set the TextBox1.SelectionLength = 0 in the form.activated event.

I don't like this as much because if the user had the text hilited and minized the application then they will lose the hilite, but is fairly easy to do. I guess you could use a flag to make sure it only did it on the first activate.

You could set a timer event in the load to clear it immediately after the load event, but that seems like overkill. I have worked at places where they had a standard function that happened on every form 100 ms after load because of problems such as this.


You could try this(it looks like a workaround):

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
    TextBox1.SelectionStart = TextBox1.Text.Length
End Sub

It depends on the TabIndex of your TextBox, if it has the lowest TabIndex it gets focus and therefore it's Text is selected.


' VS.net 2013. Use the "Shown" event.
' GotFocus isn't soon enough.

Private Sub Form_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    TB.SelectionLength = 0
End Sub


Type 1 Method

Dim speech = CreateObject("sapi.spvoice") speech.speak(TextBox1.Text)

Type 2 Method

Dim oVoice As New SpeechLib.SpVoice
    Dim cpFileStream As New SpeechLib.SpFileStream
    'Set the voice type male or female and etc
    oVoice.Voice = oVoice.GetVoices.Item(0)
    'Set the voice volume
    oVoice.Volume = 100
    'Set the text that will be read by computer
    oVoice.Speak(TextBox1.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
    oVoice = Nothing

Type 3 Method

Imports System.Speech.Synthesis

Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim spk As New SpeechSynthesizer For Each voice As InstalledVoice In spk.GetInstalledVoices ListBox1.Items.Add(voice.VoiceInfo.Name) Next ListBox1.SelectedIndex = 0 End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim spk As New SpeechSynthesizer spk.SelectVoice(ListBox1.SelectedItem.ToString) spk.Speak(TextBox1.Text) End Sub End Class


This will also happen sometimes if The TextChanged or other similar Event is fired twice for the control.


When creating each form. Each object is indexed you can set the tab Index higher then the indexed object. Example: On the third form you put a text box in.

private void textBox1_TextChanged(object sender, EventArgs e)

This was the 12th object in the project, it would be indexed at 12. if you put the tab index higher then the indexed objects throughout the project. Tab index 1000 (problem solved.)

Have a great day. Scooter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜