methode aanroepen

Status
Niet open voor verdere reacties.

savant11

Gebruiker
Lid geworden
7 jan 2008
Berichten
153
Hoi allen,

heb dit:

Code:
        public void addFrame(int idx, Frame _frame)
        {
            //frame[idx] = _frame;
            frame.Insert(idx, _frame);

        }

maar hoe spreek ik de methode nu aan in de form?

Code:
 private void panel1_Paint_1(object sender, PaintEventArgs e)
        {
            int idx;
            Frame _frame;
            game.charDef.addFrame(idx,_frame); //= AnimationBox.Select();
        }

Alvast bedankt.
 
Waarschijnlijk overheen gekeken:
Je moet eerst een instantie hebben aangemaakt.
Code:
Frame _frame = new Frame()
 
Hoi

En dan?

Ik geef wel de volledige klasse:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
//Classe for save and open files(This means, what u have made already)
namespace xCharEdit.Character
{
    public class CharDef
    {
       public List<Animation> animation;
      // Animation[] animation;
        //public Frame[] frame;
       public List<Frame> frame;
        public String path;

        public int headIdx;
        public int torsoIdx;
        public int legsIdx;
        public int weaponIdx;
        public int idx = 0;

        public CharDef()
        {
            animation  = new List<Animation>();
            frame = new List<Frame>();
            //for (int i = 0; i < animation.Length; i++)
                //animation[i] = new Animation();
            //frame = new Frame[512];
            //frame[30] = new Frame();
           // for (int i = 0; i < frame.Length; i++)
                //frame[i] = new Frame();
            Animation a = new Animation(100);
            animation.Add(a);
            path = "char";

            /*
            animation = new Animation[64];
            for (int i = 0; i < animation.Length; i++)
                animation[i] = new Animation();
            frame = new Frame[512];
            for (int i = 0; i < frame.Length; i++)
                frame[i] = new Frame();

            path = "char";
             */ 



        }//End method

        public Animation GetAnimation(int idx )
        {
            for (int i = 0;i < animation.Count;i++)
                //new CharDef();
                //new Animation();
                //animation.Add();
                return (idx >= 0) ? animation[idx]: null;
            return null;

            //Animation a = new Animation(100);
            //animation.Add(a);
                //MessageBox.Show("Selected Item Text: " + animation[idx].ToString() + "\n" +
                       // "Index: " + animation[idx].ToString());


            return null;

            if (idx == 0)
                MessageBox.Show("is null");

            //try
            //{
                // problem is null, and ProblemProperty cannot be called
                // on a null reference
                //int animation = getAnimation.ProblemProperty;
            //}
            //catch (NullReferenceException nre)
            //{
                //Console.WriteLine(
                    //"Cannot read ProblemProperty, problem is null.\n" +
                   // nre.Message);
           // }




        }

       

        public void SetAnimation(int idx, Animation _animation)
        {
            animation[idx] = _animation;
        }

        public List<Animation> GetAnimationArray()
        {
            return animation;
        }

        public Frame GetFrame(int idx)
        {
            //frame.Add(frame);

            return frame[idx];
        }

        
        public void addFrame(int idx, Frame _frame)
        {
            //frame[idx] = _frame;
            frame.Insert(idx, _frame);

        }

        public List<Frame>GetFrameArray()
        {
            return frame;
        }

