dajusha

此博客展示了一个用 C# 编写的报名系统代码。包含窗口布局、按钮点击事件处理、复选框选择逻辑以及文件读取等功能。可实现报名信息的添加、保存和查询,对单人项目报名数量有限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.IO;
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 _18kaoshi
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
       
        
        public MainWindow()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            if (count3==0)
            {
                MessageBox.Show("至少选择一个项目!", "提示", MessageBoxButton.OK);
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(this.textBox1.Text + ",");
                sb.Append(this.textBox2.Text + ",");
                sb.Append(s1 + ",");
                sb.Append(this.comboBox1.Text + ",");
                if (count1==0)
                {
                    sb.Append("无,无");
                    if (count2==0)
                    {
                        sb.Append(",无");
                    }
                    else
                    {
                        sb.Append(","+s2);
                    }
                }
                else if (count1==1)
                {
                    sb.Append(sb1.ToString()+"无");
                    if (count2 == 0)
                    {
                        sb.Append(",无");
                    }
                    else
                    {
                        sb.Append(","+s2);
                    }

                }
                else if (count1 == 2)
                {
                    sb.Append(sb1.ToString());
                    if (count2 == 0)
                    {
                        sb.Append("无");
                    }
                    else
                    {
                        sb.Append(s2);
                    }
                }
               // MessageBox.Show(sb.ToString(), "提示", MessageBoxButton.OK);
                Registration registration = new Registration(sb.ToString());

                s1 = "";
                s2 = "";
                sb1.Clear();
                count1 = 0;
                count2 = 0;
                count3 = 0;
                foreach (var v in StackPanel2.Children)
                {
                    CheckBox b = v as CheckBox;
                    b.IsChecked = false;
                }
                foreach (var v in StackPanel3.Children)
                {

                    CheckBox b = v as CheckBox;
                    b.IsChecked = false;
                }
                textBox3.Text += (registration.toString() + "\r\n");
            }
           
        }

        string s1, s2;
        StringBuilder sb1 = new StringBuilder();
        int count1 = 0, count2 = 0, count3 = 0;

        private void CheckBox1_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = e.Source as CheckBox;
            if (cb.IsChecked==true)
            {
                count1 += 1;
                count3 += 1;

            }
            else
            {
                count1 -= 1;
                count3 -= 1;
            }
            if (count1>2)
            {
                cb.IsChecked = false;
                count1 -= 1;
                MessageBox.Show("单人项目限报两项!", "提示", MessageBoxButton.OK);
            }
            if (cb.IsChecked == true)
            {
                sb1.Append(cb.Content.ToString() + ",");
               // MessageBox.Show(sb1.ToString(), "提示", MessageBoxButton.OK);
            }
        }

        private void Button3_Click(object sender, RoutedEventArgs e)//读文件
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "报名.txt";
            string[] s = File.ReadAllLines(path, Encoding.Default);
            List<Registration> registrations = new List<Registration>();


            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                string line;
                while ((line = sr.ReadLine()) != null && line!="")
                {
                    MessageBox.Show(line, "提示", MessageBoxButton.OK);
                   new Registration(line);
                }
            }


         
            if (this.textBox1.Text!="")
            {
               
                foreach (var v in s)
                {

                    if (v.Contains(textBox1.Text))
                    {
                        
                        textBox3.Text = v;
                    }
                }
            }
            else if (textBox2.Text != "")
            {
                foreach (var v in s)
                {

                    if (v.Contains(textBox2.Text))
                    {

                        textBox3.Text = v;
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入姓名或学号!", "提示", MessageBoxButton.OK);
            }


        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "报名.txt";
                MessageBox.Show(path, "提示", MessageBoxButton.OK);
                File.WriteAllLines(path, textBox3.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None), Encoding.Default);
                textBox3.Text = "";
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "写入失败!");
            }
        }

        private void CheckBox2_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = e.Source as CheckBox;
            if (cb.IsChecked == true)
            {
                count2 += 1;
                count3 += 1;
            }
            else
            {
                count2 -= 1;
                count3 -= 1;
            }
            if (count2 > 1)
            {
                cb.IsChecked = false;
                MessageBox.Show("多人项目限报一项!", "提示", MessageBoxButton.OK);
            }
            if (cb.IsChecked == true) s2 = cb.Content.ToString();
        }

        private void Group1_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton r = e.Source as RadioButton;
            if (r.IsChecked == true) s1 = r.Content.ToString();
        }
        
    }
}
 

 

 

