Text is selected in the text box - vb.net

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 it.
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

Related

Form_Load doesn't execute in application

I am new to Visual Basic. I have installed Microsoft Visual Studio 2010. Created a new Windows Form Application. As an example, I made a simple program which will ask the end user to input 2 numbers and allow them to either add them or subtract the second number from the first one and display the output in a Textbox.
Now, I added another Subroutine which would be executed automatically when the Windows Form loads. This would calculate the width of the output Textbox and the Form Width and display at the bottom.
This is how the code looks like right now:
Public Class Form1
' Run this Subroutine initially to display the Form and Text box width
Private Sub Form_Load()
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a + b
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a - b
End Sub
End Class
While everything works correctly for the addition and subtraction, it does not display the Form and output Textbox width in the Windows Form.
I think, Form_Load() is not executing properly.
I also tried, Form_Activate() but that did not work either.
Once I am able to do this, I would like to extend this concept to resize the output Textbox along with the Form resize. However, for the purpose of understanding I wanted to see if I can execute Form_Load() successfully.
Thanks.
Form_Load doesn’t execute. For now, it’s just any other method. In order to tell VB to make this method handle the Load event, you need to tell it so:
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Loasd
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
(And add the required parameters for the event.)
A few other remarks:
Ensure that Option Strict On is enabled in your project options at all times. This will make the compiler much stricter with your code and flag more errors. This is a good thing since these errors are potential bugs. In particular, your code is very lax with conversions between different data types, these should be made explicit.
Initialise variables when you declare them, don’t assign a value in a separate statement. That is, write this:
Dim a As Integer = Integer.Parse(TextBox1.Text)
(Explicit conversion added as well.)
If you want to make a control fill the form, you can just set its Dock property appropriately in the forms editor, instead of having to program this manually.
You need to add the Handle so the app executes it automatically:
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
'...
End Sub

e.target in VB.net

When using flash, I was able to get to the focus of an event by accessing the event's "target" attribute.
so if I remember, it was something similar to.
button1.addEventListener(mouse_click, doSomething);
doSomething(e: Event){
e.target.size = 50000;
}
And I'm looking for the equivalent in VB.
If you can give me it's common name across all languages, I'd be doubly grateful. I don't quite know what to search for aside from "event.target VB.net equivalent, and that's not returning anything.
Thanks in advance.
edit: for those new to flash. By focus, I mean the physical object that was clicked on. So the example given would be accessing the clicked button's size.
In VB you can wire up event handlers declaratively using the WithEvents keyword or imperatively using AddHandler.
Private WithEvents myButton
' OR
Public Sub New
Dim newButton = New Button()
AddHandler newButton.Click, AddressOf MyClickHandler
End New
'To consume it you declare a method as follows:
' The Handles clause is used when declaring WithEvents
Private Sub MyClickHandler(sender As Object, e As EventArgs) Handles myButton.Click
' The sender has a handle on the object that raised the event (aka the button)
Dim btn = DirectCast(sender, Button)
btn.Size = New Size(500, 500)
End Sub
Got it!
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclick.aspx#Y0
Sub GreetingBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
'When the button is clicked,
'change the button text, and disable it.
Dim clickedButton As Button = sender
clickedButton.Text = "...button clicked..."
clickedButton.Enabled = False
End Sub
The first parameter (sender by default) references the focused object. You can access it as you would any other normal variable, but it's information won't appear in the auto complete list unless you set it "As" that specific data type.
So I ended up with this
Private Sub nw_btn_Click(ByVal sender As System.Windows.Forms.Button, ByVal e AsSystem.EventArgs) Handles nw_btn.Click
sender.Hide()
End Sub

Need help using Checkbox

