Код xaml
Код xaml
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel x:Name="CaptchaStackPanel" Orientation="Horizontal">
</StackPanel>
<TextBox x:Name="AnswerBox" MinWidth="150"/>
<Button Content="Ок" Margin="0 10 0 0" Click="Button_Click" Background="Green" Foreground="White"/>
</StackPanel>
Код c#
Random rnd = new Random();
string CorrectAnswer = "";
public MainWindow()
{
InitializeComponent();
GenerateCaptcha();
}
public void GenerateCaptcha()
{
CaptchaStackPanel.Children.Clear();
CorrectAnswer = "";
for (int i = 0; i < 4; i++)
{
TextBlock textblock = new TextBlock();
textblock.FontSize = 26;
textblock.TextDecorations = TextDecorations.Strikethrough;
textblock.Margin = new Thickness(0, rnd.Next(-10, 10), 0, rnd.Next(-10, 10));
string tempChar = GenerateRandomString();
CorrectAnswer += tempChar;
textblock.Text = tempChar;
CaptchaStackPanel.Children.Add(textblock);
}
}
public string GenerateRandomString()
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
char randomChars = chars[rnd.Next(chars.Length)];
return randomChars.ToString();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (AnswerBox.Text == CorrectAnswer)
{
MessageBox.Show("Ответ верный");
UserWindow userWindow = new UserWindow(AnswerBox.Text);
userWindow.Login("1");
userWindow.Show();
AnswerBox.Text = "";
}
else
{
MessageBox.Show("Ответ неверный");
AnswerBox.Text = "";
GenerateCaptcha();
}
}