Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

c#: delegati i poslanici prave probleme sa dogadjajima =)

[es] :: .NET :: .NET Desktop razvoj :: c#: delegati i poslanici prave probleme sa dogadjajima =)

[ Pregleda: 2417 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Smireni Magnet
softver inzenjer

Član broj: 9520
Poruke: 81
*.crnagora.net.



Profil

icon c#: delegati i poslanici prave probleme sa dogadjajima =)13.10.2005. u 10:25 - pre 226 meseci
Problem je sledeci:

Pokusavam da u potpunosti ovladam delegatima i event handlingom, pa sam u tu svrhu napravio c# web projekat koji se sastoji od jedne forme i jedne user kontrole.
User kontrola ima dva dugmeta za napred i nazad, sa jednom labelom izmedju koja sadrzi neki broj. Web forma ima labelu i gore spomenutu user kontrolu. Text labele treba da bude identican onom od labele u user kontroli, a pritiskom na dugme napred sadrzaj labele kontrole se inkrementira, a pritiskom na dugme nazad, sadrzaj labele kontrole se dekrementira. Ovo je jednostavno i bazicno.

Ono sto ja zelim je
1. da se pritiskom na dugme napred/nazad izmeni sadrzaj labele na web formi.
2. da ja sam formiram klasu koja bi sadrzala argumente koji bi se slali web formi. (u ovom slucaju to bi bio sadrzaj labele)
3. da user kontrola ima svoj namespace zbog mogucnosti koriscenja u nekim druygim projektima.


Ovo sam sve probao da uradim, ali konstanto dobijam gresku

Object reference not set to an instance of an object.

Source za user kontrolu glasi:
Code:
namespace mojaKontrola
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;


    public delegate void IndexChangedHandler(object kontrola, RecordIndexEventArgs myEA);

    /// <summary>
    ///        Summary description for RecordIndex.
    /// </summary>
    /// 

    public class RecordIndex : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.Label lbIndex;
        protected System.Web.UI.WebControls.Button Button2;

        private int _curIndex;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
        }


        #region properties

        public int Index 
        {
            set 
            {
                this._curIndex = value;
            }
            get 
            {
                return this._curIndex;
            }
        }

        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        ///        Required method for Designer support - do not modify
        ///        the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region Dugmad
        private void Button2_Click(object sender, System.EventArgs e)
        {
            this._curIndex = (System.Convert.ToInt32((this.lbIndex.Text))) + 1;
            this.SetCurrentCursor();
        }

        private void Button1_Click(object sender, System.EventArgs e)
        {
            this._curIndex = (System.Convert.ToInt32( this.lbIndex.Text)) - 1;
            this.SetCurrentCursor();
        }

        #endregion

        #region delegati i events
        
        public event IndexChangedHandler OnIndexChanged;
        #endregion

        private void SetCurrentCursor()
        {
            this.lbIndex.Text = this._curIndex.ToString();
            OnIndexChanged(this, new RecordIndexEventArgs(this._curIndex)); 
        }

    }

    #region Event argumenti za handlere ove klase
    public class RecordIndexEventArgs : System.EventArgs 
    {
        private int index;

        public RecordIndexEventArgs(int i) 
        {
            index = i;
        }

        public int Index 
        {
            set 
            {
                this.index = value;
            }
            get 
            {
                return index;
            }
        }
    }
    #endregion
}



Source za formu glasi:
Code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace delegat
{
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lbIme;
        protected mojaKontrola.RecordIndex ri;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            this.lbIme.Text = "0";
            // Put user code to initialize the page here
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        private void ri_OnIndexChanged(object kontrola, mojaKontrola.RecordIndexEventArgs myEA)
        {
                                       this.lbIme.Text = ((RecordIndex)kontrola).Index.ToString();
        }    
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);
            
            this.ri.OnIndexChanged += new mojaKontrola.IndexChangedHandler(this.ri_OnIndexChanged); 

        }
        #endregion
    }
}


Greska glasi Object reference not set to an instance of an object.
na liniju:
Code:
this.ri.OnIndexChanged += new mojaKontrola.IndexChangedHandler(this.ri_OnIndexChanged); 


koja se nalazi u kodu za webformu u f-ji InitializeComponent()...

Zasto ovo nece da radi?
PS: Greska se pojavljuje u run time-u, nakon uspesnog kompajliranja

To me boxing is like a ballet, except
there is no choreography, no dancing and the dancers hit each other...
 
Odgovor na temu

jablan

Član broj: 8286
Poruke: 4541



+711 Profil

icon Re: c#: delegati i poslanici prave probleme sa dogadjajima =)13.10.2005. u 10:50 - pre 226 meseci
Je l' ti na formi postavljena ta kontrola ri? Okači ceo projekat zipovan.

// hiljadita poruka!

[Ovu poruku je menjao jablan dana 13.10.2005. u 11:53 GMT+1]
 
Odgovor na temu

Smireni Magnet
softver inzenjer

Član broj: 9520
Poruke: 81
*.crnagora.net.



Profil

icon Re: c#: delegati i poslanici prave probleme sa dogadjajima =)13.10.2005. u 11:29 - pre 226 meseci
Evo projekta
To me boxing is like a ballet, except
there is no choreography, no dancing and the dancers hit each other...
Prikačeni fajlovi
 
Odgovor na temu

jablan

Član broj: 8286
Poruke: 4541



+711 Profil

icon Re: c#: delegati i poslanici prave probleme sa dogadjajima =)13.10.2005. u 11:50 - pre 226 meseci
Trebalo bi da odlučiš da li ti se kontrola zove ri ili rc.
 
Odgovor na temu

Smireni Magnet
softver inzenjer

Član broj: 9520
Poruke: 81
*.crnagora.net.



Profil

icon Re: c#: delegati i poslanici prave probleme sa dogadjajima =)13.10.2005. u 12:13 - pre 226 meseci
Vidiš, vidiš, hm... Ko bi rek'o?

ups...

Ispravio sam glupi typo i sada je sve kako treba. Štaviše, ovaj mali solution bi mogao sada da bude i mali tutorial kako formirati delegate i event handlere u ASP.NET-u, zar ne?

Jablane, puno ti hvala!

SM
To me boxing is like a ballet, except
there is no choreography, no dancing and the dancers hit each other...
 
Odgovor na temu

[es] :: .NET :: .NET Desktop razvoj :: c#: delegati i poslanici prave probleme sa dogadjajima =)

[ Pregleda: 2417 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.