Bài viết

Kho tài liệu và bài viết được chia sẻ, đánh giá bởi cộng đồng

Tạo Captcha trong WPF

Vo Tan Duc đã tạo 20:28 29-06-2023 868 lượt xem 0 bình luận

Nội dung bài viết

MainWindow.xaml

<Window x:Class="CreateCaptcha_WPF.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:CreateCaptcha_WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock Height="53" FontFamily="VNI-Viettay" HorizontalAlignment="Left" FontSize="28" Foreground="Red" FontStyle="Italic" FontWeight="Heavy" Margin="86,105,0,0" Name="textBox1" VerticalAlignment="Top" Width="auto" Background="{x:Null}" FontStretch="UltraCondensed"></TextBlock>
        <Button Content="Tạo captcha" Click="Button_Click" Height="31" HorizontalAlignment="Left" Margin="112,200,0,0" Name="button1" VerticalAlignment="Top" Width="161" ></Button>
    </Grid>
</Window>

MainWindow.xaml.cs

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.Navigation;
using System.Windows.Shapes;

namespace CreateCaptcha_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Random random = new Random();
            int num = random.Next(10, 20);
            string captcha = "";
            int totl = 0;
            do
            {
                int chr = random.Next(100, 123);
                if ((chr >= 18 && chr <= 57) || (chr >= 65 && chr <= 90) || (chr >= 97 && chr <= 122))
                {
                    captcha += (char)chr;
                    totl++;
                    if (totl == num)
                    {
                        break;
                    }
                }
            } while (true);
            textBox1.Text = captcha;
        }
    }
}

 

Nội dung bài viết

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập

Câu hỏi mới nhất