Hola, buenas tardes. Me gustaría saber cómo puedo extraer de un formulario un dato de las cajas de texto que hay para luego ponerlo en un textbox de mi aplicación.
En el código fuente de la página web aparece algo como esto:
<input name="codigo2" type="text" size="16" value="10409" maxlength="6" onKeyPress="return soloNumero2(event)" disabled>
En el campo código del formulario sólo se ve el número 10409 y además no se puede editar. Bueno, es ese número el que quisiera trasladarlo hacia un textbox de la aplicación que intento hacer.
Sería algo como:
Me.Textbox1.Text = Codigo_Obtenido_desde_Pagina_WEB
Espero que se haya entendido lo que pretendo y me pueda orientar.
Saludos.
Puedes hacerlo con una simple expresión regular:
Imports System.Text.RegularExpressions
Public Class Form1
Private ReadOnly htmlSource As String =
<a><![CDATA[
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body>
<div class="test">
<input name="codigo2" type="text" size="16" value="10409" maxlength="6" onKeyPress="return soloNumero2(event)" disabled>
<input name="codigo3" type="text" size="16" value="10410" maxlength="6" onKeyPress="return soloNumero3(event)" disabled>
</div>
</body>
</html>
]]></a>.Value
Private Sub Test() Handles MyBase.Shown
Dim regEx As New Regex(<a><![CDATA[value=\"(\d+)\"]]></a>.Value,
RegexOptions.Compiled Or RegexOptions.Multiline Or RegexOptions.IgnoreCase)
If regEx.IsMatch(htmlSource) Then
For Each match As Match In regEx.Matches(htmlSource)
For Each capture As Capture In match.Groups(1).Captures
MessageBox.Show(capture.Value)
Next capture
Next match
Else
MessageBox.Show("No coincide la búsqueda")
End If
End Sub
End Class
...Pero las expresiones regulares no se diseñaron con la intención de parsear un html así que voy a mostrarte una solución más óptima, utilizando la librería
HtmlAgilityPack.
Public Class Form1
Private ReadOnly htmlSource As String =
<a><![CDATA[
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body>
<div class="test">
<input name="codigo2" type="text" size="16" value="10409" maxlength="6" onKeyPress="return soloNumero2(event)" disabled>
<input name="codigo3" type="text" size="16" value="10410" maxlength="6" onKeyPress="return soloNumero3(event)" disabled>
</div>
</body>
</html>
]]></a>.Value
Private htmlDoc As New HtmlAgilityPack.HtmlDocument
Private htmlNodes As HtmlAgilityPack.HtmlNodeCollection
Private Sub Test() Handles MyBase.Shown
' Load the html document.
htmlDoc.LoadHtml(htmlSource)
' Select the nodes.
htmlNodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='test']/input")
' Loop trough the nodes.
For Each node As HtmlAgilityPack.HtmlNode In htmlnodes
Dim Value As Integer
' Get the "value" attribute.
Integer.TryParse(node.GetAttributeValue("value", -1I), Value)
MessageBox.Show(CStr(Value))
Next node
End Sub
End Class
Saludos