# Conflicts:
#	PrototypWPFHAG/MainWindow.xaml
#	PrototypWPFHAG/MainWindow.xaml.cs
This commit is contained in:
Yongkang Zhang 2025-03-19 09:45:02 +01:00
commit ebea635c53
6 changed files with 172 additions and 47 deletions

View File

@ -12,21 +12,41 @@
TitleCharacterCasing="Normal"
WindowTitleBrush="Firebrick"
Icon="pack://application:,,,/Images/databaseicon.png"
ResizeMode="CanResizeWithGrip">
ResizeMode="CanResizeWithGrip"
Loaded="Window_Loaded">
<Grid>
<Border BorderBrush="Firebrick" BorderThickness="3" Padding="20">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<!-- 用户名标签和输入框 -->
<TextBlock Text="USERNAME" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
<!-- Username -->
<TextBlock Text="Benutzername" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
<TextBox Width="200" Height="30" Margin="0,5,0,0" x:Name="UsernameTextBox" />
<!-- 密码标签和输入框 -->
<TextBlock Text="PASSWORD" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
<PasswordBox Width="200" Height="30" Margin="0,5,0,0" x:Name="PasswordTextBox" />
<TextBlock Text="Passwort" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
<Grid>
<!-- Passwortbox (hidden when Password is visible) -->
<PasswordBox Width="170" Height="30" Margin="0,5,34,0" x:Name="PasswordTextBox"
PasswordChanged="PasswordTextBox_PasswordChanged"/>
<<<<<<< HEAD
<!-- 登录按钮 -->
<Button Height="30" Width="100" Content="Log in" Margin="0,20,0,0" Click="LoginButton_Click" />
=======
<!-- Textbox for visible password (hidden at default) -->
<TextBox Width="170" Height="30" Margin="0,5,34,0" x:Name="PasswordVisibleTextBox"
Visibility="Collapsed" IsReadOnly="True"/>
<!-- Button with Eye-Icon -->
<Button Width="30" Height="30" Margin="175,5,0,0" Content="👁"
Click="ShowPasswordButton_Click"/>
</Grid>
<!-- Login Button -->
<Button Height="30" Width="100" Content="Zur Suche" Margin="0,20,0,0"
Click="loginButton_Click" IsDefault="True" />
>>>>>>> 1195250b98172d34b8c49b85a81c21d1f26cdbac
</StackPanel>
</Border>
</Grid>
</mah:MetroWindow>
</mah:MetroWindow>

View File

