110,533
社区成员
发帖
与我相关
我的任务
分享
public void TextBox_Usersname(object sender, TextChangedEventArgs e)
{
string str1 = Console.ReadLine();
}
public void TextBox_Password(object sender, TextChangedEventArgs e)
{
string str2 = Console.ReadLine();
}
//public static void CompareTXT()
//{
// TextBox_Username str1 = new TextBox.str1();
//}
private void Login_btn(object sender, RoutedEventArgs e)
{
if (TextBox_Usersname.Text == "" || TextBox_Password.Text == "") {
MessageBox.Show("Can't be empty!");
return;
}
private void Login_btn(object sender, RoutedEventArgs e) {
if (TextBox_Usersname.Text == "" || TextBox_Password.Text == "") {
MessageBox.Show("Can't be empty!");
return;
}
try {
string str1, str2;
str1 = Convert.ToString(TextBox_Usersname.Text);
str2 = Convert.ToString(TextBox_Password.Text);
bool is_login = false;
string filePath = System.AppDomain.CurrentDomain.BaseDirectory +"text.txt";
string[] files = File.ReadAllLines(filePath);
foreach (var v in files) {
string[] user = Regex.Split(v," ", RegexOptions.IgnoreCase);
if (str1==user[0]&&str2==user[1]) {
//form2.Show();
//this.DialogResult = Convert.ToBoolean(1);
is_login = true;
MessageBox.Show("登录成功","提示",MessageBoxButton.OK,MessageBoxImage.Asterisk);
this.Close();
break;
}
}
if (!is_login) {
MessageBox.Show("Uersname or Passowrd is error!");
}
}
catch (Exception) {
MessageBox.Show("输入错误");
}
}
我改了下你的代码 就可以了,不过我也是初学者 可能写的不好
我原本的设想是在用户删除文字的时候进行判断检测是否是可用的账号密码,不过现在看来似乎就算去掉了也没啥关系吧。。<Window x:Class="Login.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Login"
mc:Ignorable="d"
Title="Login" Height="450" Width="800">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF57B9AC" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<TextBox HorizontalAlignment="Left" Height="31" Margin="333,260,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" TextChanged="TextBox_Password" Name="TextBox_Password"/>
<TextBox HorizontalAlignment="Left" Height="31" Margin="333,189,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" TextChanged="TextBox_Usersname" Name="TextBox_Usersname"/>
<Label Content="Usersname :" HorizontalAlignment="Left" Height="32" Margin="241,189,0,0" VerticalAlignment="Top" Width="80"/>
<Label Content="Password :" HorizontalAlignment="Left" Height="31" Margin="251,260,0,0" VerticalAlignment="Top" Width="80"/>
<Label Content="This is a game" HorizontalAlignment="Left" Height="79" Margin="228,62,0,0" VerticalAlignment="Top" Width="356" FontWeight="Bold" FontSize="48" Foreground="#FF16D8D0"/>
<Button Content="Login" HorizontalAlignment="Left" Height="35" Margin="294,353,0,0" VerticalAlignment="Top" Width="110" Click="Login_btn" />
<Button Content="Quit" HorizontalAlignment="Left" Height="35" Margin="469,353,0,0" VerticalAlignment="Top" Width="115" Click="Quit_btn"/>
</Grid>
</Window>
using System;
using System.IO;
using System.Text;
using System.Windows;
namespace Login
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Login_btn(object sender, RoutedEventArgs e)
{
if (TextBox_Usersname.Text == "" || TextBox_Password.Text == "") {
MessageBox.Show("Can't be empty!");
return;
}
try
{
string str1, str2;
str1 = Convert.ToString(TextBox_Usersname.Text);
str2 = Convert.ToString(TextBox_Password.Text);
string filePath = "C:\\MMORPG_DB\\highly_secure_user_file.txt";
StreamReader sr = new StreamReader(filePath, Encoding.Default);
while (sr.ReadLine() != null)
if (str1 == sr.ReadLine() || str2 == sr.ReadLine())
{
ServerState form2 = new ServerState();
form2.Show();
//this.DialogResult = Convert.ToBoolean(1);
this.Close();
break;
}
else
{
MessageBox.Show("Uersname or Passowrd is error!");
}
}
catch(Exception)
{
}
}
private void Quit_btn(object sender, RoutedEventArgs e)
{
App.Current.Shutdown();
}
}
}