From 2b5198ff7738d21e7ad0bf386f62e402350ab7a1 Mon Sep 17 00:00:00 2001 From: Sparronator9999 <86388887+Sparronator9999@users.noreply.github.com> Date: Fri, 6 Dec 2024 06:44:52 +1100 Subject: [PATCH] Add input validation to text input dialog --- .../Dialogs/TextInputDialog.Designer.cs | 47 ++++++++++--------- YAMDCC.GUI/Dialogs/TextInputDialog.cs | 10 +++- YAMDCC.GUI/Dialogs/TextInputDialog.resx | 3 -- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/YAMDCC.GUI/Dialogs/TextInputDialog.Designer.cs b/YAMDCC.GUI/Dialogs/TextInputDialog.Designer.cs index c051add..3106a8a 100644 --- a/YAMDCC.GUI/Dialogs/TextInputDialog.Designer.cs +++ b/YAMDCC.GUI/Dialogs/TextInputDialog.Designer.cs @@ -47,28 +47,16 @@ private void InitializeComponent() System.Windows.Forms.TableLayoutPanel tblMain; System.Windows.Forms.FlowLayoutPanel flwButtons; System.Windows.Forms.Button btnCancel; - System.Windows.Forms.Button btnOK; this.lblCaption = new System.Windows.Forms.Label(); this.txtInput = new System.Windows.Forms.TextBox(); + this.btnOK = new System.Windows.Forms.Button(); tblMain = new System.Windows.Forms.TableLayoutPanel(); flwButtons = new System.Windows.Forms.FlowLayoutPanel(); btnCancel = new System.Windows.Forms.Button(); - btnOK = new System.Windows.Forms.Button(); tblMain.SuspendLayout(); flwButtons.SuspendLayout(); this.SuspendLayout(); // - // lblCaption - // - this.lblCaption.AutoSize = true; - this.lblCaption.Dock = System.Windows.Forms.DockStyle.Fill; - this.lblCaption.Location = new System.Drawing.Point(10, 6); - this.lblCaption.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); - this.lblCaption.Name = "lblCaption"; - this.lblCaption.Size = new System.Drawing.Size(280, 15); - this.lblCaption.TabIndex = 1; - this.lblCaption.Text = "Caption"; - // // tblMain // tblMain.AutoSize = true; @@ -88,6 +76,17 @@ private void InitializeComponent() tblMain.Size = new System.Drawing.Size(300, 89); tblMain.TabIndex = 0; // + // lblCaption + // + this.lblCaption.AutoSize = true; + this.lblCaption.Dock = System.Windows.Forms.DockStyle.Fill; + this.lblCaption.Location = new System.Drawing.Point(10, 6); + this.lblCaption.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.lblCaption.Name = "lblCaption"; + this.lblCaption.Size = new System.Drawing.Size(280, 15); + this.lblCaption.TabIndex = 1; + this.lblCaption.Text = "Caption"; + // // txtInput // this.txtInput.Dock = System.Windows.Forms.DockStyle.Fill; @@ -96,12 +95,13 @@ private void InitializeComponent() this.txtInput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtInput.Size = new System.Drawing.Size(282, 23); this.txtInput.TabIndex = 2; + this.txtInput.TextChanged += new System.EventHandler(this.txtInput_TextChanged); // // flwButtons // flwButtons.AutoSize = true; flwButtons.Controls.Add(btnCancel); - flwButtons.Controls.Add(btnOK); + flwButtons.Controls.Add(this.btnOK); flwButtons.Dock = System.Windows.Forms.DockStyle.Fill; flwButtons.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft; flwButtons.Location = new System.Drawing.Point(9, 53); @@ -121,18 +121,18 @@ private void InitializeComponent() // // btnOK // - btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; - btnOK.Location = new System.Drawing.Point(123, 3); - btnOK.Name = "btnOK"; - btnOK.Size = new System.Drawing.Size(75, 23); - btnOK.TabIndex = 0; - btnOK.Text = "&OK"; - btnOK.UseVisualStyleBackColor = true; - btnOK.Click += new System.EventHandler(this.btnOK_Click); + this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; + this.btnOK.Location = new System.Drawing.Point(123, 3); + this.btnOK.Name = "btnOK"; + this.btnOK.Size = new System.Drawing.Size(75, 23); + this.btnOK.TabIndex = 0; + this.btnOK.Text = "&OK"; + this.btnOK.UseVisualStyleBackColor = true; + this.btnOK.Click += new System.EventHandler(this.btnOK_Click); // // TextInputDialog // - this.AcceptButton = btnOK; + this.AcceptButton = this.btnOK; this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.AutoSize = true; @@ -160,5 +160,6 @@ private void InitializeComponent() private System.Windows.Forms.Label lblCaption; private System.Windows.Forms.TextBox txtInput; + private System.Windows.Forms.Button btnOK; } } diff --git a/YAMDCC.GUI/Dialogs/TextInputDialog.cs b/YAMDCC.GUI/Dialogs/TextInputDialog.cs index 734664d..ab2c80d 100644 --- a/YAMDCC.GUI/Dialogs/TextInputDialog.cs +++ b/YAMDCC.GUI/Dialogs/TextInputDialog.cs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License along with // YAMDCC. If not, see . +using System; using System.Windows.Forms; namespace YAMDCC.GUI.Dialogs @@ -35,9 +36,16 @@ public TextInputDialog(string caption, string title = "", string text = "", bool Text = title; } - private void btnOK_Click(object sender, System.EventArgs e) + private void btnOK_Click(object sender, EventArgs e) { Result = txtInput.Text; } + + private void txtInput_TextChanged(object sender, EventArgs e) + { + // make sure text input isn't empty + // before allowing user to click "OK": + btnOK.Enabled = !string.IsNullOrEmpty(txtInput.Text); + } } } diff --git a/YAMDCC.GUI/Dialogs/TextInputDialog.resx b/YAMDCC.GUI/Dialogs/TextInputDialog.resx index 236a169..15f08d2 100644 --- a/YAMDCC.GUI/Dialogs/TextInputDialog.resx +++ b/YAMDCC.GUI/Dialogs/TextInputDialog.resx @@ -126,7 +126,4 @@ False - - False - \ No newline at end of file