SendKeys command in a TextBox?
I'm trying to read a textfile line by line, because each line is a data for mi program (integer numbers).
After reading each number, I want to add the number to a textbox and then send the "Enter" key to that textbox. Read the next number and send the enter key...
I'm trying to do this but it seems there is some kind of error:
Dim Nombre_Archivo As String = OpenFileDialog1.FileName
Dim TextLine As String
If System.IO.File.Exists(Nombre_Archivo) = True Then
Dim ObjReader As New System.IO.StreamReader(Nombre_Archivo)
Do While ObjReader.Peek() <> -1
'TextLine = TextLine & ObjReader.ReadLine() & vbNewLine
TEXTBOX_NumeroSalido.Focus()
TextLine = ObjReader.ReadLine() & vbNewLine
LISTBOX_Consejo.Items.Add(TextLine)
TEXTBOX_NumeroSalido.Text = TextLine
SendKeys.Send("{ENTER}")
Loop
'TextLine = ObjReader.ReadLine()
'TextBox1.Text = TextLine
End If
Re: SendKeys and Reading Textbox file line by line
Re: SendKeys and Reading Textbox file line by line
I'll explain the concept more in detail.
In the program I introduce integer numbers in a textbox and the program performs some calculations after pressing the enter key in the textbox.
Well, sometimes I want to save the list of numbers I introduced and for that task I save the number list in a text *.txt file.
The text file contains integer numbers, one number in each line.
Example of the data in the text file:
1
45
3
43
etc...
At the moment, the thing I want to do is to load a text file containing those integer numbers.
The first task to do is to read the text file line by line to get each time 1 integer number.
Once the program has the number, the program "writes" in a TextBox the number and press the enter key (ascii chr 13) and looks for the next number in the text file until the end of that file.
At the moment I use the code shown above:
Code:
Dim NameOfTheFile As String = OpenFileDialog1.FileName
Dim TextLine As String
If System.IO.File.Exists(NameOfTheFile) = True Then
Dim ObjReader As New System.IO.StreamReader(NameOfTheFile)
Do While ObjReader.Peek() <> -1
TEXTBOX_NumeroSalido.Focus()
TextLine = ObjReader.ReadLine() & vbNewLine
LISTBOX_Consejo.Items.Add(TextLine)
TEXTBOX_NumeroSalido.Text = TextLine
SendKeys.Send("{ENTER}")
Loop
The problem is that the program only shows the last number in the text file into the DataGridView control, instead of showing all the values that the textfile contains.
In the ListBox all values are shown, but not in the DataGridView control.
I think something with the command "Send Keys" is the problem. But I don't know.
Re: SendKeys command in a TextBox?
Quote:
The problem is that the program only shows the last number in the text file into the DataGridView control,
I think this is because you are not add the lines to each other in the text box, try this
Code:
Dim NameOfTheFile As String = OpenFileDialog1.FileName
Dim TextLine As String
If System.IO.File.Exists(NameOfTheFile) = True Then
Dim ObjReader As New System.IO.StreamReader(NameOfTheFile)
Do While ObjReader.Peek() <> -1
TEXTBOX_NumeroSalido.Focus()
TextLine = ObjReader.ReadLine()
LISTBOX_Consejo.Items.Add(TextLine)
TEXTBOX_NumeroSalido.Text &= TextLine ' & vbNewLine ' maybe vbNewLine isn't required as SendKeys.Send("{ENTER}") will insert it.
SendKeys.Send("{ENTER}")
Loop
End If
It is better to create a sub with calculations you want to perform and call it after reading each line instead of sending {Enter}
Re: SendKeys command in a TextBox?
Quote:
the DataGridView control
What DGV? There's no mention of a DGV in your code! If there exists a routine that places values in the DGV and that is failing would it not make sense to show us that code rather than the bit that is working (placing items in the Listbox)?
Re: SendKeys command in a TextBox?
Quote:
Originally Posted by
4x2y
I think this is because you are not add the lines to each other in the text box, try this
Code:
Dim NameOfTheFile As String = OpenFileDialog1.FileName
Dim TextLine As String
If System.IO.File.Exists(NameOfTheFile) = True Then
Dim ObjReader As New System.IO.StreamReader(NameOfTheFile)
Do While ObjReader.Peek() <> -1
TEXTBOX_NumeroSalido.Focus()
TextLine = ObjReader.ReadLine()
LISTBOX_Consejo.Items.Add(TextLine)
TEXTBOX_NumeroSalido.Text &= TextLine ' & vbNewLine ' maybe vbNewLine isn't required as SendKeys.Send("{ENTER}") will insert it.
SendKeys.Send("{ENTER}")
Loop
End If
It is better to create a sub with calculations you want to perform and call it after reading each line instead of sending {Enter}
It doesn't works. When I use I've in the textbox a lot of numbers like 1231231412352.... and when I change and I use it works as it worked in the beginning.
Quote:
Originally Posted by
dunfiddlin
What DGV? There's no mention of a DGV in your code! If there exists a routine that places values in the DGV and that is failing would it not make sense to show us that code rather than the bit that is working (placing items in the Listbox)?
The DGV is used in other part in the code. When I introduce the same values by hand it works, even in the DGV. I introduce for example.... 31 and after... enter key. The problem is when I try to make it automatic. It works only for the last value in the text file. The rest of the values are not processed for some reason. The last value is shown in the DGV, but from the first to the one before the last one it doesn't works.
Re: SendKeys command in a TextBox?
Quote:
The rest of the values are not processed for some reason. The last value is shown in the DGV, but from the first to the one before the last one it doesn't works.
And the most likely reason for that is that you are entering each value into the same cell and overwriting the one prior to it. But we'll never know will we as the code is apparently not relevant. :rolleyes:
Re: SendKeys command in a TextBox?
Quote:
Originally Posted by
dunfiddlin
And the most likely reason for that is that you are entering each value into the same cell and overwriting the one prior to it. But we'll never know will we as the code is apparently not relevant. :rolleyes:
Ok, here you've more than 200 lines of code... :bigyello:
Code:
Imports Setting.IniFile
Public Class Form1
Private Sub CargarListaNumerosMasSalidos()
With MatrizNumeros
.Add(NumeroSalido)
End With
Dim Group_Count = From Lst In MatrizNumeros Group Lst By Lst Into Count() Select ax = Lst, GroupCount = Count Order By GroupCount Descending
LABEL_NumerosMasRepetidos.Text = ""
For Each Counts In Group_Count.ToList
Me.LABEL_NumerosMasRepetidos.Text += String.Concat(Counts.ax, " (", ContadorNumeros(Counts.ax), ") ")
Next
End Sub
Private Sub TEXTBOX_NumeroSalido_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TEXTBOX_NumeroSalido.KeyPress
If (e.KeyChar = Microsoft.VisualBasic.Chr(13)) Then
If Len(TEXTBOX_NumeroSalido.Text) <> 0 And IsNumeric(TEXTBOX_NumeroSalido.Text) And Val(TEXTBOX_NumeroSalido.Text) <= 36 And Val(TEXTBOX_NumeroSalido.Text) >= 0 Then
CantidadJugadas = CantidadJugadas + 1
NumeroSalido = (Val(TEXTBOX_NumeroSalido.Text))
LABEL_CantidadJugadas.Text = CantidadJugadas
ContadorNumeros(Val(TEXTBOX_NumeroSalido.Text)) = ContadorNumeros(Val(TEXTBOX_NumeroSalido.Text)) + 1
'Cuadricula.Rows.Add(NumeroSalido)
TEXTBOX_NumeroSalido.Text = ""
CargarListaNumerosMasSalidos()
'Añade el numero que se acaba de introducir
Cuadricula.Rows.Add(NumeroSalido)
MatrizNumerosSalidos(CantidadJugadas) = NumeroSalido
'Añade la paridad de ese numero
Form3.COMBO_Paridad.SelectedIndex = ini.ReadValue("COMBO_Paridad", NumeroSalido)
Cuadricula.Rows(CantidadJugadas - 1).Cells(1).Value = Form3.COMBO_Paridad.SelectedItem
'Colorea el fondo y el fondo en caso - PAR
If Form3.COMBO_Paridad.SelectedIndex = 0 Then
Dim ColorFondoPar As String = ini.ReadValue("Paridad_Par_Fondo", 0)
Cuadricula.Item(1, CantidadJugadas - 1).Style.BackColor = Color.FromArgb(Integer.Parse(ColorFondoPar))
Dim ColorTextoPar As String = ini.ReadValue("Paridad_Par_Texto", 0)
Cuadricula.Item(1, CantidadJugadas - 1).Style.ForeColor = Color.FromArgb(Integer.Parse(ColorTextoPar))
End If
'Colorea el texto y el fondo en caso - IMPAR
If Form3.COMBO_Paridad.SelectedIndex = 1 Then
Dim ColorFondoImpar As String = ini.ReadValue("Paridad_Impar_Fondo", 0)
Cuadricula.Item(1, CantidadJugadas - 1).Style.BackColor = Color.FromArgb(Integer.Parse(ColorFondoImpar))
Dim ColorTextoImpar As String = ini.ReadValue("Paridad_Impar_Texto", 0)
Cuadricula.Item(1, CantidadJugadas - 1).Style.ForeColor = Color.FromArgb(Integer.Parse(ColorTextoImpar))
End If
' Caso 3 - NULO
If Form3.COMBO_Paridad.SelectedIndex = 2 Then
Dim ColorFondoNulo As String = ini.ReadValue("Paridad_Nulo_Fondo", 0)
Cuadricula.Item(1, CantidadJugadas - 1).Style.BackColor = Color.FromArgb(Integer.Parse(ColorFondoNulo))
Dim ColorTextoNulo As String = ini.ReadValue("Paridad_Nulo_Texto", 0)
Cuadricula.Item(1, CantidadJugadas - 1).Style.ForeColor = Color.FromArgb(Integer.Parse(ColorTextoNulo))
End If
'Añade el color
Form3.COMBO_RojoNegro.SelectedIndex = ini.ReadValue("COMBO_RojoNegro", NumeroSalido)
Cuadricula.Rows(CantidadJugadas - 1).Cells(2).Value = Form3.COMBO_RojoNegro.SelectedItem
If Form3.COMBO_RojoNegro.SelectedIndex = 0 Then 'Rojo
Dim ColorFondoRojo As String = ini.ReadValue("Rojo_Fondo", 0)
Cuadricula.Item(2, CantidadJugadas - 1).Style.BackColor = Color.FromArgb(Integer.Parse(ColorFondoRojo))
Dim ColorTextoRojo As String = ini.ReadValue("Rojo_Texto", 0)
Cuadricula.Item(2, CantidadJugadas - 1).Style.ForeColor = Color.FromArgb(Integer.Parse(ColorTextoRojo))
End If
If Form3.COMBO_RojoNegro.SelectedIndex = 1 Then 'Negro
Dim ColorFondoNegro As String = ini.ReadValue("Negro_Fondo", 0)
Cuadricula.Item(2, CantidadJugadas - 1).Style.BackColor = Color.FromArgb(Integer.Parse(ColorFondoNegro))
Dim ColorTextoNegro As String = ini.ReadValue("Negro_Texto", 0)
Cuadricula.Item(2, CantidadJugadas - 1).Style.ForeColor = Color.FromArgb(Integer.Parse(ColorTextoNegro))
End If
If Form3.COMBO_RojoNegro.SelectedIndex = 2 Then 'Verde
Dim ColorFondoVerde As String = ini.ReadValue("Verde_Fondo", 0)
Cuadricula.Item(2, CantidadJugadas - 1).Style.BackColor = Color.FromArgb(Integer.Parse(ColorFondoVerde))
Dim ColorTextoVerde As String = ini.ReadValue("Verde_Texto", 0)
Cuadricula.Item(2, CantidadJugadas - 1).Style.ForeColor = Color.FromArgb(Integer.Parse(ColorTextoVerde))
End If
'Añade la fila
Form3.COMBO_Fila.SelectedIndex = ini.ReadValue("COMBO_Fila", NumeroSalido)
Cuadricula.Rows(CantidadJugadas - 1).Cells(3).Value = Form3.COMBO_Fila.SelectedItem
'Añade la menor mayor
Form3.COMBO_MenorMayor.SelectedIndex = ini.ReadValue("COMBO_MenorMayor", NumeroSalido)
Cuadricula.Rows(CantidadJugadas - 1).Cells(4).Value = Form3.COMBO_MenorMayor.SelectedItem
'Añade la rojo negro
Form3.COMBO_RojoNegro.SelectedIndex = ini.ReadValue("COMBO_RojoNegro", NumeroSalido)
Cuadricula.Rows(CantidadJugadas - 1).Cells(5).Value = Form3.COMBO_RojoNegro.SelectedItem
Else
MsgBox("Introduzca un tipo de dato válido", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Dato incorrecto")
TEXTBOX_NumeroSalido.Text = ""
End If
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Form3.Show()
End Sub
Private Sub Cuadricula_Click(sender As Object, e As System.EventArgs) Handles Cuadricula.Click
End Sub
Private Sub Cuadricula_DoubleClick(sender As Object, e As System.EventArgs) Handles Cuadricula.DoubleClick
MsgBox(Column6.Width)
End Sub
Private Sub BOTON_Calculos_Click(sender As System.Object, e As System.EventArgs) Handles BOTON_Calculos.Click
Form4.Show()
End Sub
Private Sub LABEL_RepeticionNumeros_Click(sender As Object, e As System.EventArgs) Handles LABEL_RepeticionNumeros.DoubleClick
'MSGBOX PARA VISUALIZAR LOS NUMEROS MAS REPETIDOS, EN ORDEN, Y VISUALIZAR TAMBIEN LA CANTIDAD DE VECES QUE SE HA REPETIDO CADA NUMERO
End Sub
Private Sub SalirToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SalirToolStripMenuItem.Click
End
End Sub
Private Sub BOTON_Consejo_Click(sender As System.Object, e As System.EventArgs) Handles BOTON_Consejo.Click
'Construye una estadistica de probabilidades y las coloca en el listbox de Consejo
Dim Ciclo As Integer
Dim Par As Integer, Impar As Integer, Nulo As Integer
Dim Rojo As Integer, Negro As Integer, Verde As Integer
Dim VecinosCero As Integer, Huerfanos As Integer, Tercio As Integer
For Ciclo = 1 To CantidadJugadas - 1 'Cicla desde la primera jugada hasta la jugada anterior al ultimo numero que ha salido
If MatrizNumerosSalidos(Ciclo) = NumeroSalido Then
'comprobacion de paridad de numeros. a tener en cuenta el numero 0
If MatrizNumerosSalidos(Ciclo + 1) = 0 Then
Nulo = Nulo + 1
Else
If MatrizNumerosSalidos(Ciclo + 1) Mod 2 = 0 Then Par = Par + 1
If MatrizNumerosSalidos(Ciclo + 1) Mod 2 <> 0 Then Impar = Impar + 1
End If
'comprobacion del color
If (ini.ReadValue("COMBO_RojoNegro", (MatrizNumerosSalidos(Ciclo + 1))) = 0) Then Rojo = Rojo + 1
If (ini.ReadValue("COMBO_RojoNegro", (MatrizNumerosSalidos(Ciclo + 1))) = 1) Then Negro = Negro + 1
If (ini.ReadValue("COMBO_RojoNegro", (MatrizNumerosSalidos(Ciclo + 1))) = 2) Then Verde = Verde + 1
'comprobacion de la zona; VecinosCero = 0 ; Huerfanos = 1 ; Tercio = 2
If (ini.ReadValue("COMBO_Zona", (MatrizNumerosSalidos(Ciclo + 1))) = 0) Then VecinosCero = VecinosCero + 1
If (ini.ReadValue("COMBO_Zona", (MatrizNumerosSalidos(Ciclo + 1))) = 1) Then Huerfanos = Huerfanos + 1
If (ini.ReadValue("COMBO_Zona", (MatrizNumerosSalidos(Ciclo + 1))) = 2) Then Tercio = Tercio + 1
End If
Next
MsgBox("Después de: " & NumeroSalido & vbCrLf & "Par: " & Par & vbCrLf & "Impar: " & Impar & vbCrLf & "Rojo: " & Rojo & vbCrLf & "Negro: " & Negro)
End Sub
Private Sub AbrirToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AbrirToolStripMenuItem.Click
OpenFileDialog1.Title = "Seleccione archivo de sesion para abrir..."
OpenFileDialog1.InitialDirectory = "C:\Datos\"
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim Nombre_Archivo As String = OpenFileDialog1.FileName
Dim TextLine As String
If System.IO.File.Exists(Nombre_Archivo) = True Then
Dim ObjReader As New System.IO.StreamReader(Nombre_Archivo)
Do While ObjReader.Peek() <> -1
TextLine = ObjReader.ReadLine()
LISTBOX_Consejo.Items.Add(TextLine)
TEXTBOX_NumeroSalido.Text = TextLine
SendKeys.Send("{ENTER}")
Loop
End If
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
TEXTBOX_NumeroSalido.Text = "1"
TEXTBOX_NumeroSalido.Focus()
SendKeys.Send("{ENTER}")
End Sub
Private Sub TEMPORIZADOR1_Tick(sender As System.Object, e As System.EventArgs) Handles TEMPORIZADOR1.Tick
If TEMPORIZADOR1.Enabled = True Then
LISTBOX_Consejo.SelectedIndex = 0
Dim NumeroACopiar As Integer
NumeroACopiar = LISTBOX_Consejo.GetItemText(0)
TEXTBOX_NumeroSalido.Text = NumeroACopiar
TEXTBOX_NumeroSalido.Focus()
SendKeys.Send("{ENTER}")
TEXTBOX_NumeroSalido.Text = ""
LISTBOX_Consejo.Items.Remove(0)
LISTBOX_Consejo.Refresh()
End If
End Sub
End Class
Re: SendKeys command in a TextBox?
do you have Option Strict on?
where do you declare CantidadJugadas? it needs to be declared at form level to retain it's value
Re: SendKeys command in a TextBox?
Quote:
Originally Posted by
.paul.
do you have Option Strict on?
where do you declare CantidadJugadas? it needs to be declared at form level to retain it's value
Option strict is off.
I declare it in a module
Code:
Public CantidadJugadas As Integer
I've tried to do the same task, but when I press a button a number is entered into the system. The only thing I do here is to create integer random numbers instead of reading them from the text file.
Code:
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
TEXTBOX_NumeroSalido.Text = Int(Rnd() * 37)
TEXTBOX_NumeroSalido.Focus()
SendKeys.Send("{ENTER}")
End Sub
This piece of code works. Now I'm going to see why in the automated system it doesn't works.
Re: SendKeys command in a TextBox?
It is always good practice to separate code from event handler subs, so it is easy to call it from anywhere, e.g.
Code:
Private Sub TEXTBOX_NumeroSalido_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TEXTBOX_NumeroSalido.KeyPress
If (e.KeyChar = Microsoft.VisualBasic.Chr(13)) Then
DoSomething(TEXTBOX_NumeroSalido.Text)
Else
MsgBox("Introduzca un tipo de dato válido", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Dato incorrecto")
TEXTBOX_NumeroSalido.Text = ""
End If
End Sub
Private Sub OpenFileDialog1_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim Nombre_Archivo As String = OpenFileDialog1.FileName
Dim TextLine As String
If System.IO.File.Exists(Nombre_Archivo) = True Then
Dim ObjReader As New System.IO.StreamReader(Nombre_Archivo)
Do While ObjReader.Peek() <> -1
TextLine = ObjReader.ReadLine()
LISTBOX_Consejo.Items.Add(TextLine)
DoSomething(TextLine)
Loop
End If
End Sub
Private Sub DoSomething(ByVal strText As String)
' no need for Len(strText) <> 0 as IsNumeric(strText) is enough.
If IsNumeric(strText) AndAlso Val(strText) <= 36 AndAlso Val(strText) >= 0 Then
' Add code under TEXTBOX_NumeroSalido_KeyPress here
' and replace the direct reference to TEXTBOX_NumeroSalido.Text with the param strText.
End If
End Sub