        /*public void WriteBackup()
        {
            Write(@"data/" + path); //+ ".zmx");
            Write(@"../../../../CharacterEditor/Debug/char" + path);//  data/chars/" + path); //+ ".zmx");
        }

        public void Write()
        {
            Write(@"data/" + path); //+ ".zmx");
        }

        public void Write(String writePath)
        {
            BinaryWriter b = new BinaryWriter(File.Open(writePath,
                FileMode.Create));

            b.Write(path);
            b.Write(headIdx);
            b.Write(torsoIdx);
            b.Write(legsIdx);
            b.Write(weaponIdx);

            for (int i = 0; i < animation.Length; i++)
            {
                b.Write(animation[i].name);

                for (int j = 0; j < animation[i].getKeyFrameArray().Length; j++)
                {
                    KeyFrame keyframe = animation[i].GetKeyFrame(j);
                    b.Write(keyframe.frameRef);
                    b.Write(keyframe.duration);
                    String[] script = keyframe.getScriptArray();
                    for (int s = 0; s < script.Length; s++)
                        b.Write(script[s]);
                }
            }

            for (int i = 0; i < frame.Length; i++)
            {
                b.Write(frame[i].name);

                for (int j = 0; j < frame[i].GetPartArray().Count; j++)
                {
                    Part p = frame[i].GetPart(j);
                    b.Write(p.idx);
                    b.Write(p.location.X);
                    b.Write(p.location.Y);
                    b.Write(p.rotation);
                    b.Write(p.scaling.X);
                    b.Write(p.scaling.Y);
                    b.Write(p.flip);
                }
            }

            b.Close();

            Console.WriteLine("Saved.");
        }//End method Save

        public void Read()
        {
            BinaryReader b;
            try
            {
                b = new BinaryReader(File.Open(@"data/" + path + ".zmx",
                    FileMode.Open, FileAccess.Read));
            }
            catch (Exception e)
            {
                return;
            }

            path = b.ReadString();
            headIdx = b.ReadInt32();
            torsoIdx = b.ReadInt32();
            legsIdx = b.ReadInt32();
            weaponIdx = b.ReadInt32();
            

            for (int i = 0; i < animation.Length; i++)
            {   
                animation[i].name = b.ReadString();

                for (int j = 0; j < animation[i].getKeyFrameArray().Length; j++)
                {
                    KeyFrame keyframe = animation[i].GetKeyFrame(j);
                    keyframe.frameRef = b.ReadInt32();
                    keyframe.duration = b.ReadInt32();

                    String[] script = keyframe.getScriptArray();
                    for (int s = 0; s < script.Length; s++)
                        script[s] = b.ReadString();
                }
            }

            for (int i = 0; i < frame.Length; i++)
            {   
                frame[i].name = b.ReadString();

                for (int j = 0; j < frame[i].GetPartArray().Length; j++)
                {
                    Part p = frame[i].GetPart(j);
                    p.idx = b.ReadInt32();
                    p.location.X = b.ReadSingle();
                    p.location.Y = b.ReadSingle();
                    p.rotation = b.ReadSingle();
                    p.scaling.X = b.ReadSingle();
                    p.scaling.Y = b.ReadSingle();
                    p.flip = b.ReadInt32();
                }
            }

            b.Close();

            Console.WriteLine("Loaded.");
        }*/
    }
}

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharacterEditor;
using HelperClasses;
//using CharacterEditor;
using xCharEdit;
using xCharEdit.Character;

namespace CharacterEditor
{
    public partial class MainForm : Form
    {

        public Game1 game;
        CharacterEditor.Controller.AdminAnnimation_controller _controller;

        //Constructor
        public MainForm()
        {
            InitializeComponent();

            _controller = new CharacterEditor.Controller.AdminAnnimation_controller(this);

            AnimationBox.Items.Add(new Animation(AnimationBox.Items.Count));
        }

        xCharEdit.Model.AdminAnnimation temp = new xCharEdit.Model.AdminAnnimation();
        //public xCharEdit.Model.AdminAnnimation currentAdminAnnimation
        //{
        //get;
        // set;
        //}

        public string Naam
        {
            get { return textBox1.Text; }


        }

        public IntPtr getDrawSurface()
        {
            return panel1.Handle;
        }

        public void setGame(Game1 game)
        {
            this.game = game;
        }

        //FrameList
        private void SelectedPartChanged(object sender, EventArgs e)
        {
           /*
            if (FrameList.SelectedItem != null)
            {
                game.selFrame = FrameList.SelectedIndex;
            }
            

            if (FrameList.SelectedIndex >= 0)
            {
                int index = FrameList.SelectedIndex;
                game.selPart = index;
                partGrid.SelectedObject = game.charDef.frame[game.selFrame].partList[index];

                //game.FrameList.Name.Replace;

            }
            */ 
            

        }

        private void newCharDef(object sender, EventArgs e)
        {
            game.charDef = new xCharEdit.Character.CharDef();
        }

        private void saveCharDef(object sender, EventArgs e)
        {
            SaveFileDialog sd = new SaveFileDialog();
            if (sd.ShowDialog() == DialogResult.OK)
            {
                XMLFileManager<xCharEdit.Character.CharDef>.WriteToFile(sd.FileName, game.charDef);
            }
        }

        private void openCharDef(object sender, EventArgs e)
        {
            OpenFileDialog od = new OpenFileDialog();
            if (od.ShowDialog() == DialogResult.OK)
            {
                game.charDef = XMLFileManager<xCharEdit.Character.CharDef>.OpenFromFile(od.FileName);
            }
            //game.DrawPartList();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            game.Exit();
        }

        private void RemoveFrame(object sender, EventArgs e)
        {
            if (FrameList.SelectedItems.Count >= 1 && FrameList.SelectedIndex > -1)
            {
                int index = FrameList.SelectedIndex;
                FrameList.Items.RemoveAt(index);
                game.charDef.frame[game.selFrame].partList.RemoveAt(index);
            }
            if (AnimationBox.SelectedItem != null)
            {
                Animation a = ((Animation)AnimationBox.SelectedItem);
                a.KeyFrame.Remove((KeyFrame)FrameList.SelectedItem);
                FrameList.DataSource = null;
                FrameList.DataSource = ((Animation)AnimationBox.SelectedItem).KeyFrame;
            }
        }

