2012-06-24 11 views
6

Otwieram witrynę w formancie WebBrowser przy użyciu VB.NET 2008. Na czwartej stronie witryny chcę ustawić kontrolę, uruchamiając klawisz tabulatora programowo. Korzystam z następującego kodu:Ustaw fokus na pole tekstowe HTML lub przycisk w formancie WebBroswer

If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then 
    System.Windows.Forms.SendKeys.Send("{TAB}") 
End If 

Jednak mój kod nie może wywołać klawisza tabulatora. Czy ktoś wie jak to zrobić?

+0

Co oznacza "mój kod jest niezdolny"? Czy otrzymujesz komunikat o błędzie?Co dokładnie się dzieje? Co robi, gdy używasz debuggera z punktem przerwania w linii 'If adtxt.Text'? –

+0

Gdzie umieszczasz ten kod? – Ryan

+0

Nie używaj klawisza TAB, znajdę ci bardziej niezawodną metodę ustawiania fokusu na element HTLM w kontrolce WebBrowser –

Odpowiedz

0

Metoda 1

Private Sub Form_Load() 
    WebBrowser1.Navigate "http://www.google.com/" 
    Do 
    Thread.Sleep(100) 
    Loop While webBrowser1.IsBusy = True 
End Sub 

Private Sub Command1_Click() 
    WebBrowser1.Document.All("q").focus 'Set focus to the search text field 
End Sub 

Private Sub Command2_Click() 
    WebBrowser1.Document.All("btnI").focus 'Set focus to the google "I Am feeling lucky button" 
End Sub 

Metoda 2

I konwertowane go do VB.Net z tego MSDN thread: Focus issues with System.Windows.Controls.WebBrowser

trzeba będzie zmienić ActiveElement w webBrowser.Document.ActiveElement.Focus() do pola tekstowego lub przycisku.

Public Partial Class Form1 
    Inherits Form 
    Public Sub New() 
    InitializeComponent() 
    Dim host As New WindowsFormsHost() 
    im webBrowser As New WebBrowser() 
    host.Child = webBrowser 
    elementHost1.Child = host 

    webBrowser.Navigate(New Uri("http://www.google.com")) 
    Me.Activated += Function() Do 
     Console.WriteLine(Me.ActiveControl) 
     If webBrowser.Document <> Nothing Then 
     If Me.ActiveControl = elementHost1 AndAlso webBrowser.Document.ActiveElement <> Nothing Then 
      webBrowser.Document.ActiveElement.Focus() 
     End If 
     End If 
    End Function 
    End Sub 
End Class 

Metoda 3

Innym sposobem może być to zrobić w HTML, np:

OnLoad="document.myform2.mybutton.focus();"> 
0

powiedzmy, że html youre stronie:

<button id="btn">Ok</button><input id="txt"> 

Można ustawić ostrość w ten sposób:

If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then 
    webbrowser1.document.getelementbyid("btn").focus() 
    webbrowser1.document.getelementbyid("txt").focus() 
End If 
+0

ale mój program nie może uzyskać id – user1473832

0

Innym sposobem:

użyć GetElementsByTagName(TagName)

powiedzmy, że HTML jest:

<button>no</button> 
<button>no</button> 
<button onclick='alert(1);'>--focus me!--</button> 
<button>no</button> 



Dim Elems As HtmlElementCollection 
     Dim WebOC As WebBrowser = WebBrowser1 
     Elems = WebOC.Document.GetElementsByTagName("button") 
     For Each elem As HtmlElement In Elems 
      If elem.InnerHtml = "--focus me!--" Then 
       elem.Focus() 
       elem.InvokeMember("click") 

      End If 

     Next 


jeszcze jedno:

Dim num As Integer = 1 
     Dim elms As HtmlElementCollection 
     Dim wb As WebBrowser = WebBrowser1 
     elms = wb.Document.GetElementsByTagName("button") 
     For Each elem As HtmlElement In elms 
      If elem.Id = "" Then 
       elem.Id = "button" & num.ToString 
       num = num + 1 
      End If 
     Next 

     WebBrowser1.Document.GetElementById("button3").Focus() 
+0

Te oba również nie działają – user1473832

+0

daj mi html strony i staram się spersonalizować mój kod. – RedDm

+0

Myślę, że jego rozwiązanie polega na programowym uruchomieniu klawisza TAB. Ale nie znam kodu wyzwalającego klawisz TAB. Czy możesz mi powiedzieć, jak wywołać naciśnięcia klawiszy TAb Automatycznie według kodu programu ?? – user1473832

0

Aby skupić wybierz element, za pomocą funkcji regulacji ostrości w VB.NET. Na przykład:

Me.WebBrowser1.Document.All.Item("password").Focus() 

Spowoduje to ustawienie fokusu na elemencie o nazwie hasło!

Użyj Me.WebBrowser1.Document.All.Item("YOURelement"), aby znaleźć właściwy element, a następnie dodaj .Focus(), aby ustawić ostrość na tym, który chcesz! : D

-1

Wykonaj Me.WebBrowser1.Document.All.Item (TextBox1.Text) .Focus()

dokonać pole tekstowe, a następnie, jeśli chcesz go spambot łatwy Wykrywa każdym Twój typ pisać i wysyłać

Powiązane problemy