Click to See Complete Forum and Search --> : C# Forms - Custom Popup InputBox


bixel
April 24th, 2009, 09:00 PM
I've created a custom popup input box (much like the one from VB). It saves the string that has been inputed. Most of the code has been imported from here >>

http://www.csharp-examples.net/inputbox/

I want to add a Enter key functionality, in-order to speed up the process since the user will be using this box a lot.


private void EnterKey(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
InputResponse = this.txtInput.Text;
this.Close();
}
}


Since the custom Input box is created on the fly - I can't simply right click its object and add the event. It needs to be done in code. But I am not able to get it correct.


//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(10, 36);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(200, 20);
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
this.txtInput.??? += new System.EventHandler(this.EnterKey);
//


I've tried this.txtInput.TextChanged -- but the error is

No overload for 'EnterKey' matches delegate 'System.EventHandler'

dunno what to do here.

pgrammer
April 24th, 2009, 10:14 PM
You make things way too complicated. If its anything like the VB input box it has an enter button and a cancel button. You need two statements.


Inputbox.AcceptButton = btnEnter;
Inputbox.CancelButton = btnCancel;


Optionally you can set the DialogResult property for the buttons so you can tell what button was clicked.

bixel
April 24th, 2009, 10:22 PM
I already have the buttons. But I want to enter the text and hit the Enter key on my keyboard - its much faster.

pgrammer
April 24th, 2009, 11:12 PM
I already have the buttons. But I want to enter the text and hit the Enter key on my keyboard - its much faster.

I understand what you are asking. I provided the solution above. There is no need for you to write this maunally when the .Net Framework has built in support for what you want to do.

If you need to return a string to the calling code you can do it with a delegate. There is no need for you to test for the enter button maunally.

Hook up your buttons like I showed you before and test it.

