有什么方法可以在C#中动态创建控件?
这首先是对象,但说控件会更准确.我的术语搞砸了.谢谢乔尔.
编辑{在运行时创建的控件.并且能够被程序访问和编辑.这有帮助吗?}
我喜欢动态创作的想法,并想知道有什么方法可以做到这一点.
每个答案只有一个,我想看看人们如何排名.
例如
private Label _lblCLastName = new Label();
private static List<ChildrenPanel> _ListCP = new List<ChildrenPanel>();
public void CreatePanel(Panel Container)
{
// Created Controls
#region Controls
_pnlStudent.Controls.Add(_lblCLastName);
//
// lblCLastName
//
_lblCLastName.AutoSize = true;
_lblCLastName.Location = new System.Drawing.Point(6, 32);
_lblCLastName.Name = "lblCLastName";
_lblCLastName.Size = new System.Drawing.Size(58, 13);
_lblCLastName.TabIndex = 10;
_lblCLastName.Text = "Last Name";
// Adds controls to selected forms panel
Container.Controls.Add(_pnlStudent);
// Creates a list of created panels inside the class
// So I can access …Run Code Online (Sandbox Code Playgroud) 我试图编写一个规范的例子,即'it'应该......"do"是在运行时确定的.我尝试将'it'方法放在我自己的方法中,以便我可以多次调用它:
def new_method(test)
it "#{test} should... " do
end
end
Run Code Online (Sandbox Code Playgroud)
但是,'it'方法不能从当前的Spec :: Example :: ExampleGroup :: Subclass实例中获得.
对于我的上一个项目,我在delphi应用程序中使用了很多帧,所以我决定创建dll并将它们放在dll中(所有在Delphi中创建)
我已经浏览了很多网站,并提出了有效的代码,但对于该示例,我必须使用运行时包编译这两个应用程序和dll,这意味着我还必须分发bpls.如果不检查构建与运行时包错误即将到来
这是我发现的代码
在exe中
procedure TForm1.Button1Click(Sender: TObject);
type
TGetTheFrame =Function( Owner: TComponent; TheParent: TWinControl ): TFrame; stdcall ;
var
GetTheFrame : TGetTheFrame;
begin
try
GetTheFrame(application,TabSheet1).Free ;
except
end;
frm := GetTheFrame(application,TabSheet1) ;
dllHandle := LoadLibrary('project1.dll') ;
if dllHandle <> 0 then
begin
GetTheFrame := GetProcAddress(dllHandle, 'GetTheFrame') ;
frm := GetTheFrame(application,TabSheet1) //call the function
{ ShowMessage('error function not found') ;
FreeLibrary(dllHandle) ; }
end
else
begin
ShowMessage('xxxx.dll not found / not loaded') ;
end
Run Code Online (Sandbox Code Playgroud)
在DLL中
Function GetTheFrame( Owner: TComponent; …Run Code Online (Sandbox Code Playgroud) 好的,我正在尝试在TScrollBox表面上创建一些自定义数量的TPanel,就像你可以在下面的图像上看到的那样.

为了得到这个,我使用以下代码,并且工作正常.
var
pan: array of TPanel;
maxp, i, x, y: Integer;
...
maxp := 10;
SetLength(pan, maxp);
for i := 1 to maxp do begin
// x is correct value; doesn't cause problem
// y is correct value; doesn't cause problem
pan[i-1] := TPanel.Create(form1);
with pan[i-1] do begin
Width := 100;
Height := 150;
Top := x * 151;
Left := y * 101;
Parent := ScrollBox1;
end;
end;
Run Code Online (Sandbox Code Playgroud)
现在,我有问题将TImage对象放在每个具有相同索引的TPanel中(img [0] - > pan [0],img [1] - > pan …
我想在运行时加载和创建程序集,有人告诉我使用命名空间System.Reflection.Assembly和System.Reflection.Emit.
我在msdn上找到的只有参考资料,但是当你不知道在哪里以及如何开始时它不适合使用它.我已经谷歌搜索,但我没有找到任何有用的教程/样本/参考.
有人可以向我解释功能或给我一些示例/教程吗?
是否可以通过在运行时编译的代码片段来提供接口的实现?
以下不起作用(安全类型转换返回"null"):
一个完整的控制台应用
using System;
using System.Collections.Generic;
namespace Foo
{
using System.CodeDom.Compiler;
using Microsoft.CSharp;
class Program
{
static void Main(string[] args)
{
// code snippet to compile:
string source =@"namespace Foo {
public class Test:ITest
{
public void Write()
{
System.Console.WriteLine(""Hello World"");
}
}
public interface ITest
{
void Write();
}
}
";//end snippet
// the "real" code:
Dictionary<string, string> providerOptions = new Dictionary<string, string>
{
{"CompilerVersion", "v4.0"}
};
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters compilerParams = new CompilerParameters
{ …Run Code Online (Sandbox Code Playgroud) 我设法做objcopy来查看和提取.rodata段内容作为不可变完整性检查的引用,但是,我意识到内核驱动程序无法读取文件.从这个意义上说,我们如何编写驱动程序来确定它在运行时的自身完整性?
一些示例代码来制作一些插图会很好.
我有一个程序,我需要在其中更新数据库表,并在编辑框中输入信息,最后用一个按钮进行更新.但是,表单是在运行时创建的,包括按钮在内的所有元素也以相同的方式创建.我想办法允许数据库参数定义更新数据库的过程,例如:
procedure UpdateDatabase(Field1,Field2,Field3:string);
begin
//update database here...
end;
Run Code Online (Sandbox Code Playgroud)
然后将我的按钮的OnClick事件分配给此过程,其参数预填充如下:
Button1.OnClick := UpdateDatabase(Edit1.text,Edit2.Text,Edit3.Text);
Run Code Online (Sandbox Code Playgroud)
但是,类型不兼容,因为它需要不同的数据类型.我还注意到参数通常不能传递给OnClick函数.实际上有没有办法实现我提出的建议?
这是我当前的创建按钮代码:
function buttonCreate(onClickEvent: TProcedure;
left: integer; top: integer; width: integer; height: integer;
anchors: TAnchors; caption: string; parent: TWinControl; form: TForm;): TButton;
var
theButton: TButton;
begin
theButton := TButton.Create(form);
theButton.width := width;
theButton.height := height;
theButton.left := left;
theButton.top := top;
theButton.parent := parent;
theButton.anchors := anchors;
//theButton.OnClick := onClickEvent;
theButton.Caption := caption;
result := theButton;
end;
Run Code Online (Sandbox Code Playgroud)
任何和所有帮助表示赞赏!
这是我的示例:http : //play.golang.org/p/D608cYqtO5
基本上我想这样做:
theType := reflect.TypeOf(anInterfaceValue)
theConvertedValue := anInterfaceValue.(theType)
Run Code Online (Sandbox Code Playgroud) 在python中,假设我们有以下列表:
my_list = ['a', 'b', 'c', 'd', ...]
会my_list.pop(3)比效率更高my_list.remove('d')吗?