【问题标题】:How to layout items in winform如何在winform中布局项目
【发布时间】:2019-12-30 22:28:58
【问题描述】:

我正在做一个作业 c# winforms 项目,并希望在我的主窗体的右上角添加日期和时间,这样在第一行我有一个写在一个标签中的日期,而在第二行我有时间写在第二个标签上。 如果调整表格大小,我还需要将它们粘贴在右上角。

我不知道这是否重要,但那些标签控件位于顶部停靠在表单中的面板内,并且该面板已经包含两个停靠在左侧的控件。

example of what I want

我一直在使用 anchor 和 dock 属性,但我无法让它以我想要的方式工作。

private void GlavnaForma_Load(object sender, EventArgs e)
{
    timerDateTime.Start();
    lblDate.Text = DateTime.Now.ToString("dddd, dd.M.yyyy", new CultureInfo("hr-HR"));
    lblTime.Text = DateTime.Now.ToString("HH:mm:ss", new CultureInfo("hr-HR"));
}

private void timerDateTime_Tick(object sender, EventArgs e)
{
    lblDate.Text = DateTime.Now.ToString("dddd, dd.M.yyyy", new CultureInfo("hr-HR"));
    lblTime.Text = DateTime.Now.ToString("HH:mm:ss", new CultureInfo("hr-HR"));
}

【问题讨论】:

    标签: c# visual-studio winforms layout


    【解决方案1】:

    将锚点设置为顶部,如下所示:

    【讨论】:

    • 另外,他可能希望将文本“RightToLeft”属性设置为 true。
    • William,感谢您的回答,但如果我对两个控件都这样做,我会得到:ibb.co/SX8gRnn
    • “RightToLeft”属性设置为 true 也不能解决问题
    • 尝试将 TextAlign 设置为 MiddleRight 并将 AutoSize 设置为 false。然后将标签调整到你想要的大小。
    • 我设法通过将 TextAlign 属性设置为“MiddleRight”并将 Dock 属性设置为“Right”来获得所需的第一行......现在它可以正常显示了。如何让第二行停靠在第一行下方?
    【解决方案2】:

    有几种方法可以做到这一点。

    我可能会让主窗体有一个一列三行的表格布局面板。将前两行设为绝对大小,第三行的大小类型为“percent”,值为 100.0%,以占用所有剩余空间。然后在前两行分别放置一个标签,并通过将标签的“Dock”属性设置为“Right”来使标签向右对齐。

    所有这些都可以在表单设计器 GUI 中完成。生成的代码如下所示:

    this.tableLayout = new System.Windows.Forms.TableLayoutPanel();
    this.labelDate = new System.Windows.Forms.Label();
    this.labelTime = new System.Windows.Forms.Label();
    this.tableLayout.SuspendLayout();
    this.SuspendLayout();
    // 
    // tableLayout
    // 
    this.tableLayout.ColumnCount = 1;
    this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
    this.tableLayout.Controls.Add(this.labelDate, 0, 0);
    this.tableLayout.Controls.Add(this.labelTime, 0, 1);
    this.tableLayout.Dock = System.Windows.Forms.DockStyle.Fill;
    this.tableLayout.Location = new System.Drawing.Point(0, 0);
    this.tableLayout.Name = "tableLayout";
    this.tableLayout.RowCount = 3;
    this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F));
    this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F));
    this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
    this.tableLayout.Size = new System.Drawing.Size(800, 450);
    this.tableLayout.TabIndex = 0;
    // 
    // labelDate
    // 
    this.labelDate.AutoSize = true;
    this.labelDate.Dock = System.Windows.Forms.DockStyle.Right;
    this.labelDate.Location = new System.Drawing.Point(742, 0);
    this.labelDate.Name = "labelDate";
    this.labelDate.Size = new System.Drawing.Size(55, 24);
    this.labelDate.TabIndex = 0;
    this.labelDate.Text = "26-8-2019";
    // 
    // labelTime
    // 
    this.labelTime.AutoSize = true;
    this.labelTime.Dock = System.Windows.Forms.DockStyle.Right;
    this.labelTime.Location = new System.Drawing.Point(748, 24);
    this.labelTime.Name = "labelTime";
    this.labelTime.Size = new System.Drawing.Size(49, 24);
    this.labelTime.TabIndex = 1;
    this.labelTime.Text = "19:59:58";
    

    将您想要的任何其他内容添加到第三行。也许在停靠到“填充”的那一行添加一个面板

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2014-02-11
      相关资源
      最近更新 更多