Часто при написании каркаса программы необходимо вместо расфуфырнного окошка ввода значений просто побыстряку ввести какую либо строку/значение, в C++Builder для этой цели была функция InputQuery,
к сожалению в .NET Framework кроме корявой функции из сборки VisualBasic ничего найти не удалось,
предлагаю аналог в виде класса на C#
inputbox.cs:
public class InputBox : Form
{
private InputBox(string Caption, string Text)
{
this.label = new System.Windows.Forms.Label();
this.textValue = new System.Windows.Forms.TextBox();
this.buttonOK = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(9, 13);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(31, 13);
this.label.TabIndex = 1;
this.label.Text = Text;
this.textValue.Location = new System.Drawing.Point(12, 31);
this.textValue.Name = "textValue";
this.textValue.Size = new System.Drawing.Size(245, 20);
this.textValue.TabIndex = 2;
this.textValue.WordWrap = false;
this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.buttonOK.Location = new System.Drawing.Point(57, 67);
this.buttonOK.Name = "buttonOK";
this.buttonOK.Size = new System.Drawing.Size(75, 23);
this.buttonOK.TabIndex = 3;
this.buttonOK.Text = "OK";
this.buttonOK.UseVisualStyleBackColor = true;
this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonCancel.Location = new System.Drawing.Point(138, 67);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 23);
this.buttonCancel.TabIndex = 4;
this.buttonCancel.Text = "Cancel";
this.buttonCancel.UseVisualStyleBackColor = true;
this.AcceptButton = this.buttonOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.buttonCancel;
this.ClientSize = new System.Drawing.Size(270, 103);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK);
this.Controls.Add(this.textValue);
this.Controls.Add(this.label);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = Caption;
this.ResumeLayout(false);
this.PerformLayout();
}
public static bool Query(string Caption, string Text, ref string s_val)
{
InputBox ib = new InputBox(Caption, Text);
ib.textValue.Text = s_val;
if (ib.ShowDialog() != DialogResult.OK) return false;
s_val = ib.textValue.Text;
return true;
}
public static bool InputValue(string Caption, string Text, string prefix, string format, ref int value, int min, int max)
{
int val = value;
string s_val = prefix + value.ToString(format);
bool OKVal;
do
{
OKVal = true;
if (!Query(Caption, Text, ref s_val)) return false;
try
{
string sTr = s_val.Trim();
if ((sTr.Length > 0) && (sTr[0] == '#'))
{
sTr = sTr.Remove(0, 1);
val = Convert.ToInt32(sTr, 16);
}
else if ((sTr.Length > 1) && ((sTr[1] == 'x') && (sTr[0] == '0')))
{
sTr = sTr.Remove(0, 2);
val = Convert.ToInt32(sTr, 16);
}
else
val = Convert.ToInt32(sTr, 10);
}
catch { MessageBox.Show("Требуется ввести число!"); OKVal = false; }
if ((val < min) || (val > max)) { MessageBox.Show("Требуется число в диапазоне " + min.ToString() + "..." + max.ToString() + " !"); OKVal = false; }
} while (!OKVal);
value = val;
return true;
}
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textValue;
private System.Windows.Forms.Button buttonOK;
private System.Windows.Forms.Button buttonCancel;
}
пример использования:
string value = "abcd";
if(!InputBox.Query("Ввод строки", "Строка:", ref value))
MessageBox.Show("Cancel");
else
MessageBox.Show(value);
можно также вводить значения int:
int value=0;
if(!InputBox.InputValue("Ввод числа X", "Значение X:", "0x", "X4", ref value, 0, 0xFFFF)) return;
MessageBox.Show("Введено число X="+value.ToString());
при вводе числа можно использовать 16-ричную кодировку, для этого число должно начинаться с 0x или с #
впрочем при ненужности метод InputValue можно удалить...
Тот кто говорит не знает, тот кто знает не говорит.
сорри, перепутал кнопочку
inputbox.cs:
public class InputBox : Form
{
private InputBox(string Caption, string Text)
{
this.label = new System.Windows.Forms.Label();
this.textValue = new System.Windows.Forms.TextBox();
this.buttonOK = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(9, 13);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(31, 13);
this.label.TabIndex = 1;
this.label.Text = Text;
this.textValue.Location = new System.Drawing.Point(12, 31);
this.textValue.Name = "textValue";
this.textValue.Size = new System.Drawing.Size(245, 20);
this.textValue.TabIndex = 2;
this.textValue.WordWrap = false;
this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.buttonOK.Location = new System.Drawing.Point(57, 67);
this.buttonOK.Name = "buttonOK";
this.buttonOK.Size = new System.Drawing.Size(75, 23);
this.buttonOK.TabIndex = 3;
this.buttonOK.Text = "OK";
this.buttonOK.UseVisualStyleBackColor = true;
this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonCancel.Location = new System.Drawing.Point(138, 67);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 23);
this.buttonCancel.TabIndex = 4;
this.buttonCancel.Text = "Cancel";
this.buttonCancel.UseVisualStyleBackColor = true;
this.AcceptButton = this.buttonOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.buttonCancel;
this.ClientSize = new System.Drawing.Size(270, 103);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK);
this.Controls.Add(this.textValue);
this.Controls.Add(this.label);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = Caption;
this.ResumeLayout(false);
this.PerformLayout();
}
public static bool Query(string Caption, string Text, ref string s_val)
{
InputBox ib = new InputBox(Caption, Text);
ib.textValue.Text = s_val;
if (ib.ShowDialog() != DialogResult.OK) return false;
s_val = ib.textValue.Text;
return true;
}
public static bool InputValue(string Caption, string Text, string prefix, string format, ref int value, int min, int max)
{
int val = value;
string s_val = prefix + value.ToString(format);
bool OKVal;
do
{
OKVal = true;
if (!Query(Caption, Text, ref s_val)) return false;
try
{
string sTr = s_val.Trim();
if ((sTr.Length > 0) && (sTr[0] == '#'))
{
sTr = sTr.Remove(0, 1);
val = Convert.ToInt32(sTr, 16);
}
else if ((sTr.Length > 1) && ((sTr[1] == 'x') && (sTr[0] == '0')))
{
sTr = sTr.Remove(0, 2);
val = Convert.ToInt32(sTr, 16);
}
else
val = Convert.ToInt32(sTr, 10);
}
catch { MessageBox.Show("Требуется ввести число!"); OKVal = false; }
if ((val < min) || (val > max)) { MessageBox.Show("Требуется число в диапазоне " + min.ToString() + "..." + max.ToString() + " !"); OKVal = false; }
} while (!OKVal);
value = val;
return true;
}
private System.Windows.Forms.Label label;
private System.Windows.Forms.TextBox textValue;
private System.Windows.Forms.Button buttonOK;
private System.Windows.Forms.Button buttonCancel;
}
Тот кто говорит не знает, тот кто знает не говорит.
Здравствуйте, Streamer1, Вы писали:
Вот тебе то же самое, только с локализацией в ресурсах.
Бросаешь компонент на форму, выставляшь св-ва в редакторе св-в, вызов очень простой:
string answer = inputBox.Ask();
string answer = inputBox.Ask(defaultAnswer);
Код компонент:
/// <summary>
/// Summary description for InputBox.
/// </summary>
[ToolboxItem(true)]
public class InputBox : System.ComponentModel.Component {
protected string _prompt = string.Empty;
protected string _caption = string.Empty;
protected string _errorMsgOnEmpty;
private InputBoxForm inputBox;
public InputBox(System.ComponentModel.IContainer container) {
container.Add(this);
}
public InputBox() {}
/// <summary>
/// Property Prompt (string)
/// </summary>
[DefaultValue("")]
public string Prompt {
get { return _prompt; }
set { _prompt = value; }
}
/// <summary>
/// Property Caption (string)
/// </summary>
[DefaultValue("")]
public string Caption {
get { return _caption; }
set { _caption = value; }
}
/// <summary>
/// Property ErrorMsgOnEmpty (string)
/// </summary>
[DefaultValue("")]
public string ErrorMsgOnEmpty {
get { return _errorMsgOnEmpty; }
set { _errorMsgOnEmpty = value; }
}
/// <summary>
/// Shows input-box
/// </summary>
/// <param name="defText">Default text for InputBox</param>
/// <returns>null if Cancel has been pressed or string</returns>
public virtual string Ask(string defText) {
if (inputBox == null)
inputBox = new InputBoxForm();
if (_caption != "")
inputBox.Text = _caption;
else
inputBox.Text = Application.ProductName;
inputBox.Prepare(_prompt, _errorMsgOnEmpty, defText);
DialogResult res = inputBox.ShowDialog();
if (res != DialogResult.OK)
return null;
else
return inputBox.txtInputText.Text.Trim();
}
/// <summary>
/// Shows input-box
/// </summary>
/// <returns>null if Cancel has been pressed or string</returns>
public string Ask() {
return Ask("");
}
}
public class InputBoxForm : System.Windows.Forms.Form {
...
public void Prepare(string prompt, string errorMsgOnEmpty, string defText) {
lblInputLabel.Text = prompt + ':';
ErrorMsgOnEmpty = errorMsgOnEmpty;
txtInputText.Text = defText;
ActiveControl = txtInputText;
errorProvider.SetError(txtInputText, "");
}
...
}
... << RSDN@Home 1.1.4 stable SR1 rev. 568>>