Text nach bestimmten Wort in Textbox ausgeben (Mehrzeilig)

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 20 Antworten in diesem Thema. Der letzte Beitrag () ist von nikexo.

    Text nach bestimmten Wort in Textbox ausgeben (Mehrzeilig)

    Hallo Community!

    Ich weiß, der Titel klingt sehr verwirrend, wusste aber nicht wie ich es am besten beschreiben soll.

    Also folgendes Problem.
    Ich gebe in der TextBox1 eine IP an (z.B. von google.com (173.194.65.113)).
    Außerdem verwende ich hierfür diese API: api.predator.wtf/geoip/?arguments=173.194.65.113

    So, als Rückgabewert der Seite bekomm ich dann das hier:
    IP: 173.194.65.113
    Hostname: ee-in-f113.1e100.net
    AS Number: AS15169 Google Inc.
    Country: United States
    Country Code: US
    Region: California / CA
    City: Mountain View
    Postal Code: 94043
    ISP: Google
    Organization: Google
    Timezone: America/Los_Angeles
    Maps: maps.google.com/?ll=37.419200897217,-122.05740356445


    Nun will ich ein paar Stellen davon in verschiedene Textboxen ausgeben lassen.
    z.B.:
    in TextBox2 soll (Country:) United States ausgegeben werden
    in TextBox3 soll (Country Code:) US ausgegeben werden
    in TextBox4 soll (Region:) California / CA ausgegeben werden

    Ich glaube ihr versteht was ich damit meine.

    Ich hoffe mir kann jemand dabei helfen. :)

    Edit: Suche sowas:

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „nikexo“ ()

    Du könntest eine andere API (Webseite) nutzen die dir das ganze als XML anbietet. Ich nutze dafür z.B. 'http://ip-api.com/'.
    Hab dir dazu mal mein Projekt angehängt.

    Spoiler anzeigen

    Die Klasse

    VB.NET-Quellcode

    1. Public NotInheritable Class GetUserLocationByIp
    2. Private Sub New()
    3. End Sub
    4. Public Structure IpInformation
    5. Public Property Status() As String
    6. Get
    7. Return m_Status
    8. End Get
    9. Set
    10. m_Status = Value
    11. End Set
    12. End Property
    13. Private m_Status As String
    14. Public Property Country() As String
    15. Get
    16. Return m_Country
    17. End Get
    18. Set
    19. m_Country = Value
    20. End Set
    21. End Property
    22. Private m_Country As String
    23. Public Property CountryCode() As String
    24. Get
    25. Return m_CountryCode
    26. End Get
    27. Set
    28. m_CountryCode = Value
    29. End Set
    30. End Property
    31. Private m_CountryCode As String
    32. Public Property Region() As String
    33. Get
    34. Return m_Region
    35. End Get
    36. Set
    37. m_Region = Value
    38. End Set
    39. End Property
    40. Private m_Region As String
    41. Public Property RegionName() As String
    42. Get
    43. Return m_RegionName
    44. End Get
    45. Set
    46. m_RegionName = Value
    47. End Set
    48. End Property
    49. Private m_RegionName As String
    50. Public Property City() As String
    51. Get
    52. Return m_City
    53. End Get
    54. Set
    55. m_City = Value
    56. End Set
    57. End Property
    58. Private m_City As String
    59. Public Property Zip() As String
    60. Get
    61. Return m_Zip
    62. End Get
    63. Set
    64. m_Zip = Value
    65. End Set
    66. End Property
    67. Private m_Zip As String
    68. Public Property Lat() As String
    69. Get
    70. Return m_Lat
    71. End Get
    72. Set
    73. m_Lat = Value
    74. End Set
    75. End Property
    76. Private m_Lat As String
    77. Public Property Lon() As String
    78. Get
    79. Return m_Lon
    80. End Get
    81. Set
    82. m_Lon = Value
    83. End Set
    84. End Property
    85. Private m_Lon As String
    86. Public Property TimeZone() As String
    87. Get
    88. Return m_TimeZone
    89. End Get
    90. Set
    91. m_TimeZone = Value
    92. End Set
    93. End Property
    94. Private m_TimeZone As String
    95. Public Property ISP() As String
    96. Get
    97. Return m_ISP
    98. End Get
    99. Set
    100. m_ISP = Value
    101. End Set
    102. End Property
    103. Private m_ISP As String
    104. Public Property ORG() As String
    105. Get
    106. Return m_ORG
    107. End Get
    108. Set
    109. m_ORG = Value
    110. End Set
    111. End Property
    112. Private m_ORG As String
    113. Public Property [AS]() As String
    114. Get
    115. Return m_AS
    116. End Get
    117. Set
    118. m_AS = Value
    119. End Set
    120. End Property
    121. Private m_AS As String
    122. Public Property Query() As String
    123. Get
    124. Return m_Query
    125. End Get
    126. Set
    127. m_Query = Value
    128. End Set
    129. End Property
    130. Private m_Query As String
    131. End Structure
    132. Public Shared Function GetLocation(targetIP As IPAddress) As IpInformation
    133. Dim info As New IpInformation()
    134. Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://ip-api.com/xml/" + targetIP.ToString()), HttpWebRequest)
    135. Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    136. Dim doc As New XmlDocument()
    137. doc.Load(response.GetResponseStream())
    138. info.Status = doc.GetElementsByTagName("status")(0).InnerText
    139. info.Country = doc.GetElementsByTagName("country")(0).InnerText
    140. info.CountryCode = doc.GetElementsByTagName("countryCode")(0).InnerText
    141. info.Region = doc.GetElementsByTagName("region")(0).InnerText
    142. info.RegionName = doc.GetElementsByTagName("regionName")(0).InnerText
    143. info.City = doc.GetElementsByTagName("city")(0).InnerText
    144. info.Zip = doc.GetElementsByTagName("zip")(0).InnerText
    145. info.Lat = doc.GetElementsByTagName("lat")(0).InnerText
    146. info.Lon = doc.GetElementsByTagName("lon")(0).InnerText
    147. info.TimeZone = doc.GetElementsByTagName("timezone")(0).InnerText
    148. info.ISP = doc.GetElementsByTagName("isp")(0).InnerText
    149. info.ORG = doc.GetElementsByTagName("org")(0).InnerText
    150. info.[AS] = doc.GetElementsByTagName("as")(0).InnerText
    151. info.Query = doc.GetElementsByTagName("query")(0).InnerText
    152. End Using
    153. Return info
    154. End Function
    155. End Class


    Verwendung

    VB.NET-Quellcode

    1. Console.Write("Bitte geben Sie eine IP -Addresse ein: ")
    2. Dim ipInfo As New GetUserLocationByIp.IpInformation()
    3. Dim targetIP As IPAddress
    4. If IPAddress.TryParse(Console.ReadLine(), targetIP) Then
    5. ipInfo = GetUserLocationByIp.GetLocation(targetIP)
    6. Else
    7. Return
    8. End If
    9. Console.WriteLine(String.Format(vbLf & vbLf & " Land:" & vbTab & vbTab & "{0} [{1}]", ipInfo.Country, ipInfo.CountryCode))
    10. Console.WriteLine(String.Format(" Region:" & vbTab & "{0} [{1}]", ipInfo.RegionName, ipInfo.Region))
    11. Console.WriteLine(" Stadt:" & vbTab & vbTab + ipInfo.City)
    12. Console.WriteLine(" PLZ:" & vbTab & vbTab + ipInfo.Zip)
    13. Console.WriteLine(" Breitengrad:" & vbTab + ipInfo.Lat)
    14. Console.WriteLine(" Längengrad:" & vbTab + ipInfo.Lon)
    15. Console.WriteLine(" Zeitzone:" & vbTab + ipInfo.TimeZone)
    16. Console.WriteLine(" Anbieter:" & vbTab + ipInfo.ISP)
    17. Console.ReadKey()


    Klar kannst du das so verwenden.
    Ist ja schließlich ne Klasse, die kannst du einfach in deinem Code instanzieren und dann auf die Properties zugreifen.
    Falls du deinen Code weiter verwenden willst kannst du aber auch einfach einen Lexer schreiben, der die Informationen richtig einliest und separiert ausgibt.
    Ich verstehe nicht wieso ihr nicht sein Problem löst antatt ihm andere Möglichkeiten anbietet

    Split nach Char(10) = LF


    VB.NET-Quellcode

    1. Dim x As String() = RichTextBox1.Text.Split(Chr(10))
    2. Dim IP As String = x(0)
    3. Dim Hostname As String = x(1)
    4. Dim AS_Number As String = x(2)
    5. Dim Country As String = x(3)
    6. Dim Country_Code As String = x(4)
    7. Dim Region As String = x(5)
    8. Dim City As String = x(6)
    9. Dim Postal_Code As String = x(7)
    10. Dim ISP As String = x(8)
    11. Dim Organization As String = x(9)
    12. Dim Timezone As String = x(10)
    13. Dim Maps As String = x(11)
    EMPIRE BUSINESS
    Wie bereits erwähnt du brauchst dir nur die Klasse in deinem Projekt anlegen. Anschließend kannst du in deiner Form folgendes schreiben

    VB.NET-Quellcode

    1. Dim ipInfo As New GetUserLocationByIp.IpInformation()
    2. If IPAddress.TryParse(Deine IP, targetIP) Then
    3. ipInfo = GetUserLocationByIp.GetLocation(targetIP)
    4. TextBox2.Text = ipInfo.Country
    5. TextBox3.Text = ipInfo.CountryCode
    6. End If


    'Deine IP' musst du nur mit der IP ersetzen die du abfragen willst.

    .prox schrieb:

    VB.NET-Quellcode

    1. Dim x As String() = RichTextBox1.Text.Split(Chr(10))
    Eine RichTextBox hat eine Property .Lines.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Vielen Dank an alle Antworten! :)
    Ich probiere jetzt mal etwas herum.

    Fakiz schrieb:

    Wie bereits erwähnt du brauchst dir nur die Klasse in deinem Projekt anlegen. Anschließend kannst du in deiner Form folgendes schreiben

    VB.NET-Quellcode

    1. Dim ipInfo As New GetUserLocationByIp.IpInformation()
    2. If IPAddress.TryParse(TextBox1.Text, targetIP) Then
    3. ipInfo = GetUserLocationByIp.GetLocation(targetIP)
    4. TextBox2.Text = ipInfo.Country
    5. TextBox3.Text = ipInfo.CountryCode
    6. End If


    'Deine IP' musst du nur mit der IP ersetzen die du abfragen willst.


    Geht das so?


    Den oberen Code von Fakiz verstehe ich ja noch, aber der untere? Das ist doch einen Konsolenanwendung oder nicht?

    VB.NET-Quellcode

    1. Console.Write("Bitte geben Sie eine IP -Addresse ein: ")
    2. Dim ipInfo As New GetUserLocationByIp.IpInformation()
    3. Dim targetIP As IPAddress
    4. If IPAddress.TryParse(Console.ReadLine(), targetIP) Then
    5. ipInfo = GetUserLocationByIp.GetLocation(targetIP)
    6. Else
    7. Return
    8. End If
    9. Console.WriteLine(String.Format(vbLf & vbLf & " Land:" & vbTab & vbTab & "{0} [{1}]", ipInfo.Country, ipInfo.CountryCode))
    10. Console.WriteLine(String.Format(" Region:" & vbTab & "{0} [{1}]", ipInfo.RegionName, ipInfo.Region))
    11. Console.WriteLine(" Stadt:" & vbTab & vbTab + ipInfo.City)
    12. Console.WriteLine(" PLZ:" & vbTab & vbTab + ipInfo.Zip)
    13. Console.WriteLine(" Breitengrad:" & vbTab + ipInfo.Lat)
    14. Console.WriteLine(" Längengrad:" & vbTab + ipInfo.Lon)
    15. Console.WriteLine(" Zeitzone:" & vbTab + ipInfo.TimeZone)
    16. Console.WriteLine(" Anbieter:" & vbTab + ipInfo.ISP)
    17. Console.ReadKey()

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „nikexo“ ()

    nikexo schrieb:

    VB.NET-Quellcode

    1. If IPAddress.TryParse(Console.ReadLine(), targetIP) Then
    machst Du einfach:

    VB.NET-Quellcode

    1. If Not IPAddress.TryParse(Console.ReadLine(), targetIP) Then
    2. Return
    3. End If
    4. ipInfo = GetUserLocationByIp.GetLocation(targetIP)
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ich verstehe das mit Console.ReadLine() nicht, das ist doch eine Konsolenanwendung?
    Ich verwende doch Windows Forms also TextBoxen?

    Edit: Ja klar ist das eine Konsoleanwendung. Funktioniert ja auch alles gut, will es aber in Windows Forms mit TextBoxen...


    Und ich glaube nicht das ich es selbst schaffe, es von einer Konsolenanwendung umzuschreiben...


    Edit: Ich will sowas:

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „nikexo“ ()

    Wenn in Textbox1 die IP drin steht dann funktioniert das so. Ja der Untere Teil ist eine Konsole, ist aber nicht von Bedeutung. Das liegt nur daran das bei meinem Projekt eine Konsole zum Einsatz kam und keine Forms Anwendung.

    *Edit*
    Du musst hier nichts umschreiben, du erstellst dir in der Forms -Anwendung die Klasse und in z.B. Button KlickEvent

    VB.NET-Quellcode

    1. Private Sub button1_Click(sender As Object, e As EventArgs)
    2. Dim ipInfo As New GetUserLocationByIp.IpInformation()
    3. Dim targetIP As IPAddress
    4. If Not IPAddress.TryParse(Textbox1.Text, targetIP) Then
    5. Return
    6. End If
    7. ipInfo = GetUserLocationByIp.GetLocation(targetIP)
    8. textBox2.Text = ipInfo.City
    9. textBox3.Text = ipInfo.Country
    10. End Sub

    Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von „Fakiz“ ()

    ja, eine NullReferenceException.
    Finde halt heraus, welcher Teil-Ausdruck Null ist und nicht sein darf - die Fehlerzeile steht dir ja gelb vor Augen.
    Bisserl Hilfe zum Herausfinden, bzw. wovon ich ühaupt rede:
    Exceptions, und was sie uns sagen wollen
    VisualStudio richtig nutzen (Google ist nicht deine Mami)

    Also die Hilfe ist bischen allgemeiner, aber wenn du das durcharbeitest, und damit den Fehler selbst identifizierst, hast du glaub mehr gelernt als wenn man einfach vorsagte.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ErfinderDesRades“ ()

    nein, der Fehler ist in der Fehlerzeile, und die ist gelb.
    Nur in dieser Zeile suchen - nirgends sonst.

    weißt du nicht, was ein "Teil-Ausdruck" ist, oder was ist das Problem mit dem Herausfinden des TeilAusdrucks, der unzulässigerweise Nothing ergibt?

    Und im Exception-Tut gehe ich glaub auch etwas ausführlicher grad auf die NullRefException ein.
    Naja - ist hier nicht der Punkt, dein Prob scheints zu sein, zusammengesetzte Ausdrücke auseinanderzudröseln. Auch dazu gibts dort einen Abschnitt: "BruteForce-Debugging".

    Also nachlesen, nachfragen, verstehen, lernen - nicht planlos rumprobieren.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ErfinderDesRades“ ()

    In dem Fall musst du eben die Exception abfangen, dies machst du mit einer Zusätzlichen If -Abfrage

    VB.NET-Quellcode

    1. If doc.GetElementsByTagName("country")(0).InnerText IsNot Nothing Then
    2. info.Country = doc.GetElementsByTagName("country")(0).InnerText
    3. else
    4. info.Country = "Konnte nicht ermittelt werden"
    5. End If


    VB.NET-Quellcode

    1. Dim targetIP As IPAddress = Nothing