确定和取消按钮未显示

时间:2013-07-15 20:26:29

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

为什么我的确定和取消按钮没有显示在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();
}

1 个答案:

答案 0 :(得分:1)

你的fileTextbox.Location.Y为零,所以减去80就会放在表格上方。

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

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