i have a couple of questions about my basic project
if i have a check box and when its checked, it is going to a text box which is displaying the price, when i uncheck it my price still says in that text box, how can i make it dissapear as i uncheck the box?
Dim total As Double
If rb_s1.Checked = True Then
total += 650.0
txt_1.Text = total
thats my code.
and i have many combo boxes, how can i make them all add up as i check/uncheck them.
I would add this functionality into the CheckBox_Changed event handler. This way you can tell if it is unchecked or checked and add or subtract the value from price.
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
total += 650.00
Else
total -= 650.00
End If
TextBox1.Text = total.ToString()
End Sub
You have to use Checked_Changed event of checkbox.
SHARED void CheckBox1_CheckedChanged(object sender, EventArgs e)
IF ChkBx.Checked = true then
textBox1.text = "1500"
else
textBox1.text = ""
END IF
END SUB
To get your displayed text to change when the state of your checkbox changes, you'll need to handle the CheckedChanged event. In Visual Studio while in Desginer mode for your form/control, you can select the check box control, and then in the Properties window, select the Events tab (the one with the little lightingbolt icon), and double click the CheckChanged event to stub in an event handler method AND attach the event to the handler.
ETA: I re-reading this, I'm not sure how clear I was. When I mentioned stubbing in the event handler and attaching the event to the handler, I meant that going the route of double-clicking the event in the designer will do this for you.
As an aside, it sounds like you want the text to be a sum of only the checked items, so from an architechtueral sense, I would recommend creating a single method to determine the sum, and have all check-box check events invoke that method rather than trying to make the event handler method itself do too much directly (maybe that was already clear to you).
So you might do something like this:
Public Class Form1
Private Sub DisplayTotal()
Dim total As Decimal = 0
If (CheckBox1.Checked) Then
total += Decimal.Parse(txtItem1.Text)
End If
'Add other items
txtTotal.Text = total
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
DisplayTotal()
End Sub
Private Sub CheckBox2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
DisplayTotal()
End Sub
End Class

visual basic :Help removing items from listbox

I have to make a listbox with a few(8) names in it & double clicking on a name in the listbox will removed the name from it.
I have already add the names into the form using the listbox.items.add method & would display the names in it.
Then I enter the coding for 8 names in double_click procedure(listbox) using the "listbox.items.remove" method.
However, when i try double clicking on a name in the listbox, it would remove all the names instead.
What coding do i need? help appreciated!
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Clear()
ListBox1.Items.Add("1")
ListBox1.Items.Add("2")
ListBox1.Items.Add("3")
ListBox1.Items.Add("4")
ListBox1.Items.Add("5")
ListBox1.Items.Add("6")
ListBox1.Items.Add("7")
ListBox1.Items.Add("8")
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim i As Integer = ListBox1.SelectedIndex
If i >= 0 And i < ListBox1.Items.Count Then
ListBox1.Items.RemoveAt(i)
End If
End Sub
End Class
if you are looking at dynamic deletion of items, i think you should check out Jquery,Ajax,DOM
there are a couple of nice tutorials that would help you with that. i just came across this one and found it interesting
http://www.satya-weblog.com/2010/02/add-input-fields-dynamically-to-form-using-javascript.html

Search ListBox elements in VB.Net

I'm migrating an application from VB6 to VB.Net and I found a change in the behavior of the ListBox and I'm not sure of how to make it equal to VB6.
The problem is this:
In the VB6 app, when the ListBox is focused and I type into it, the list selects the element that matches what I type. e.g. If the list contains a list of countries and I type "ita", "Italy" will be selected in the listbox.
The problem is that with the .Net version of the control if I type "ita" it will select the first element that starts with i, then the first element that starts with "t" and finally the first element that starts with "a".
So, any idea on how to get the original behavior? (I'm thinking in some property that I'm not seeing by some reason or something like that)
I really don't want to write an event handler for this (which btw, wouldn't be trivial).
Thanks a lot!
I shared willw's frustration. This is what I came up with. Add a class called ListBoxTypeAhead to your project and include this code. Then use this class as a control on your form. It traps keyboard input and moves the selected item they way the old VB6 listbox did. You can take out the timer if you wish. It mimics the behavior of keyboard input in Windows explorer.
Public Class ListBoxTypeAhead
Inherits ListBox
Dim Buffer As String
Dim WithEvents Timer1 As New Timer
Private Sub ListBoxTypeAhead_KeyDown(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.A To Keys.Z, Keys.NumPad0 To Keys.NumPad9
e.SuppressKeyPress = True
Buffer &= Chr(e.KeyValue)
Me.SelectedIndex = Me.FindString(Buffer)
Timer1.Start()
Case Else
Timer1.Stop()
Buffer = ""
End Select
End Sub
Private Sub ListBoxTypeAhead_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.LostFocus
Timer1.Stop()
Buffer = ""
End Sub
Public Sub New()
Timer1.Interval = 2000
End Sub
Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Buffer = ""
End Sub
End Class
As you probably know, this feature is called 'type ahead,' and it's not built into the Winform ListBox (so you're not missing a property).
You can get the type-ahead functionality on the ListView control if you set its View property to List.
Public Function CheckIfExistInCombo(ByVal objCombo As Object, ByVal TextToFind As String) As Boolean
Dim NumOfItems As Object 'The Number Of Items In ComboBox
Dim IndexNum As Integer 'Index
NumOfItems = objCombo.ListCount
For IndexNum = 0 To NumOfItems - 1
If objCombo.List(IndexNum) = TextToFind Then
CheckIfExistInCombo = True
Exit Function
End If
Next IndexNum
CheckIfExistInCombo = False
End Function