C#为控件添加样式

时间:2023-01-17 13:48:27

I have a Panel and I am adding controls inside this panel. But there is a specific control that I would like to float. How would I go about doing that?

我有一个Panel,我在这个面板中添加控件。但是我想浮动一个特定的控件。我该怎么做呢?

pnlOverheadDetails is the panel name

pnlOverheadDetails是面板名称

pnlOverheadDetails.Controls.Add(lnkCalcOverhead);

The control named lnkCalcOverhead is the control I'd like to float.

名为lnkCalcOverhead的控件是我想要浮动的控件。

Thanks in advance

提前致谢

EDIT: By float I meant the css style not anything fancy :)

编辑:通过浮动我的意思是css风格没有任何花哨:)

2 个解决方案

#1


22  

If you have a CSS class defined for the control, you could do this before calling the Controls.Add method:

如果您为控件定义了CSS类,则可以在调用Controls.Add方法之前执行此操作:

lnkCalcOverhead.CssClass = "MyClass";

If you want to use the style attribute directly, try this:

如果要直接使用style属性,请尝试以下操作:

lnkCalcOverhead.Style.Add("float", "left");

#2


0  

IF you are talking about System.Windows.Forms here (and not WPF or ASP.NET):

如果你在这里谈论System.Windows.Forms(而不是WPF或ASP.NET):

When you are talking about float, do you mean you want to position it anywhere you want by code? If so, just set the .Location property of the control.

当你谈论浮动时,你的意思是你想通过代码将它放在任何你想要的地方吗?如果是这样,只需设置控件的.Location属性。

If you are talking about letting a control be moved around inside the panel by the user of your program, you will have to code that. That means capturing mouse events and moving the control accordingly?

如果您正在讨论让程序用户在控制面板内移动控件,则必须对其进行编码。这意味着捕获鼠标事件并相应地移动控件?

Alternatively you can instead of letting the control reside within the Panel, make it as a single control occupying a new form (hence you dont have to code all the mouse event handling). Just make sure that the window is limited to be moved within the boundaries of the "parent panel" (just check on the move event of the form if its within the boundariesm and force it to stay inside).

或者,您可以不将控件驻留在Panel中,而是将其作为占用新窗体的单个控件(因此您不必编写所有鼠标事件处理代码)。只需确保窗口仅限于在“父面板”的边界内移动(只需检查窗体的移动事件,如果它在边界内并强制它留在内部)。

#1


22  

If you have a CSS class defined for the control, you could do this before calling the Controls.Add method:

如果您为控件定义了CSS类,则可以在调用Controls.Add方法之前执行此操作:

lnkCalcOverhead.CssClass = "MyClass";

If you want to use the style attribute directly, try this:

如果要直接使用style属性,请尝试以下操作:

lnkCalcOverhead.Style.Add("float", "left");

#2


0  

IF you are talking about System.Windows.Forms here (and not WPF or ASP.NET):

如果你在这里谈论System.Windows.Forms(而不是WPF或ASP.NET):

When you are talking about float, do you mean you want to position it anywhere you want by code? If so, just set the .Location property of the control.

当你谈论浮动时,你的意思是你想通过代码将它放在任何你想要的地方吗?如果是这样,只需设置控件的.Location属性。

If you are talking about letting a control be moved around inside the panel by the user of your program, you will have to code that. That means capturing mouse events and moving the control accordingly?

如果您正在讨论让程序用户在控制面板内移动控件,则必须对其进行编码。这意味着捕获鼠标事件并相应地移动控件?

Alternatively you can instead of letting the control reside within the Panel, make it as a single control occupying a new form (hence you dont have to code all the mouse event handling). Just make sure that the window is limited to be moved within the boundaries of the "parent panel" (just check on the move event of the form if its within the boundariesm and force it to stay inside).

或者,您可以不将控件驻留在Panel中,而是将其作为占用新窗体的单个控件(因此您不必编写所有鼠标事件处理代码)。只需确保窗口仅限于在“父面板”的边界内移动(只需检查窗体的移动事件,如果它在边界内并强制它留在内部)。