从C#中的另一个类调用一个函数

时间:2012-10-18 07:20:19

标签: c# draw shape

我正在处理一个新项目。我有一个类来绘制矩形到窗体。我想将这个类嵌入另一个类。代码如下;

主代码将调用形状代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using sekilciz_uygulama;



namespace xml_test_v1

{
class Program
{
    static void Main(string[] args)
    {
        XmlDocument xDoc = new XmlDocument();

        xDoc.Load("c:\\sw_xml_test_4.xml");
        int rad=0;
        string giris_text = Console.ReadLine().ToString();

        Console.WriteLine(giris_text);
            foreach(XmlNode node in xDoc.SelectNodes("network/switch"))



            {

                string ip_adress =      node.SelectSingleNode("ip_adress").InnerText.ToString();

                Console.WriteLine(ip_adress);

                if (ip_adress.Contains(giris_text))

                {
                   // call for shape code!!!
                }


                }}}}

创建形状的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace sekilciz_uygulama
{
public class Sekilciz
{

    public Rectangle[] skare;

    private SolidBrush firca;
    private int x,y, genislik, yukseklik;

    public Sekilciz()

    {
      skare = new Rectangle[5];
        firca = new SolidBrush(Color.Blue);

        x = 500;
        y = 200;
        genislik= 100;

        yukseklik =100;


        for(int i=0; i< skare.Length;i++)

        {
             skare[i] = new Rectangle(x,y,genislik,yukseklik);

            x-=150;
        }
    }

    public void kareciz(Graphics duzlem)

    {
        foreach(Rectangle rec in skare)

        {
            duzlem.FillRectangle(firca,rec);
        }
    }
    }

}

2 个答案:

答案 0 :(得分:0)

// "this" is  your windows form, or control like button
var myGraphic = this.CreateGraphics();
var sekilciz = new Sekilciz(myGraphic);
sekilciz.kareciz();

但是你的构造函数中有太多的处理过程。最好将该代码移到同一个类中的其他方法中。

在这里查看完成窗体上绘图的示例: 的 The Basics of Drawing Graphics onto Windows Forms

答案 1 :(得分:0)

var sekilciz = new Sekilciz();
sekilciz.kareciz(null);

您需要将参数传递给方法kareciz