【问题标题】:Ok and Cancel Button Not showing确定和取消按钮不显示
【发布时间】:2013-07-13 20:24:50
【问题描述】:

为什么我的确定和取消按钮没有显示在 UI 屏幕上?我看到了表单、标签和文本框,但看不到“取消”和“确定”按钮。 为了给你一个背景,我正在以编程方式创建这个对话框,我只需要几个文本框,当然还有它们的标签。还有一个确定和取消按钮。

我在这里使用的所有这些尺寸都是通过反复试验,因为我在 Visual C# 2010 的 UI 控制领域没有太多经验。

public void function x ()
{
    var fileNameDialog = new Form();
        fileNameDialog.Text = "Save New Name";
        Label fileLabel = new Label();
        fileLabel.Size = new System.Drawing.Size(150, 40);
        fileLabel.Text= "Enter Person Name";

        fileNameDialog.Controls.Add(fileLabel);
        TextBox fileTextBox = new TextBox();
        fileTextBox.Location = new System.Drawing.Point(fileLabel.Location.X + 300, fileLabel.Location.Y);
        fileTextBox.Size = new System.Drawing.Size(220, 40);
        fileNameDialog.Controls.Add(fileTextBox);
        fileTextBox.TextChanged += TextBox_TextChanged;
        fileTextBox.Text= textboxValue;
        Button okButton = new Button();
        okButton.Visible = true;
        okButton.Text = "OK";
        okButton.Location = new System.Drawing.Point(fileTextBox.Location.X, fileTextBox.Location.Y - 80);
        fileNameDialog.Controls.Add(okButton);
        okButton.Click += new EventHandler(okButton_Click);
        Button cancelButton = new Button();
        cancelButton.Visible = true;
        cancelButton.Text = "Cancel";
        fileNameDialog.Controls.Add(cancelButton);
        cancelButton.Click += new EventHandler(cancelButton_Click);
        cancelButton.Location = new System.Drawing.Point(fileTextBox.Location.X+50, fileTextBox.Location.Y - 80);

        fileNameDialog.ShowDialog();
}

【问题讨论】:

  • 尝试将fileTextBox.location.Y - 80 更改为fileTextbox.location.Y + 80
  • 为什么还要动态创建控件?
  • @chancea : 谢谢这是问题
  • @PoweredByOrange:你有什么建议?
  • 那么 Y 设置为什么?很可能它已经变成了负数。看不到动态创建组件的任何原因,但 OK/Cancel 无论如何都不是候选对象。在设计时将锚点设置为右下角。如果你想让它们消失,然后把它放在面板中并在表单的底部对齐。

标签: c# winforms visual-studio-2010 button user-interface


【解决方案1】:

您的 fileTextbox.Location.Y 为零,因此在表格上方减去 80。

试试fileTextBox.Bottom + 4 或类似的东西。

使用设计器创建此对话框表单可能是更好的选择。除了控件的放置,您还可以使用 Anchors 使控件与表单的大小相关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2021-05-07
    相关资源
    最近更新 更多