Bas*_*Bas 258
您需要创建自己的"提示"对话框.你也许可以为此创建一个类.
public static class Prompt
{
    public static string ShowDialog(string text, string caption)
    {
        Form prompt = new Form()
        {
            Width = 500,
            Height = 150,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = caption,
            StartPosition = FormStartPosition.CenterScreen
        };
        Label textLabel = new Label() { Left = 50, Top=20, Text=text };
        TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
        Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70, DialogResult = DialogResult.OK };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(textBox);
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.AcceptButton = confirmation;
        return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
    }
}
并称之为:
string promptValue = Prompt.ShowDialog("Test", "123");
更新:
根据评论和其他问题添加了默认按钮(输入键)和初始焦点.
小智 46
添加引用Microsoft.VisualBasic并将其用于您的C#代码:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", 
                       "Title", 
                       "Default", 
                       0, 
                       0);
Mar*_*mić 15
在Windows窗体中本机没有这样的东西.
您必须为此创建自己的表单或:
使用Microsoft.VisualBasic参考.
Inputbox是为.Net与VB6兼容而带入的遗留代码 - 所以我建议不要这样做.
将VisualBasic库导入C#程序通常不是一个好主意(不是因为它们不起作用,而是因为兼容性,样式和升级能力),但是你可以调用Microsoft.VisualBasic.Interaction.InputBox()显示您正在寻找的那种盒子.
如果你可以创建一个Windows.Forms对象,那将是最好的,但你说你不能这样做.
Bas 的答案理论上可以让你陷入内存问题,因为 ShowDialog 不会被处理。我认为这是一个更合适的方法。还要提到 textLabel 可以用更长的文本读取。
public class Prompt : IDisposable
{
    private Form prompt { get; set; }
    public string Result { get; }
    public Prompt(string text, string caption)
    {
        Result = ShowDialog(text, caption);
    }
    //use a using statement
    private string ShowDialog(string text, string caption)
    {
        prompt = new Form()
        {
            Width = 500,
            Height = 150,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = caption,
            StartPosition = FormStartPosition.CenterScreen,
            TopMost = true
        };
        Label textLabel = new Label() { Left = 50, Top = 20, Text = text, Dock = DockStyle.Top, TextAlign = ContentAlignment.MiddleCenter };
        TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
        Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(textBox);
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.AcceptButton = confirmation;
        return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
    }
    public void Dispose()
    {
        //See Marcus comment
        if (prompt != null) { 
            prompt.Dispose(); 
        }
    }
}
执行:
using(Prompt prompt = new Prompt("text", "caption")){
    string result = prompt.Result;
}
| 归档时间: | 
 | 
| 查看次数: | 186971 次 | 
| 最近记录: |