        private void partGrid_Click(object sender, EventArgs e)
        {

        }

        //load the images
        private void ItemTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void importImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                OpenFileDialog open = new OpenFileDialog();

                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

                if (open.ShowDialog() == DialogResult.OK)
                {

                    //ItemTabContr.I = new Bitmap(open.FileName);

                }

            }

            catch (Exception)
            {

                throw new ApplicationException("Failed loading image");

            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {
            //listBox1.Items.Add(((TextBox)sender).Text);

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void addAnimation(object sender, EventArgs e)
        {
            //AnimationBox.Items.Add(new Animation(AnimationBox.Items.Count));

            Animation ani = new Animation(AnimationBox.Items.Count);
            AnimationBox.Items.Add(ani);

            //CharDef charDef = new CharDef();

            game.charDef.animation.Add(ani);


            //charDef.idx.GetAnimation.add(ani);// animation.Add(ani);


            
        }

        private void btVerwijden_Click(object sender, EventArgs e)
        {
            if (AnimationBox.SelectedItem != null)
                AnimationBox.Items.Remove(AnimationBox.SelectedItem);
/*            if (AnimationBox.SelectedIndex == -1)
            {
                MessageBox.Show("Please select an item first.", "No item selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                AnimationBox.Items.RemoveAt(AnimationBox.SelectedIndex);
            }*/

        }

        private void btWijzigen_Click(object sender, EventArgs e)
        {
            if(AnimationBox.SelectedIndex == 1)
            {
                //MessageBox.Show("U didnt select an item!!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);   
            }    
        
       // else
       // {
           // listBox1.Items.Insert(listBox1.SelectedItem);   
       // }
       }

        private void ChangeAnimation(object sender, EventArgs e)
        {
            if (AnimationBox.SelectedItem != null)//|| (AnimationBox.SelectedIndex <= null))
            {
                game.selAnim = AnimationBox.SelectedIndex;
                FrameList.DataSource = ((Animation)AnimationBox.SelectedItem).KeyFrame;

                MessageBox.Show("Selected Item Text: " + AnimationBox.SelectedItem.ToString() + "\n" +
                    "Index: " + AnimationBox.SelectedIndex.ToString());
            }
            


        }

        private void AddFrame(object sender, EventArgs e)
        {
            ((Animation)AnimationBox.SelectedItem).KeyFrame.Add(new KeyFrame(FrameList.Items.Count));
            FrameList.DataSource = null;
            FrameList.DataSource = ((Animation)AnimationBox.SelectedItem).KeyFrame;
        }

        private void listbSpeedAnnimatie_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listbSpeedAnnimatie.SelectedItem != null)
            {
                game.selAnim = listbSpeedAnnimatie.SelectedIndex;
                FrameList.DataSource = ((Animation)listbSpeedAnnimatie.SelectedItem).KeyFrame;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
           listbSpeedAnnimatie.Items.Add(new Animation(listbSpeedAnnimatie.Items.Count));
        }

        private void panel1_Paint_1(object sender, PaintEventArgs e)
        {
            
            game.charDef.addFrame = ItemTabControl.SelectedIndex();
            //Frame _frame = new Frame(_frame);
            CharDef charDef = new CharDef(); 
            game.charDef.addFrame(idx = selIndex );
        }
    }
}
 
Om de methode vanuit de scope van je eigen form aan te roepen hoef je niet eerst een instantie aan te maken.

Roep hem gewoon zo aan:
panel1_Paint_1(null,null);
 
Hoi Marceljo,

wat is je mail? Dan stuur ik je het project oke.

Niels
 
maar bedoel je zoiets:

Code:
 public List<KeyFrame> KeyFrame
        {
          
            panel1_Paint_1(null, null); 
            get { return keyFrame; }            
        }
 
Ik heb je mail ontvangen, maar helaas heb ik niet de mogelijkheid om je hele project door te spitten. Wel weet ik dat je het paint-event niet aan HOEFT te roepen, het wordt door het framework uit zichzelf al aangeroepen.

De code van jou
public List<KeyFrame> KeyFrame
{

panel1_Paint_1(null, null);
get { return keyFrame; }
}
zal ook werken, maar dan wordt dus de paint elke keer aangeroepen als je het KeyFrame opvraagt. Netter is het dan om een Invalidate() aan te roepen op het panel.

panel1.Invalidate();

(hij zal dan uit zichzelf de panel1_Paint_1 weer aanroepen.
 
Status
Niet open voor verdere reacties.

Nieuwste berichten

Terug
Bovenaan Onderaan