-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputDialog.cs
More file actions
89 lines (79 loc) · 3.1 KB
/
Copy pathInputDialog.cs
File metadata and controls
89 lines (79 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace QQLogin
{
public class InputDialog : Form
{
private Label labelPrompt;
private TextBox textBox;
private Button button1;
private Button button2;
public string InputText => textBox.Text; // 公共属性用于获取输入的内容
public string PromptText
{
get => labelPrompt.Text;
set => labelPrompt.Text = value;
}
public InputDialog(string title="", string prompt = "请输入:")
{
// 窗口设置
this.Text = title;
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedDialog; // 固定对话框尺寸
//最大化按钮和最小化按钮取消
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Size = new Size(400, 200);
// 创建提示标签
labelPrompt = new Label();
labelPrompt.Text = prompt;
labelPrompt.Location = new Point(50, 0);
labelPrompt.AutoSize = true;
this.Controls.Add(labelPrompt);
// 创建输入框
textBox = new TextBox();
textBox.Location = new Point(50, 30);
textBox.Size = new Size(300, 20);
// 添加KeyDown事件处理,确保按回车键可以实现确定效果
textBox.KeyDown += TextBox_KeyDown;
this.Controls.Add(textBox);
// 创建第一个按钮
button1 = new Button();
button1.Text = "确定";
button1.Location = new Point(100, 80);
button1.Size = new Size(80, 30);
//button1.Click += (sender, e) => InputAuthCode();
button1.DialogResult = DialogResult.OK; // 设置对话框结果
this.Controls.Add(button1);
// 创建第二个按钮
button2 = new Button();
button2.Text = "取消";
button2.Location = new Point(220, 80);
button2.Size = new Size(80, 30);
//button2.Click += (sender, e) => this.Close(); ;
button2.DialogResult = DialogResult.Cancel; // 设置对话框结果
this.Controls.Add(button2);
// 在创建完按钮后再设置AcceptButton和CancelButton,确保引用有效
this.AcceptButton = button1; // 设置回车默认按钮
this.CancelButton = button2; // 设置ESC默认按钮
}
/// <summary>
/// 处理输入框的按键事件,确保按回车键可以触发确定按钮的点击事件
/// </summary>
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
// 如果按下的是回车键
if (e.KeyCode == Keys.Enter)
{
// 模拟点击确定按钮
button1.PerformClick();
// 阻止默认行为
e.SuppressKeyPress = true;
}
}
}
}