# 55屆公開第四題 ## 題目要求 (1) 表單載入時,付現的預設值為「是」。 (2) 預約訊息須儲存於 Dictionary<String, String> record 之中。key 為日期,value 為紀錄,所有預約訊息顯示於 ListBox 中如格式: key "-" value,value 紀錄格式為:人數(一個半形空白)付現:是(或否)。 顯示如:2024 年 12 月 26 日-2 付現:是。 (3) 點選「訂房」按鈕,新增一筆紀錄顯示下方 ListBox 列表資料中,並顯示【訂房 完成】訊息。一天只能預約一筆資料,若選擇的日期已經有紀錄則無法預約,顯 示【資料已經存在無法預訂】訊息;若未選擇人數即訂房,顯示【請選擇人數】 訊息。 (4) 點選「取消」按鈕,若選擇日期沒有紀錄,則無法取消,顯示【查無紀錄】訊息; 否則刪除該筆資料,並重新顯示 ListBox 的列表資料。 (5) 資料更新時,必須重新顯示 ListBox 的列表資料。 (6) 表單載入時,付現的預設值為「是」。 (7) 點選「訂房」按鈕,新增一筆紀錄顯示下方 ListBox 列表資料中,並顯示【訂房 完成】訊息。一天只能預約一筆資料,若選擇的日期已經有紀錄則無法預約,顯 示【資料已經存在無法預訂】訊息。 ## 我的作法 > 部分有用ai除錯 ### 介面 就按照題目拉取  ### 檢測combobox文字內容 寫一個if來判斷 ```csharp if (comboBoxText == "") { MessageBox.Show("請選擇人數!"); return; } ``` ### 暫停 後面的程式抽象到我也看不懂 ### 完整程式碼 ```csharp= using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CSA04 { public partial class Form1 : Form { private Dictionary<string, string> record = new Dictionary<string, string>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 初始化時的處理邏輯(目前為空) } private void button1_Click(object sender, EventArgs e) { DateTime selectedDate = dateTimePicker1.Value; string comboBoxText = comboBox1.Text; if (string.IsNullOrEmpty(comboBoxText)) { MessageBox.Show("請選擇人數!"); return; } string dateKey = selectedDate.ToString("yyyy-MM-dd"); if (record.ContainsKey(dateKey)) { MessageBox.Show("資料已經存在,無法預訂"); return; } string displayText = $"{dateKey} -人數: {comboBoxText} 付現: {(radioButton1.Checked ? "是" : "否")}"; record[dateKey] = displayText; listBox1.Items.Add(displayText); MessageBox.Show("訂房完成"); } private void button2_Click(object sender, EventArgs e) { string dateKey = dateTimePicker1.Value.ToString("yyyy-MM-dd"); if (record.Remove(dateKey)) { for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString().Contains(dateKey)) { listBox1.Items.RemoveAt(i); break; } } MessageBox.Show("取消訂房完成"); } else { MessageBox.Show("查無紀錄"); } } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up