Im trying to make my visual basic code speak multiple text boxes with a delay of 30 seconds between each textbox, so far i have:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
End If
If My.Computer.FileSystem.FileExists(OpenFileDialog1.FileName) Then
Dim ioFile As New System.IO.StreamReader(OpenFileDialog1.FileName)
TextBox1.Text = ioFile.ReadLine()
TextBox2.Text = ioFile.ReadLine()
TextBox3.Text = ioFile.ReadLine()
TextBox4.Text = ioFile.ReadLine()
TextBox5.Text = ioFile.ReadLine()
TextBox6.Text = ioFile.ReadLine()
TextBox7.Text = ioFile.ReadLine()
TextBox8.Text = ioFile.ReadLine()
TextBox9.Text = ioFile.ReadLine()
TextBox10.Text = ioFile.ReadLine()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.Speak(TextBox1.Text)
End Sub
End Class
Basically i have 10 text boxes that have 10 words in them (loaded form a txt file) and i have a txt to speech code saying the first textbox but i want that text to speech code linked to button 2 to say all the textboxes with a 30 second delay between each textbox, how would i go about doing this?
For this to work you probably want a multithreaded application. However if you don't a quick and dirty way of making it would would be:
system.threading.thread.sleep(30000)
But then the UI would lock up. So you really want this running on a different thread. A timer is the easiest way to implement this. Just have the timer.tick value set to 30,000. When button 2 is pressed have it create an array with all the textbox strings in. The have a global variable (integer). Each time the timer is ticked it speaks the text in the array at the ndex of the global variable, then increment the global variable!
http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx
Here's an example using a BackgroundWorker() and an Enumerator built from the words in the TextBoxes:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim matches() As Control
Dim lines() As String = System.IO.File.ReadAllLines(OpenFileDialog1.FileName)
For i As Integer = 1 To 10
matches = Me.Controls.Find("TextBox" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
If i <= lines.Count Then
DirectCast(matches(0), TextBox).Text = lines(i - 1)
Else
DirectCast(matches(0), TextBox).Text = ""
End If
End If
Next
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not BackgroundWorker1.IsBusy Then
Dim words As New List(Of String)
Dim matches() As Control
For i As Integer = 1 To 10
matches = Me.Controls.Find("TextBox" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim TB As TextBox = DirectCast(matches(0), TextBox)
If TB.Text.Trim <> "" Then
words.Add(TB.Text.Trim)
End If
End If
Next
If words.Count > 0 Then
BackgroundWorker1.RunWorkerAsync(words.GetEnumerator)
End If
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim SAPI = CreateObject("SAPI.spvoice")
Dim wordsEnum As IEnumerator(Of String) = DirectCast(e.Argument, IEnumerator(Of String))
While wordsEnum.MoveNext
SAPI.Speak(wordsEnum.Current)
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(30).TotalMilliseconds)
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MessageBox.Show("Done!")
End Sub
End Class
Related
enter image description herei am looking for this problem for very long time and I have found nothing about that... :( .
So, I have 2 .csv files. In the .csv file 1 are the listboxes and all of these listboxes have one number between 1 and 6. In the .csv file 2 are some tools like (audi, bmw) and the tools have one numer between 1 and 6 too.
Example:
.csv file 1
Listbox 1 (X,Y,Length,Width) category 1
Listbox 2 (X,Y,Length,Width) category 2
Listbox 3 (X,Y,Length,Width) category 3
.csv file 2
Mercedes category 1
BMW category 2
Audi category 3
So, I want compare the 2 .csv files and allow the listbox 2 for only BMW to drag and drop.
If I drop categroy 2 to category 1 in the listbox, it should be false.
Imports System.Windows.Forms
Imports System.IO
Public Class Form2
Private meineListBoxen As New List(Of ListBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim strZeilen() As String
Dim strFelder() As String
Dim strZeilen0() As String
Dim strZeilen2() As String = IO.File.ReadAllLines("K:\Ruebel_Andreas\Modellpflege\Datenerfassung.csv")
Dim strFelder1() As String
strZeilen = File.ReadAllLines("K:\Ruebel_Andreas\Modellpflege\listboxpflege.csv")
strZeilen0 = IO.File.ReadAllLines("K:\Ruebel_Andreas\Modellpflege\Spritzguss.csv")
For i As Integer = 1 To strZeilen.GetUpperBound(0)
strFelder = strZeilen(i).Split(";")
meineListBoxen.Add(New ListBox)
With meineListBoxen(i - 1)
.Left = strFelder(0)
.Top = strFelder(1)
.Width = strFelder(2)
.Height = strFelder(3)
End With
Me.Controls.Add(meineListBoxen(i - 1))
Me.meineListBoxen(i - 1).AllowDrop = True
AddHandler meineListBoxen(i - 1).MouseDown, AddressOf meineListbox_MouseDown
AddHandler meineListBoxen(i - 1).DragDrop, AddressOf meineListbox_DragDrop
AddHandler meineListBoxen(i - 1).DragEnter, AddressOf meineListbox_DragEnter
Next
For a As Integer = 0 To strZeilen0.GetUpperBound(0)
Me.meineListBoxen(1 - 1).Items.Add(strZeilen0(a).Substring(0, strZeilen0(a).IndexOf(";")))
Next
For j As Integer = 1 To strZeilen2.GetUpperBound(0)
strFelder1 = strZeilen2(j).Split(" ")
With meineListBoxen(j)
.Items.Add(strFelder1(4))
End With
For Each itm In meineListBoxen(j).Items
If meineListBoxen(1 - 1).Items.Contains(itm) Then meineListBoxen(1 - 1).Items.Remove(itm)
Next
Next
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Me.lblMaus.Text = "X: " & e.X & " , Y:" & e.Y
End Sub
Private source As ListBox
Private sourceIndex As Integer
Private Sub meineListbox_MouseDown(ByVal sendre As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim aPoint As Point
Dim lbx As ListBox
Dim aIndex As Integer
lbx = CType(sendre, ListBox)
aPoint = New Point(e.X, e.Y)
aIndex = lbx.IndexFromPoint(aPoint)
Try
If aIndex <= 0 Then
source = lbx
sourceIndex = aIndex
lbx.DoDragDrop(lbx.Items(aIndex), DragDropEffects.All)
End If
Catch ex As Exception
MessageBox.Show("Bitte wählen Sie ein Werkzeug aus")
End Try
End Sub
Private Sub meineListbox_DragDrop(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DragEventArgs)
Dim lbx As ListBox
lbx = CType(sender, ListBox)
If Not source Is Nothing Then
source.Items.RemoveAt(sourceIndex)
End If
lbx.Items.Add(e.Data.GetData(DataFormats.Text))
End Sub
Private Sub meineListbox_DragEnter(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DragEventArgs)
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Speichern.Click
Dim w As New IO.StreamWriter("K:\Ruebel_Andreas\Modellpflege\Datenerfassung.csv")
For i = 0 To meineListBoxen.Count - 1
w.WriteLine(meineListBoxen.Item(i))
Next
w.Close()
End Sub
Private Sub WerkzeugHinzufügen_Click(sender As Object, e As EventArgs) Handles WerkzeugHinzufügen.Click
Process.Start("K:\Ruebel_Andreas\Modellpflege\Spritzguss.csv")
End Sub
Private Sub StellplatzHinzufügen_Click(sender As Object, e As EventArgs) Handles StellplatzHinzufügen.Click
Process.Start("K:\Ruebel_Andreas\Modellpflege\listboxpflege.csv")
End Sub
End Class
Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Speichern.Click
Using w As New StreamWriter("K:\Ruebel_Andreas\Modellpflege\Datenerfassung.csv")
For i = 0 To meineListBoxen.Count - 1
w.WriteLine(meineListBoxen.Item(i))
Next
End Using
End Sub
I find it odd that the Handles clause does not match the name of the method. The default would be Private Sub Speichern_Click
StreamWriter needs to be disposed. Using...End Using blocks will handle this even if there is an error.
Let's say you have 3 list boxes. Your loop will go from index 0 to index 2. WriteLine will call ToString on each item in the list of ListBox. I don't believe that a ListBox provides an implementation of ToString so you will get the fully qualified name of the object's type. I don't think you want this.
Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Speichern.Click
Using w As New StreamWriter("K:\Ruebel_Andreas\Modellpflege\Datenerfassung.csv")
For Each LB As ListBox In meineListBoxen
For Each item As String In LB.Items
w.WriteLine(item)
Next
Next
End Using
End Sub
You can initialize your variables as you declare them and with Option Infer they will be strongly typed (only works for local variables.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim strZeilen2 = File.ReadAllLines("K:\Ruebel_Andreas\Modellpflege\Datenerfassung.csv")
Dim strZeilen = File.ReadAllLines("K:\Ruebel_Andreas\Modellpflege\listboxpflege.csv")
Dim strZeilen0 = File.ReadAllLines("K:\Ruebel_Andreas\Modellpflege\Spritzguss.csv")
For i = 1 To strZeilen.GetUpperBound(0) 'Why are skipping the first line?
Dim strFelder = strZeilen(i).Split(";"c)
Dim LB As New ListBox
With LB
.Left = CInt(strFelder(0))
.Top = CInt(strFelder(1))
.Width = CInt(strFelder(2))
.Height = CInt(strFelder(3))
.AllowDrop = True
.Name = "ListBox" & i 'In case you need to refer to the control be name
End With
If i = 1 Then
For a As Integer = 0 To strZeilen0.GetUpperBound(0)
LB.Items.Add(strZeilen0(a).Substring(0, strZeilen0(a).IndexOf(";")))
Next
End If
AddHandler LB.MouseDown, AddressOf meineListbox_MouseDown
AddHandler LB.DragDrop, AddressOf meineListbox_DragDrop
AddHandler LB.DragEnter, AddressOf meineListbox_DragEnter
'I fleshed out the control before adding to the collections
Controls.Add(LB)
meineListBoxen.Add(LB)
Next
For j = 1 To strZeilen2.GetUpperBound(0) 'Again skipping first line, perhaps it is a title line
Dim strFelder1 = strZeilen2(j).Split(" "c)
meineListBoxen(j).Items.Add(strFelder1(4)) 'You are skipping the first list box
For Each itm In meineListBoxen(j).Items
If meineListBoxen(0).Items.Contains(itm) Then
meineListBoxen(0).Items.Remove(itm)
End If
Next
Next
End Sub
No need to define a point, just pass the coordinates to the IndexFromPoint method. You can use DirectCast to get the ListBox since we are sure the sender is a ListBox. DirectCast skips some of the checks that CType does so the method so is a bit faster. I am not sure if the check on the value of aIndex is necessary. Exception handling is heavy and shouldn't be used for a simple value check.
Private Sub meineListbox_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
Dim lbx = DirectCast(sender, ListBox)
Dim aIndex = lbx.IndexFromPoint(e.X, e.Y)
If aIndex < 0 Then
MessageBox.Show("Bitte wählen Sie ein Werkzeug aus")
Else
source = lbx
sourceIndex = aIndex
lbx.DoDragDrop(lbx.Items(aIndex), DragDropEffects.All)
End If
End Sub
Use IsNot when checking for null objects.
Private Sub meineListbox_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs)
Dim lbx = DirectCast(sender, ListBox)
If source IsNot Nothing Then
source.Items.RemoveAt(sourceIndex)
End If
lbx.Items.Add(e.Data.GetData(DataFormats.Text))
End Sub
Updating this label over and over will just slow things down. Delete this Sub.
'Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
'lblMaus.Text = "X: " & e.X & " , Y: " & e.Y
'End Sub
I can't help more without correct data from your files. At one point, you are splitting by a semicolon and there aren't any in your sample data! The file content you showed is certainly not a CSV file.
I have the following situation in Visual Basic. 5 Label, 4 TextBox and 2 Button according to the picture. TextBox1 + Textbox2 + TextBox3 = TextBox4 and Label4 = TextBox4. Button1 represents TOTAL at (TextBox1 + Textbox2 + TextBox3). Button2, represents SAVE. I want Label5 to show me, after pressing the SAVE Button, the value in TextBox1 and to be saved. This is the code used. Apparently the application works but when I reopen it it doesn't store the value of Label5 and TextBox1. In TextBox1 i want to show, after reopen, the zero value . Please help me!
THIs IS THE CODE
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t1 As Integer
Dim t2 As Integer
Dim t3 As Integer
Dim t4 As Integer
Dim total1 As Integer
If Integer.TryParse(TextBox1.Text, t1) AndAlso Integer.TryParse(TextBox2.Text, t2) AndAlso Integer.TryParse(TextBox3.Text, t3) AndAlso Integer.TryParse(TextBox4.Text, t4) Then
total1 = t1 + t2 + t3 + t4
Else
MessageBox.Show("Introduceti 1 sau 0. Ati introdus o litera")
Exit Sub
End If
TextBox4.Text = total1.ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Label5.Text = TextBox1.Text
My.Settings.QQQ = Label5.Text
My.Settings.QQQ = TextBox1.Text
My.Settings.Save()
Label4.Text = TextBox4.Text
My.Settings.QQQ = Label4.Text
My.Settings.QQQ = TextBox4.Text
My.Settings.Save()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.QQQ
Label5.Text = My.Settings.QQQ
TextBox4.Text = My.Settings.QQQ
Label4.Text = My.Settings.QQQ
If TextBox4.Text = " " Then
Button1.Enabled = False
Else
Button1.Enabled = True
End If
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
If TextBox4.Text = " " Then
Button1.Enabled = False
Else
Button1.Enabled = True
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = " " Then
Button1.Enabled = False
Else
Button1.Enabled = True
End If
End Sub
End Class
First, make sure that your setting QQQ is scoped to User not Application so it can be updated. The Type should be String.
In Button2.Click you set the label and text box to display the same thing. Then you set the setting to equal the label text. Next you overwrite the setting with the text box value. This doesn't make sense. They are the same value.
Then you overwrite it 2 more times. Only the last assignment to settings, My.Settings.QQQ = TextBox4.Text will be preserved at the end of the method.
As a side note, in the TextChanged events, do you really mean a space character? " " I think you may want and empty string "".
Just get rid of TextBox4 completely and use Label4 to hold the total. You don't want the user to be able change the total directly.
What is Label5 doing? Looks like that is unnecessary.
The User Settings are persisted between sessions. When you set up a user setting in the project properties, it is stored in the app.config found in your project folder.
<userSettings>
<TestCode3.My.MySettings>
<setting name="QQQ" serializeAs="String">
<value />
</setting>
</TestCode3.My.MySettings>
</userSettings>
The actual Value is saved to user.config
C:\Users{username}\AppData{ApplicationName}\ continue to drill down until you reach user.config
This is just background information. You do not interact directly with these files.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "0"
Label4.Text = My.Settings.QQQ
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t1, t2, t3, t4 As Integer
Dim total1 As Integer
If Integer.TryParse(TextBox1.Text, t1) AndAlso Integer.TryParse(TextBox2.Text, t2) AndAlso Integer.TryParse(TextBox3.Text, t3) AndAlso Integer.TryParse(TextBox4.Text, t4) Then
total1 = t1 + t2 + t3 + t4
Else
MessageBox.Show("Introduceti 1 sau 0. Ati introdus o litera")
Exit Sub
End If
Label4.Text = total1.ToString
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Settings.QQQ = Label4.Text
My.Settings.Save()
End Sub
I think your main problem is overwriting the single setting you created.
This is the question
=If the FN button
will be click, then it will display a message base on the status of the textbox1 control. For the MN button, if
the event is click then it will display a message base on the status of the textbox2 control. The LN button
was clicked then it will display a message base on the status of the textbox3 control. Lastly, if the Clear
button control will be click and any of the textbox control is not empty then it will clear all the textbox
controls, but if all textbox controls are empty then it will show a message that all textbox controls are empty.
this is my code
Public Class Form1
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Label1.Text = "First Name"
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
Label2.Text = "Middle Name"
End Sub
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
Label3.Text = "Last Name"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Text = "FN"
If TextBox1.Text = vbNullString Then
MessageBox.Show("The First name is empty")
Else MessageBox.Show("The First name is " & TextBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button2.Text = "MN"
If TextBox2.Text = vbNullString Then
MessageBox.Show("The Middle name is empty")
Else : MessageBox.Show("The Middle name is " & TextBox2.Text)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Button3.Text = "LN"
If TextBox3.Text = vbNullString Then
MessageBox.Show("The Last name is empty")
Else : MessageBox.Show("The Last name is " & TextBox3.Text)
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Button4.Text = "Clear"
If (TextBox1.Text) And (TextBox2.Text) = vbNullString Then
MessageBox.Show("Textbox control is empty")
Else : TextBox1.Clear()
End If
For Each txt As Control In Me.Controls.OfType(Of TextBox)()
txt.Text = ""
Next
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
End Class
My question is , how to work on clear button because i cant do it in Textbox2 and Textbox3 thankyou
Try something like this
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
Try to add Next txt to your For Each
So, you are asking about Button4_Click.
This is what I was about to suggest first, because clearing all text boxes whether they are empty or not, is faster than first checking and then clearing.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Button4.Text = "Clear"
' If (TextBox1.Text) And (TextBox2.Text) = vbNullString Then
' MessageBox.Show("Textbox control is empty")
' Else : TextBox1.Clear()
' End If
For Each txt As Control In Me.Controls.OfType(Of TextBox)()
txt.Text = ""
Next
MessageBox.Show("Textbox controls are empty")
End Sub
But then, if the intention is to a) just clear all textboxes, without any message if any one has some text. b) show the message only if all are empty.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Button4.Text = "Clear"
If (TextBox1.Text = vbNullString) And (TextBox2.Text = vbNullString) And (TextBox3.Text = vbNullString) Then
MessageBox.Show("Textbox controls are empty")
Else
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End If
or (if this works, VB6 is alien for me)
....
Else
For Each txt As Control In Me.Controls.OfType(Of TextBox)()
txt.Text = ""
Next
End If
End Sub
I'd use a List(Of TextBox) allowing you to leverage All and ForEach:
Public Class Form1
Private TBs As New List(Of TextBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TBs.AddRange({TextBox1, TextBox2, TextBox3})
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If TBs.All(Function(tb) tb.Text = "") Then
MessageBox.Show("All TextBoxes are empty!")
Else
TBs.ForEach(Sub(tb) tb.Clear())
End If
End Sub
End Class
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Button4.Text = "Clear"
If (TextBox1.Text = vbNullString) And (TextBox2.Text = vbNullString) And (TextBox3.Text = vbNullString) Then
MessageBox.Show("Textbox controls are empty")
Else
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End If
thanks sir Tom Brunberg, this code works for me :)
I have textbox1.text as the main textbox. Any text that is input into textbox1.text when the submit button is selected is supposed to input the text into the next available textbox which is textbox2.text, textbox3.text and textbox4.text. How can I tell vb.net that if textbox2.text is full then input the text into textbox3.text and if textbox3.text is full to input the text into textbox4.text. Here is where I got stuck. I'm still in the process of learning more about vb.net.
Private Sub SubBttn_Click(sender As Object, e As EventArgs) Handles SubBttn.Click
If TextBox1.Text.Trim.Length > 0 Then
TextBox2.Text = TextBox1.Text
If TextBox2.Text.Trim.Length > 0 Then
TextBox3.Text = TextBox1.Text
End If
End If
End Sub
More compact code to do the job
Private Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
Dim LstTextBox As New List(Of TextBox) 'A list to contain text boxes excluding the main text box
LstTextBox.Add(TextBox2)
LstTextBox.Add(TextBox3)
LstTextBox.Add(TextBox4)
For Each Tbox As TextBox In LstTextBox
If String.IsNullOrWhiteSpace(Tbox.Text) = True Then 'If text box is empty or contains white spaces
Tbox.Text = TextBox1.Text 'Copies text from main text box
TextBox1.Clear() 'Clears main text box
Exit Sub
End If
Next
End Sub
You can compare the textbox's textlength with the textbox's max length, and if they match up, the next
Private Sub TextBox1_TextChanged(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If TextBox1.TextLength = TextBox1.MaxLength - 1 Then
TextBox2.Focus()
End If
End Sub
This will switch focus to textbox2 as soon as the last digit is typed.
If you want it in a button event, then do the same as above, but without the - 1
If you want to keep the text the same throughout, then do this:
Private Sub SubBttn_Click(sender As Object, e As EventArgs) Handles SubBttn.Click
If TextBox1.TextLength = TextBox1.MaxLength Then
TextBox2.Text = TextBox1.Text
TextBox2.Focus()
End If
End Sub
You would repeat the same action for each textbox, for example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.TextLength = TextBox1.MaxLength Then
TextBox2.Text = TextBox1.Text
TextBox2.Focus()
End If
If TextBox2.TextLength = TextBox2.MaxLength Then
TextBox3.Text = TextBox2.Text
TextBox3.Focus()
End If
If TextBox3.TextLength = TextBox3.MaxLength Then
TextBox4.Text = TextBox3.Text
TextBox4.Focus()
End If
End Sub
Hope this helps!
I am using a DataSource in my form app, and it was working fine for calls made across the board, Fill, Add, Delete, etc... It suddenly stopped working. I get no errors on build, no data is added to any ComboBoxes, and no new Adds work either.
I created a new DataSource from the same database that works fine with the exact same connection. The location of the database never moved, no changes were made to any properties of the DataSource or any of the Adapters assigned to the source, it just stopped working. Here is some screen shots and code of my form.
I tried to do a Code Compare but since there are a bunch of Adapters assigned to the source I can't find any anomalies. What would kill a data connection so that the code still sees the connection, but nothing gets filled?
The following code no longer works, no Fill or Add to DCGDataSet;
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
'TODO: This line of code loads data into the 'DCGDataSet1.Main' table. You can move, or remove it, as needed.
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
'comboClear()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As System.EventArgs)
' Add new Job to the database
Dim newJobRow As New DCGDataSetTableAdapters.MainTableAdapter
Dim intInsert As Integer
Dim jobText = txtBoxAddNewJob.Text
intInsert = newJobRow.InsertJob(jobText)
If intInsert = 1 Then
MessageBox.Show("New Job Added")
' Update the comboBox values
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
txtBoxAddNewJob.Text = ""
clearTabOne()
Else
MessageBox.Show("Job Not Added")
End If
End Sub
The following call for a ComboBox works fine, this is the new DataSource;
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DCGDataSet1.Main' table. You can move, or remove it, as needed.
Me.MainTableAdapter1.Fill(Me.DCGDataSet1.Main)
End Sub
These pics are of the two DS I am working with, the top one is the non-working DS.
Entire .vb of Form1 Code;
Public Class MainForm
Dim strCurrency As String = ""
Dim acceptableKey As Boolean = False
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DCGDataSet1.Main' table. You can move, or remove it, as needed.
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
comboClear()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As System.EventArgs)
' Add new Job to the database
Dim newJobRow As New DCGDataSetTableAdapters.MainTableAdapter
Dim intInsert As Integer
Dim jobText = txtBoxAddNewJob.Text
intInsert = newJobRow.InsertJob(jobText)
If intInsert = 1 Then
MessageBox.Show("New Job Added")
' Update the comboBox values
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
txtBoxAddNewJob.Text = ""
clearTabOne()
Else
MessageBox.Show("Job Not Added")
End If
End Sub
Private Sub TabPage2_Enter(sender As Object, e As System.EventArgs)
Me.ActiveControl = txtBoxAddNewJob
clearTabOne()
End Sub
Public Sub comboClear()
ComboBox1.SelectedIndex = -1
ComboBox2.SelectedIndex = -1
End Sub
Private Sub TabPage1_Enter(sender As Object, e As System.EventArgs)
'comboClear()
ComboBox1.SelectedIndex = -1
'ComboBox1.SelectedText = ""
End Sub
Private Sub TabPage3_Enter(sender As Object, e As System.EventArgs)
'comboClear()
ComboBox2.SelectedIndex = -1
clearTabOne()
End Sub
Private Sub btnDeleteJob_Click(sender As Object, e As System.EventArgs)
Dim delJobID = ComboBox2.SelectedValue
Dim delJobRowAdpt As New DCGDataSetTableAdapters.MainTableAdapter
Dim delJobRow As DCGDataSet.MainRow
Dim intDelete As Integer
delJobRow = DCGDataSet.Main.FindByID(delJobID)
delJobRow.Delete()
intDelete = delJobRowAdpt.Update(DCGDataSet.Main)
If intDelete = 1 Then
MessageBox.Show("Job Deleted")
'comboClear()
clearTabOne()
'ComboBox2.SelectedValue = -1
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
Else
MessageBox.Show("Job Failed to Delete")
End If
ComboBox2.SelectedValue = -1
End Sub
Private Sub FillSubCombo(ByVal subJob As String)
Dim selSubRow = DCGDataSet.SubBilling
Dim selSubValue As New DCGDataSetTableAdapters.SubBillingTableAdapter
Me.SubBillingTableAdapter.SubName(Me.DCGDataSet.SubBilling, subJob)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs)
Dim subJob As String = ComboBox1.Text
If subJob.Length > 1 Then
Label5.Visible = True
ComboBox3.Visible = True
FillSubCombo(subJob)
End If
End Sub
Public Sub clearTabOne()
Label5.Visible = False
ComboBox3.Visible = False
End Sub
Private Sub TabPage4_Enter(sender As Object, e As System.EventArgs)
clearTabOne()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
acceptableKey = True
Else
acceptableKey = False
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' Check for the flag being set in the KeyDown event.
If acceptableKey = False Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
Return
Else
If e.KeyChar = Convert.ToChar(Keys.Back) Then
If strCurrency.Length > 0 Then
strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
End If
Else
strCurrency = strCurrency & e.KeyChar
End If
If strCurrency.Length = 0 Then
TextBox1.Text = ""
ElseIf strCurrency.Length = 1 Then
TextBox1.Text = "0.0" & strCurrency
ElseIf strCurrency.Length = 2 Then
TextBox1.Text = "0." & strCurrency
ElseIf strCurrency.Length > 2 Then
TextBox1.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
End If
TextBox1.Select(TextBox1.Text.Length, 0)
End If
e.Handled = True
End Sub
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DCGDataSet1.Main' table. You can move, or remove it, as needed.
Me.MainTableAdapter1.Fill(Me.DCGDataSet1.Main)
End Sub
End Class
Your not-working code is missing the Handles clauses on Form1_Load and btnAddNew_Click. If you don't connect the event handler to the event (using either a Handles clause or an AddHandler statement), the event handler won't be run.