gpt4 book ai didi

c# - 带控件的基本 UserControl - 派生控件中的对齐不正确

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:53 24 4
gpt4 key购买 nike

我正在尝试构建将用于构建其他控件的基础控件。
想法是向基础添加通用功能。

我在 Visual Studio 中使用 Add>User Control 构建了基础。我的控件在右上角有 4 个按钮,它们都固定在右上角:

enter image description here

下面是我的控制代码:

using System;
using System.Windows.Forms;

namespace LayoutManager
{
public partial class BasePanel : UserControl
{
public event EventHandler<Direction> Expand;

public BasePanel()
{
InitializeComponent();
}

protected virtual void OnExpand(Direction e)
{
var handler = Expand;
if (handler != null) handler(this, e);
}

private void Right(object sender, EventArgs e)
{
OnExpand(Direction.Right);
}

private void Left(object sender, EventArgs e)
{
OnExpand(Direction.Left);
}

private void Up(object sender, EventArgs e)
{
OnExpand(Direction.Up);
}

private void Down(object sender, EventArgs e)
{
OnExpand(Direction.Down);
}
}
}

和设计器代码:

namespace LayoutManager
{
partial class BasePanel
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.rightBtn = new System.Windows.Forms.Button();
this.leftBtn = new System.Windows.Forms.Button();
this.upBtn = new System.Windows.Forms.Button();
this.downBtn = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// rightBtn
//
this.rightBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.rightBtn.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.rightBtn.Location = new System.Drawing.Point(185, 27);
this.rightBtn.Name = "rightBtn";
this.rightBtn.Size = new System.Drawing.Size(23, 23);
this.rightBtn.TabIndex = 0;
this.rightBtn.Text = "►";
this.rightBtn.UseVisualStyleBackColor = true;
this.rightBtn.Click += new System.EventHandler(this.Right);
//
// leftBtn
//
this.leftBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.leftBtn.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.leftBtn.Location = new System.Drawing.Point(160, 27);
this.leftBtn.Name = "leftBtn";
this.leftBtn.Size = new System.Drawing.Size(23, 23);
this.leftBtn.TabIndex = 1;
this.leftBtn.Text = "◄";
this.leftBtn.UseVisualStyleBackColor = true;
this.leftBtn.Click += new System.EventHandler(this.Left);
//
// upBtn
//
this.upBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.upBtn.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.upBtn.Location = new System.Drawing.Point(160, 3);
this.upBtn.Name = "upBtn";
this.upBtn.Size = new System.Drawing.Size(23, 23);
this.upBtn.TabIndex = 3;
this.upBtn.Text = "▲";
this.upBtn.UseVisualStyleBackColor = true;
this.upBtn.Click += new System.EventHandler(this.Up);
//
// downBtn
//
this.downBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.downBtn.Font = new System.Drawing.Font("Arial Narrow", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.downBtn.Location = new System.Drawing.Point(185, 3);
this.downBtn.Name = "downBtn";
this.downBtn.Size = new System.Drawing.Size(23, 23);
this.downBtn.TabIndex = 4;
this.downBtn.Text = "▼";
this.downBtn.UseVisualStyleBackColor = true;
this.downBtn.Click += new System.EventHandler(this.Down);
//
// BasePanel
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.downBtn);
this.Controls.Add(this.upBtn);
this.Controls.Add(this.leftBtn);
this.Controls.Add(this.rightBtn);
this.Name = "BasePanel";
this.Size = new System.Drawing.Size(211, 140);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button rightBtn;
private System.Windows.Forms.Button leftBtn;
private System.Windows.Forms.Button upBtn;
private System.Windows.Forms.Button downBtn;
}
}

当我添加第二个控件并将其设置为扩展 BasePanel 时:

public partial class MyPanel : BasePanel
{
public MyPanel()
{
InitializeComponent();
}
}

我得到这个控件:

enter image description here

但是底部右上角的按钮不再正确对齐。
我无法更改按钮属性,因为它们被锁定(点击后我可以看到左上角的小锁角)

我应该如何创建具有其他控件(在我的例子中是按钮)的基本 UserControl,以及我应该如何扩展它以使所有控件在派生控件中正确对齐?

编辑:我使用 VS2013 Pro Update 4

最佳答案

您可以使用 Panel 作为 BasePanel 中按钮的容器。然后将面板的 Dock 属性设置为 Top 并将您的 Button 控件放在面板中并将其 Anchor 属性设置为顶部,右侧

这样,当您从BasePanel 继承时,包括容器面板和按钮在内的所有控件都是私有(private)的并且不能被编辑,但是设计者尊重控件的定位。

enter image description here

你也可以移除面板和按钮的设计器,这样它们对设计器处的鼠标是透明的:

public BasePanel()
{
InitializeComponent();
TypeDescriptor.AddAttributes(this.panel1, new DesignerAttribute(typeof(object)));
TypeDescriptor.AddAttributes(this.button1, new DesignerAttribute(typeof(object)));
TypeDescriptor.AddAttributes(this.button2, new DesignerAttribute(typeof(object)));
TypeDescriptor.AddAttributes(this.button3, new DesignerAttribute(typeof(object)));
TypeDescriptor.AddAttributes(this.button4, new DesignerAttribute(typeof(object)));
}

enter image description here

关于c# - 带控件的基本 UserControl - 派生控件中的对齐不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com