C#从静态类向非静态表单添加控件

时间:2016-10-13 14:16:25

标签: c# xml controls

在C#.NET中,我试图将控件从静态类添加到非静态类。

这是我的代码:

    public static void AddMediaToPanel(string Title, string Description, string Source, string Cover, string Genre, int Rating)
    {
        PictureBox MediaCanvas = new PictureBox();
        MediaCanvas.BackColor = Color.LightGray;
        MediaCanvas.BorderStyle = BorderStyle.FixedSingle;
        MediaCanvas.Size = new Size(150, 235);
        MediaCanvas.Padding = new Padding(10);

        try
        {
            MediaCanvas.ImageLocation = Source;
        }
        catch { }

        var gui = new GUI();
        gui.Controls.Add(MediaCanvas);
    }

GUI方法代表名为“GUI'”的Windows窗体。 我试图建立一个允许你添加电影的开源媒体中心 到存储库。

静态void AddMediaToPanel必须是静态的,因为这个类:

    public static void RetrieveMedia(string XMLFile)
    {
        // Declare the Media Collections
        MediaCollection media = null;

        // Declare the XML-readers
        XmlSerializer serializer = new XmlSerializer(typeof(MediaCollection));
        StreamReader sr = new StreamReader(XMLFile);
        try
        {
            media = (MediaCollection)serializer.Deserialize(sr);
        }
        catch(Exception ex)
        {
            MessageBox.Show("The following media repository could not be loaded:\n" +
                            XMLFile + "\n" +
                            "Please check your code and try again later.\n\n" +
                            "Error Information: " + ex.Message, "Repository error",
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        // Create new instance of the media class
        sr.Close();

        // Return properties
        try
        {
            foreach (Video video in media.Video)
            {
                GUI.AddMediaToPanel(video.Title, video.Description, video.Source, video.Cover, video.Genre, video.Rating);
            }
        }
        catch { }
    }

我使用的是.NET Framework 4.5.2。 提前谢谢!

~Kees van V。

2 个答案:

答案 0 :(得分:0)

这不能以这种方式工作。在你的循环中,你正在调用静态方法:

foreach (Video video in media.Video)
{
    GUI.AddMediaToPanel(video.Title, video.Description, video.Source, video.Cover, video.Genre, video.Rating);
}

但是你的静态方法每次都会创建一个新的Form,为它添加一个控件,然后扔掉它:

var gui = new GUI();
gui.Controls.Add(MediaCanvas);

您需要创建一次表单(在静态方法之外),然后让静态方法返回一个可以添加到单个Form实例的控件。

循环的实例方法可能如下所示:

var gui = new GUI();
foreach (Video video in media.Video)
{
     var control = GUI.AddMediaToPanel(video.Title, video.Description, video.Source, video.Cover, video.Genre, video.Rating);
     gui.Controls.Add(control);
}

答案 1 :(得分:0)

在您当前的代码中,您只需创建并丢弃.model small .stack 100h .data n1 dw 3 n2 dw 95 count dw ? msg db 'Quotient = $' str db 10 dup('$') .code mov ax, @data mov ds, ax ;DIVIDE BY SUBTRACTIONS. mov count, 0 ;COUNTER FOR SUBTRACTIONS. subtract: ;CHECK IF SUBTRACTION CAN BE DONE. mov ax, n1 ;COPY N1 INTO AX BECAUSE CAN... cmp n2, ax ;...NOT COMPARE MEMORY-MEMORY. jl finale ;N2 < N1. NO MORE SUBTRACTIONS. ;SUBTRACT. sub n2, ax ;N2-N1. inc count ;INCREASE SUBTRACTIONS COUNTER. jmp subtract ;REPEAT. finale: ;DISPLAY MESSAGE. mov ah, 9 mov dx, offset msg int 21h ;CONVERT QUOTIENT (COUNT) INTO STRING. mov ax, count mov si, offset str call number2string ;DISPLAY COUNT (QUOTIENT). mov ah, 9 mov dx, offset str int 21h ;WAIT FOR A KEY TO BE PRESSED. mov ah, 0 int 16h ;EXIT. mov ax, 4c00h int 21h ;------------------------------------------ ;CONVERT A NUMBER IN STRING. ;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE ;THEM IN STACK, THEN EXTRACT THEM IN REVERSE ;ORDER TO CONSTRUCT STRING (STR). ;PARAMETERS : AX = NUMBER TO CONVERT. ; SI = POINTING WHERE TO STORE STRING. number2string proc mov bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10. mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS. cycle1: mov dx, 0 ;NECESSARY TO DIVIDE BY BX. div bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER. push dx ;PRESERVE DIGIT EXTRACTED FOR LATER. inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED. cmp ax, 0 ;IF NUMBER IS jne cycle1 ;NOT ZERO, LOOP. ;NOW RETRIEVE PUSHED DIGITS. cycle2: pop dx add dl, 48 ;CONVERT DIGIT TO CHARACTER. mov [ si ], dl inc si loop cycle2 ret number2string endp 表单:

gui

您必须找到您希望将控件添加到的表单实例:

   var gui = new GUI();
   gui.Controls.Add(MediaCanvas);