@ -25,46 +25,116 @@ public partial class MainWindow : MetroWindow
public MainWindow()
{
InitializeComponent();
<<<<<<< HEAD
//TestConnection();
//Console.ReadKey();
}
private static void TestConnection()
=======
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
UsernameTextBox.Focus(); // Focus first TextBox on start
}
private static NpgsqlConnection GetConnection()
{
return new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=postgres;");
}
private string ValidateUser(string username, string password)
>>>>>>> 1195250b98172d34b8c49b85a81c21d1f26cdbac
{
using (NpgsqlConnection con = GetConnection())
{
con.Open();
if (con.State == ConnectionState.Open)
// Check for correct UserName
string userQuery = "SELECT COUNT(*) FROM \"User\" WHERE \"UserName\" = @username";
using (NpgsqlCommand userCmd = new NpgsqlCommand(userQuery, con))
{
Console.WriteLine("Connected to Server");
userCmd.Parameters.AddWithValue("@username", username);
int userExists = Convert.ToInt32(userCmd.ExecuteScalar());
if (userExists == 0)
{
return "Dieser Benutzername existiert nicht";
}
}
// Check for correct Password
string pwQuery = "SELECT COUNT(*) FROM \"User\" WHERE \"UserName\" = @username AND \"Password\" = @password";
using (NpgsqlCommand pwCmd = new NpgsqlCommand(pwQuery, con))
{
pwCmd.Parameters.AddWithValue("@username", username);
pwCmd.Parameters.AddWithValue("@password", password);
int correctPassword = Convert.ToInt32(pwCmd.ExecuteScalar());
if (correctPassword == 0)
{
return "Falsches Password!";
}
}
}
<<<<<<< HEAD
}
private static NpgsqlConnection GetConnection()
{
return new NpgsqlConnection(@"Server=localhost;Port=7854;User Id=postgres;Database=postgres;");
=======
return "Erfolgreich";
>>>>>>> 1195250b98172d34b8c49b85a81c21d1f26cdbac
}
//class Program
//{
// static void Main(string[] args)
// {
// TestConnection();
// Console.ReadKey();
// }
// private static void TestConnection()
// {
// using(NpgsqlConnection con = GetConnection()){
// con.Open();
// if (con.State == ConnectionState.Open) {
// Console.WriteLine("Connected to Server");
// }
// }
// }
// private static NpgsqlConnection GetConnection()
// {
// return new NpgsqlConnection(@"Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=TestServer;");
// }
//}
private void ShowPasswordButton_PreviewMouseDown(object sender, RoutedEventArgs e)
{
PasswordVisibleTextBox.Text = PasswordTextBox.Password; // Write password in the visible box
PasswordVisibleTextBox.Visibility = Visibility.Visible;
PasswordTextBox.Visibility = Visibility.Collapsed;
}
private void ShowPasswordButton_Click(object sender, RoutedEventArgs e)
{
if (PasswordTextBox.Visibility == Visibility.Visible)
{
// Show password
PasswordVisibleTextBox.Text = PasswordTextBox.Password;
PasswordVisibleTextBox.Visibility = Visibility.Visible;
PasswordTextBox.Visibility = Visibility.Collapsed;
}
else
{
// Hide password
PasswordTextBox.Visibility = Visibility.Visible;
PasswordVisibleTextBox.Visibility = Visibility.Collapsed;
}
}
// Syncs password with visible TextBox
private void PasswordTextBox_PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordVisibleTextBox.Text = PasswordTextBox.Password;
}
private void loginButton_Click(object sender, RoutedEventArgs e)
{
string username = UsernameTextBox.Text;
string password = PasswordTextBox.Password;
string result = ValidateUser(username, password);
if (result == "Erfolgreich")
{
SearchWindow searchWindow = new();
searchWindow.Show(); // Open new window
this.Close(); // Close this window
}
else
{
MessageBox.Show(result, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}

View File

@ -4,7 +4,7 @@
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<Nullable>enable</Nullable>

View File

@ -1,7 +1,8 @@
<Window x:Class="SuchenFenster.MainWindow"
<Window x:Class="PrototypWPFHAG.SearchWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Suchen Fenster" Height="600" Width="800">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PrototypWPFHAG"
Title="Such Fenster" Height="600" Width="800">
<Grid>
<!-- 左侧垂直布局 -->
<Grid.ColumnDefinitions>
@ -40,6 +41,7 @@
</Border>
<Button Content="ADD" Width="60" Height="30" Grid.Column="1" Margin="5,0,0,0"/>
</Grid>
<Button Content="Zurück " Margin="5,380" Click="BackToLogIn_Click"/>
</StackPanel>
<!-- 右侧区域 -->
@ -47,19 +49,19 @@
<Label Content="Zeugnisse:" HorizontalAlignment="Left" Margin="0,2,0,8"/>
<!-- ListField -->
<ListBox x:Name="ListField" Margin="0,29,0,35" BorderThickness="1" BorderBrush="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<!-- 内容 -->
<TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Padding="5"/>
<!-- 分隔线 -->
<Border Height="1" Background="Black" Margin="0,2,0,2"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<!-- 内容 -->
<TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Padding="5"/>
<!-- 分隔线 -->
<Border Height="1" Background="Black" Margin="0,2,0,2"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- 删除按钮 -->
<Button Content="Löschen" Width="100" Height="30" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
</Grid>

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace PrototypWPFHAG
{
/// <summary>
/// Interaktionslogik für SearchWindow.xaml
/// </summary>
public partial class SearchWindow : Window
{
public SearchWindow()
{
InitializeComponent();
}
private void BackToLogIn_Click(object sender, RoutedEventArgs e)
{
MainWindow loginWindow = new();
loginWindow.Show(); // Open new window
this.Close(); // Close this window
}
}
}

View File

@ -1 +0,0 @@