在运行时创建对象 [英] creating object at runtime

查看:83
本文介绍了在运行时创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要创建一种网格(具有表,线形或其他形状),该网格中的行数和列数根据用户输入而变化.
然后,我需要在网格线上方创建并移动几个图片框(也根据用户需求创建).

我尝试使用桌子和上方的图片框,但无法使用,因为我无法放置图片框并将其移至所需的方向.另外,我不能在同一单元格中放置2个图片框.

*网格仅用作图片框移动的一种途径.

你能帮我找到其他方法吗?

thanx

我使用了TalbeLayoutPanel-无论如何,正如我写的那样-我不在乎要使用哪个对象,因为它仅用作背景.
图片框必须能够在表单中移动,所以我不能使用面板,因为表格中的面板是在每个单元格中单独使用的,所以我将无法创建移动..

Hi

I need to create a sort of grid (with table, line shapes or whatever) that the number of rows and columns in it changes according to user input.
Then, I need to create and move several picture box above the grid lines (also created according to user demands).

I tried to use table and above picture box but its not possible because I cant put the picture box and move them to which direction I want. also, I cant put 2 picture box at the same cell.

* the grid only serve as a kind of route for the picture boxes to move in.

can you help me find other way to do that?

thanx

I used TalbeLayoutPanel - anyway as I wrote - I don''t care which object to use becuse it only serves as a background.
the picturebox need to be able to move in the form so I can''t use panel because the panel in a table is used in each cell separately so I won''t be able to create a movement..

推荐答案

通常可以通过将一个单元中的两个PictureBox放在一个Panel上并将其放入目标单元中来实现.

对于其他几点,请参阅我的评论并详细说明您的问题.

[更新]
您只需定义目标列和行即可在TableLayoutPanel中移动图片框.但是,对于一个单元格中的其中几个,您将必须实现一些逻辑,以将其放置在该单元格中的共享面板上.

由于无论如何都必须实现自定义逻辑,因此您也可以扔掉整个桌子上的东西,从System.Windows.Forms.Control派生自定义控件,覆盖其OnPaint()方法,并在需要时绘制每张图片.

这样可以使您在图片放置时更加自由,并能够以动画般的方式移动图片.
[/更新]

[Update2]
在项目资源管理器中,右键单击您的项目,然后选择新建类".例如,将其命名为"PictureBoard.cs".
Visual Studio将为您创建代码文件并将其与您的解决方案集成.

更改
Two PictureBoxes in one cell usually can achieved by putting them on one Panel and putting that into the targeted cell.

For the other points, please see my comment and elaborate on your question.

[Update]
You can move pictureboxes around in a TableLayoutPanel just by defining the target column and row. For several of them in one cell, though, you will have to implement some logic to place them on a shared panel within the cell.

Since you have to implement custom logic anyway, you can also throw the whole talbe stuff away, derive a custom control from System.Windows.Forms.Control, override its OnPaint() method and draw every picture wherever you need it at the moment.

This gives you more freedom in picture placement and the ability to move pictures in an animation-ish way.
[/Update]

[Update2]
In Project explorer, you right-click on your project and select "New Class". Call it something meaningful, "PictureBoard.cs", for example.
Visual Studio will create the code file for you and integrate it with your solution.

Change
class PictureBoard


public class PictureBoard : System.Windows.Forms.Control


和贴士:您只是从所有控件的基类Control中派生了自定义的新PictureBoard类.

现在创建OnPaint()重载.开始输入
覆盖"加上一个空格键,Intellisense应该列出您可能要覆盖的方法列表.选择OnPaint().
Visual Studio将为您创建一个标准的OnPaint()替代:


and voilà: you just derived your custom new PictureBoard class from all control''s base class: Control.

Now create your OnPaint() overload. Start typing
"override" plus a spacebar hit and Intellisense should come up with a list of methods you could possibly want to override. Choose OnPaint().
Visual Studio will create a standard OnPaint() override for you:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
}


您可以保留base.OnPaint(e);到位.将自定义绘画代码放在其后.
自定义绘画的关键是PaintEventArgs e参数.它包含一个名为 e.Graphics System.Drawing.Graphics实例.这用于绘制所需的任何内容.在 MSDN [


You can leave base.OnPaint(e); in place. Put your custom painting code after it.
The key to custom painting is the PaintEventArgs e parameter. It holds, among others, an instance of System.Drawing.Graphics called e.Graphics. This is used to draw whatever you need to. Look it up on MSDN[^]

First, you will want to use one of the overloads of DrawRectangle(), DrawLine() and DrawImage().

Build your OnPaint() method in a way that it can be called at any moment since it is unpredictable when that will be the case.
You need to access your images along with positioning data from OnPaint(). Check all data for availability before using it.

Finally, whenever you want your control to update itself, call Invalidate() on it. It''s a method inherited from Control that will tell the OS that the control needs to be re-drawn. Whenever Windows finds it suitable, it will then call OnPaint().
[/Update2]


这可能是 ListView [ ^ ]满足您的需求?
Could this maybe ListView[^] serve your needs?


这篇关于在运行时创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