<Window x:Class="_18kaoshi.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:_18kaoshi"
        xmlns:asp="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox  Name="textBox3"  HorizontalAlignment="Left" Height="303" Margin="304,21,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="466" />
        <Button Content="保存" HorizontalAlignment="Left" Margin="443,356,0,0" VerticalAlignment="Top" Width="75" Click="Button2_Click"/>
        <Button Content="查询" HorizontalAlignment="Left" Margin="614,356,0,0" VerticalAlignment="Top" Width="75" Click="Button3_Click"/>
        <StackPanel HorizontalAlignment="Left" Height="375" Margin="22,21,0,0" VerticalAlignment="Top" Width="263">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" MinWidth="87"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition Height="70"></RowDefinition>
                    <RowDefinition Height="150"></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>

                <Label Content="学号:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right"
                Width="70"/>
                <Label Content="姓名:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right"
                Width="70"/>
                <Label Content="学院:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Right"
                Width="70"/>
                <Label Content="性别:" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Right"
                Width="70"/>

                <TextBox Name="textBox1" Grid.Column="1" Grid.Row="0" >请输入学号</TextBox>
                <TextBox Name="textBox2" Grid.Column="1" Grid.Row="1" >请输入姓名</TextBox>
                <!--<GroupBox Header="" Margin="5" BorderThickness="1" Grid.Column="1" Grid.Row="3">
                    <StackPanel  Orientation="Horizontal" RadioButton.Checked="Group1_Checked" HorizontalAlignment="Center" Name="stackPanel">
                        <RadioButton IsChecked="True" Margin="10 0 10 0" Content="男"></RadioButton>
                        <RadioButton Margin="10 0 10 0" Content="女"></RadioButton>
                    </StackPanel>
                </GroupBox>-->

               
                    <StackPanel  Grid.Column="1" Grid.Row="3" Orientation="Horizontal" RadioButton.Checked="Group1_Checked" Margin="0,0,-0.2,23.6">
                        <RadioButton>男</RadioButton>
                        <RadioButton>女</RadioButton>
                    </StackPanel>
                

                <ComboBox Name="comboBox1" Margin="10 5 10 0" Grid.Column="1" Grid.Row="2" VerticalAlignment="Top" SelectedIndex ="0">
                    <ComboBoxItem>机电与信息工程学院</ComboBoxItem>
                    <ComboBoxItem>商学院</ComboBoxItem>
                    <ComboBoxItem>法学院</ComboBoxItem>
                    <ComboBoxItem>数学院</ComboBoxItem>
                    <ComboBoxItem>艺术学院</ComboBoxItem>
                    <ComboBoxItem>文化传播学院</ComboBoxItem>
                    <ComboBoxItem>东北亚学院</ComboBoxItem>
                </ComboBox>

                <StackPanel Name="StackPanel2"  Grid.Column="0"  Grid.Row="3" 
                          HorizontalAlignment="Center"   Margin="0,25.2,10.2,0.4" Grid.RowSpan="2" CheckBox.Click="CheckBox1_Click">
                    <CheckBox Margin="5">100米</CheckBox>
                    <CheckBox Margin="5">200米</CheckBox>
                    <CheckBox Margin="5">400米</CheckBox>
                    <CheckBox Margin="5">800米</CheckBox>
                    <CheckBox Margin="5">1500米</CheckBox>
                    <CheckBox Margin="5">3000米</CheckBox>
                    
                </StackPanel>
                <StackPanel Name="StackPanel3"  Grid.Column="1"  Grid.Row="3" 
                          HorizontalAlignment="Center"   Margin="0,25.2,10.2,0.4" Grid.RowSpan="2" CheckBox.Click="CheckBox2_Click">
                    <CheckBox Margin="5">4*100米</CheckBox>
                    <CheckBox Margin="5">4*400米</CheckBox>
                </StackPanel>
                <StatusBar Grid.Column="1"  Grid.Row="5">
                    <Button Content="添加" Width="75" HorizontalAlignment="Center" Click="Button1_Click" />
                </StatusBar>

            </Grid>
        </StackPanel>
    </Grid>
</Window>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值