bixel
April 24th, 2009, 11:20 PM
I'm sorry I don't understand.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Resturant_Helper
{
/// <summary>
/// Summary description for InputBox.
///
public class InputBoxDialog : System.Windows.Forms.Form
{

#region Windows Contols and Constructor

private System.Windows.Forms.Label lblPrompt;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtInput;
/// <summary>
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

public InputBoxDialog()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

#endregion

#region Dispose

/// <summary>
/// Clean up any resources being used.
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#endregion

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.lblPrompt = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.txtInput = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// lblPrompt
//
this.lblPrompt.Anchor =
((System.Windows.Forms.AnchorStyles)
((((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lblPrompt.BackColor = System.Drawing.SystemColors.Control;
this.lblPrompt.Font =
new System.Drawing.Font("Microsoft Sans Serif", 10.0F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.lblPrompt.Location = new System.Drawing.Point(12, 6);
this.lblPrompt.Name = "lblPrompt";
this.lblPrompt.Size = new System.Drawing.Size(180, 82);
this.lblPrompt.TabIndex = 3;
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnOK.Location = new System.Drawing.Point(56, 70);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(32, 20);
this.btnOK.TabIndex = 1;
this.btnOK.Text = "&OK";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// button1
//
this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button1.Location = new System.Drawing.Point(110, 70);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 20);
this.button1.TabIndex = 2;
this.button1.Text = "&Cancel";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(10, 36);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(200, 20);
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
//
// InputBoxDialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(228, 96);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.button1);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.lblPrompt);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBoxDialog";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "InputBox";
this.Load += new System.EventHandler(this.InputBox_Load);
this.ResumeLayout(false);

}
#endregion

#region Private Variables
string formCaption = string.Empty;
string formPrompt = string.Empty;
string inputResponse = string.Empty;
string defaultValue = string.Empty;
#endregion

#region Public Properties
public string FormCaption
{
get { return formCaption; }
set { formCaption = value; }
} // property FormCaption
public string FormPrompt
{
get { return formPrompt; }
set { formPrompt = value; }
} // property FormPrompt
public string InputResponse
{
get { return inputResponse; }
set { inputResponse = value; }
} // property InputResponse
public string DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; }
} // property DefaultValue

#endregion

#region Form and Control Events
private void InputBox_Load(object sender, System.EventArgs e)
{
this.txtInput.Text = defaultValue;
this.lblPrompt.Text = formPrompt;
this.Text = formCaption;
this.txtInput.SelectionStart = 0;
this.txtInput.SelectionLength = this.txtInput.Text.Length;
this.txtInput.Focus();
}

private void btnOK_Click(object sender, System.EventArgs e)
{
InputResponse = this.txtInput.Text;
this.Close();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
#endregion

private void EnterKey(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
InputResponse = this.txtInput.Text;
this.Close();
}
}

}
}

pgrammer
April 25th, 2009, 12:32 AM
Sadly, I am using my cell phone to view the website right now so it is very hard for me to view rhe code.

Until I get to a PC I won't be able to make much sense of it.

in your Form_Load you can add this line for now.


this.AcceptButton = btnOk;


If you can't figure it out and no one else helps. You'll have to wait until I get to a PC so that I can view your code. Its too hard to read it on a cell phone.

JonnyPoet
April 26th, 2009, 05:17 PM
What you are looking for is the Keydown Event from the textbox
this.txtBox.KeyDown += new
KeyEventHandler(this.txtBox_KeyDown)


// and the delegate
private void txtBox_KeyDown(object sender KeyEventArgs e){


}

pgrammer
April 27th, 2009, 11:38 AM
What you are looking for is the Keydown Event from the textbox
this.txtBox.KeyDown += new
KeyEventHandler(this.txtBox_KeyDown)


// and the delegate
private void txtBox_KeyDown(object sender KeyEventArgs e){


}



It would work, but I still don't understand why he is coding something manually that is already supported by the Framework. There is no reason to trap the enter key in that event in order to close the dialog.

Anyway, Good Luck.

JonnyPoet
April 27th, 2009, 12:25 PM
It would work, but I still don't understand why he is coding something manually that is already supported by the Framework. There is no reason to trap the enter key in that event in order to close the dialog.

Anyway, Good Luck.As much as I have understood he doesn't want to click one of the buttons. He has a textbox and the two buttons :Ok and cancel there, but wanted to close the box with DialogResuslt.Ok when he simple presses 'Enter' after entering some text in the textbox. So if enterkey is pressed being in the textbox the Dialog should close with result ok. So for this he can use the delegate of the KeDown in the textbox.

BigEd781
April 27th, 2009, 12:51 PM
As much as I have understood he doesn't want to click one of the buttons. He has a textbox and the two buttons :Ok and cancel there, but wanted to close the box with DialogResuslt.Ok when he simple presses 'Enter' after entering some text in the textbox. So if enterkey is pressed being in the textbox the Dialog should close with result ok. So for this he can use the delegate of the KeDown in the textbox.

I believe that simply setting the "Accept" and "Cancel" button properties of the form will do just that, but I can't test it right now.

JonnyPoet
April 27th, 2009, 01:27 PM
I believe that simply setting the "Accept" and "Cancel" button properties of the form will do just that, but I can't test it right now.I just tested it. it works. I must confess I never used that. But maybe, because I normally dont have any dialogs which only have simple one textbox. And when I have more then one ( as usual in most of my programs ) I dislike to press enter for closing. In that case I want the focus to move to the next textbox ( In our firm no One uses the Tab button for this, all are wanting to have the enter button doing this.

bixel please do the following code for example in your constructor just after InitializeComponents();
public partial class Form2 : Form {
public InputBoxDialog() {
InitializeComponent();
this.AcceptButton = btnOK;
this.CancelButton = button1;
}
}

pgrammer
April 28th, 2009, 09:35 PM
I believe that simply setting the "Accept" and "Cancel" button properties of the form will do just that, but I can't test it right now.

It will.

That's what I've been trying to tell the OP for several days now.

Some people like doing things the hard way I guess.