C# 给控件添加样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/290112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:51:28  来源:igfitidea点击:

C# Adding style to a control

c#asp.netcontrols

提问by Mike Fielden

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?

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

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样式不是什么花哨的:)

采纳答案by Jeromy Irvine

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

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

lnkCalcOverhead.CssClass = "MyClass";

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

如果你想直接使用 style 属性,试试这个:

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

回答by Wolf5

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).

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