VB-If, Else,Then...我想

VB-If, Else,Then...I think

嗨,我是 vb 的新手,我今天早上才开始……我什么都不知道 我正在尝试制作一个搜索引擎,但遇到了一些麻烦,这就是我目前所拥有的

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If TextBox1.Text = "" Then
        MsgBox("Please make an input")
    End If

     TextBox1.Text = "message" Then
    MsgBox("Yaw searcher is now searching for your input")
    Timer1.Enabled = True
    MsgBox("File Found,redirecting to search results")
    System.Diagnostics.Process.Start("https://www.google.com/search?q=" + _
        TextBox1.Text + _
    "%3F&aqs=chrome..69i57j0j69i65l3j69i60.3515j0j7&sourceid=chrome&es_sm=93&ie=UTF-8")

我正在尝试做到这一点,如果输入任何消息,程序就会出现并说

    MsgBox("Yaw searcher is now searching for your input")
    Timer1.Enabled = True
    MsgBox("File Found,redirecting to search results")
    System.Diagnostics.Process.Start("https://www.google.com/search?q=" + _
        TextBox1.Text + _
    "%3F&aqs=chrome..69i57j0j69i65l3j69i60.3515j0j7&sourceid=chrome&es_sm=93&ie=UTF-8")

如果不是它说 pelase make an imput...我得到了那部分但不能得到最后一部分谢谢你的帮助

你应该使用 If, Else, then End If

This Link will help you understand

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If TextBox1.Text = "" Then
        MsgBox("Please make an input")
    ElseIf TextBox1.Text = "message" Then
           MsgBox("Yaw searcher is now searching for your input")
           Timer1.Enabled = True
           MsgBox("File Found,redirecting to search results")
           System.Diagnostics.Process.Start("https://www.google.com/search?q=" + TextBox1.Text + "%3F&aqs=chrome..69i57j0j69i65l3j69i60.3515j0j7&sourceid=chrome&es_sm=93&ie=UTF-8")

    Else
        MsgBox("input is not message or blank")
    End If

您不需要额外的 if 检查 @prospector as String.IsNullOrEmpty 将涵盖这两种情况(null,或者在 VB.NET 的情况下 Nothing, 和 String.Empty):

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'check if the textbox content is empty or null
    If String.IsNullOrEmpty(TextBox1.Text) Then
        MsgBox("You need to enter something to search for!")
    'if not empty or null, search google for that keyword instead
    Else
        MsgBox("Yaw searcher is now searching for your input")
        MsgBox("File Found,redirecting to search results")
        System.Diagnostics.Process.Start("https://www.google.com/search?q=" + TextBox1.Text)
    End If
End Sub

请记住,MsgBox("File Found,redirecting to search results") 并非真正需要或提供信息,因为实际的 google 搜索在您调用 Process.Start.

时在浏览器中开始

最简单的形式是 VB.NET 中的 If-Then-Else 语法:

If SomeCondition Then
    DoSomething()
ElseIf SomeOtherCondition Then
    DoSomethingElse()
Else
    DoSomethingIrrelevant()
End If 

来自 MSDN "If...Then...Else Statement (Visual Basic)" 的示例(稍作修改):

Dim count As Integer = 0
Dim message As String 

If count = 0 Then
    'this will only run if count = 0 
    message = "There are no items." 
ElseIf count = 1 Then
    'this will only run if count = 1
    message = "There is 1 item." 
ElseIf count = 2 Then
    'this will only run if count = 2
    message = "There are 2 items."
Else
    'this will only run if count has any value other than 0, 1 or 2
    message = "There are more than 2 items or less than 0 items." 
End If