» »

[c#] delegat, event handler

[c#] delegat, event handler

DuleKrtola ::

Živ,
ne obvladam najbol teh delegatov, spodaj je koda. Sem probal pobrisat čimveč kode, ki je irelavantna, pa je vseeno 90 vrstic.
using System;
using System.Windows.Forms;
...
using WiimoteLib;

namespace joyPoll
{
    public partial class Form1 : Form
    {
        public delegate void UpdateWiimoteStateDelegate(WiimoteChangedEventArgs args);
        Dictionary<Guid, Form1> mWiimoteMap = new Dictionary<Guid, Form1>();
        private Wiimote mWiimote;
        
        WiimoteCollection mWC;
        
        public Form1()
        {
            InitializeComponent();         
        }

        public Form1(Wiimote wm) : this()
        {
            mWiimote = wm;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (Wiimote wm in mWC)
                wm.Disconnect();
        }

        public void UpdateState(WiimoteChangedEventArgs args)
        {
	    //BeginInvoke(new UpdateWiimoteStateDelegate(UpdateWiimoteChanged), args);
            if (IsHandleCreated)
            {
		//BeginInvoke(new UpdateWiimoteStateDelegate(UpdateWiimoteChanged), args);
	    }
            else
            {
                CreateHandle();
                //BeginInvoke(new UpdateWiimoteStateDelegate(UpdateWiimoteChanged), args);
            }
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {

            mWC = new WiimoteCollection();
                        
            try
            {
                mWC.FindAllWiimotes();
            }
            
            foreach (Wiimote wm in mWC)
            {
                Form1 wi = new Form1();
                mWiimoteMap[wm.ID] = wi;
                // connect it and set it up as always
                wm.WiimoteChanged += wm_WiimoteChanged;
                                
                wm.Connect();
                if (wm.WiimoteState.ExtensionType != ExtensionType.BalanceBoard)
                    wm.SetReportType(InputReport.IRExtensionAccel, IRSensitivity.Maximum, true);
                wm.SetLEDs(true, true, false, true);
                break;
            }
        }
      
        void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs e)
        {     
            Form1 wi = mWiimoteMap[((Wiimote)sender).ID];
               wi.UpdateState(e);
        }
       
        private void UpdateWiimoteChanged(WiimoteChangedEventArgs args)
        {
            WiimoteState ws = args.WiimoteState;
            lblAccel.Text = ws.AccelState.Values.ToString(); 
			// tukaj se updatejo labele in checkboxi v formu
        }
     
        public Wiimote Wiimote
        {
            set { mWiimote = value; }
        }
    }
}


Če odkomentiram 34. vrstico ter zakomentiram IF/ELSE, dobim runtime error v omenjeni vrstici:
System.InvalidOperationException was unhandled
Message="Invoke or BeginInvoke cannot be called on a control until the window handle has been created."
Source="System.Windows.Forms"

Če pustim tako kot je (zakomentirane vse 3), mi CreateHandle() povzroči, da se program usuje.

Bi kdo vrgel uč na tole kodo, pls?

Mitja Bonča ::

Delegati potrebujejo eno kontrolo za metodo BeginUpdate (ali katero drugo):
Tebi manjka:
Control.BeginUpdate(new UpdateWiimoteStateDelegate(UpdateWiimoteChanged), args);

poglej si čisto simple primer:
public delegate void InvokeDelegate();

private void Invoke_Click(object sender, EventArgs e)
{
   myTextBox.BeginInvoke(new InvokeDelegate(InvokeMethod));
}
public void InvokeMethod()
{
   myTextBox.Text = "Executed the given delegate";
}


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

c# form

Oddelek: Programiranje
131884 (1398) FuI2cY
»

[c#] Risanje z miško

Oddelek: Programiranje
221440 (1121) japol
»

[c#] event, klik tipke escape

Oddelek: Programiranje
7761 (665) japol
»

[c#] Vprasanje glede BackGroundWorker classa in spreminanja gui elementa

Oddelek: Programiranje
6795 (721) Ericssony
»

[VB] Komunikacija s serijskimi napravami

Oddelek: Programiranje
412318 (1592) mNeRo

Več podobnih tem