mirror of
https://github.com/Alvin-Zilverstand/ict-algemeen-opdrachten.git
synced 2026-03-06 11:06:59 +01:00
Refactor dictionary application to use API for word meanings and update UI components
This commit is contained in:
@@ -1,59 +1,71 @@
|
||||
using System;
|
||||
using Microsoft.Data.SqlClient; // Update this line
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Woordenboek
|
||||
namespace DictionaryApp
|
||||
{
|
||||
public class Program : Form
|
||||
{
|
||||
private TextBox wordTextBox;
|
||||
private Button searchButton;
|
||||
private Label meaningLabel;
|
||||
private ListBox meaningListBox;
|
||||
private static readonly HttpClient client = new HttpClient();
|
||||
|
||||
public Program()
|
||||
{
|
||||
wordTextBox = new TextBox { Left = 50, Top = 20, Width = 200 };
|
||||
searchButton = new Button { Text = "Zoek", Left = 260, Top = 20, Width = 100 };
|
||||
meaningLabel = new Label { Left = 50, Top = 60, Width = 310 };
|
||||
searchButton = new Button { Text = "Search", Left = 260, Top = 20, Width = 100 };
|
||||
meaningListBox = new ListBox { Left = 50, Top = 60, Width = 310, Height = 200 };
|
||||
|
||||
searchButton.Click += new EventHandler(SearchButton_Click);
|
||||
wordTextBox.KeyDown += new KeyEventHandler(WordTextBox_KeyDown);
|
||||
|
||||
Controls.Add(wordTextBox);
|
||||
Controls.Add(searchButton);
|
||||
Controls.Add(meaningLabel);
|
||||
Controls.Add(meaningListBox);
|
||||
|
||||
Text = "Woordenboek";
|
||||
Size = new System.Drawing.Size(400, 150);
|
||||
Text = "Dictionary";
|
||||
Size = new System.Drawing.Size(400, 300);
|
||||
}
|
||||
|
||||
private void SearchButton_Click(object? sender, EventArgs e)
|
||||
private void WordTextBox_KeyDown(object? sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Enter)
|
||||
{
|
||||
SearchButton_Click(this, new EventArgs());
|
||||
e.SuppressKeyPress = true; // Prevent the beep sound on Enter key press
|
||||
}
|
||||
}
|
||||
|
||||
private async void SearchButton_Click(object? sender, EventArgs e)
|
||||
{
|
||||
string word = wordTextBox.Text;
|
||||
string? meaning = GetMeaningFromDatabase(word);
|
||||
meaningLabel.Text = meaning ?? "Betekenis niet gevonden.";
|
||||
string? meaning = await GetMeaningFromApi(word);
|
||||
meaningListBox.Items.Clear();
|
||||
meaningListBox.Items.Add(meaning ?? "Meaning not found.");
|
||||
}
|
||||
|
||||
private string? GetMeaningFromDatabase(string word)
|
||||
private async Task<string?> GetMeaningFromApi(string word)
|
||||
{
|
||||
string connectionString = "Server=your_server_name;Database=your_database_name;User Id=your_username;Password=your_password;";
|
||||
string query = "SELECT Meaning FROM Dictionary WHERE Word = @word";
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
try
|
||||
{
|
||||
SqlCommand command = new SqlCommand(query, connection);
|
||||
command.Parameters.AddWithValue("@word", word);
|
||||
connection.Open();
|
||||
SqlDataReader reader = command.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
return reader["Meaning"].ToString();
|
||||
string apiUrl = $"https://api.dictionaryapi.dev/api/v2/entries/en/{word}";
|
||||
HttpResponseMessage response = await client.GetAsync(apiUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
string responseBody = await response.Content.ReadAsStringAsync();
|
||||
// Parse the responseBody to extract the meaning
|
||||
// This is a simplified example, you may need to adjust the parsing based on the actual API response format
|
||||
var json = System.Text.Json.JsonDocument.Parse(responseBody);
|
||||
var meaning = json.RootElement[0].GetProperty("meanings")[0].GetProperty("definitions")[0].GetProperty("definition").GetString();
|
||||
return meaning;
|
||||
}
|
||||
else
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[STAThread]
|
||||
public static void Main()
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("woordenboek")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+aa5e80f9e3910a71e1746d19afcc838a78abfd1d")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ca3a62d7744d2cbad686550a04e7f0edbf1507ba")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("woordenboek")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("woordenboek")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@@ -1 +1 @@
|
||||
a4d97aee743d38e06f5c7332cf0b02a732fd1c583a5827cdff8f3b4115f2f4d1
|
||||
7fdaf72c06cd35ed008236cce24530d9814f5189d5f4d6d7744da073e9b12c9f
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
|
||||
{"documents":{"C:\\Users\\steen\\Desktop\\Alvin\\ict-algemeen-opdrachten\\*":"https://raw.githubusercontent.com/Alvin-Zilverstand/ict-algemeen-opdrachten/aa5e80f9e3910a71e1746d19afcc838a78abfd1d/*"}}
|
||||
{"documents":{"C:\\Users\\steen\\Desktop\\Alvin\\ict-algemeen-opdrachten\\*":"https://raw.githubusercontent.com/Alvin-Zilverstand/ict-algemeen-opdrachten/ca3a62d7744d2cbad686550a04e7f0edbf1507ba/*"}}
|
||||
Reference in New Issue
Block a user