KDM

KDM

  • NA
  • 1
  • 0

Generate buttons to modify an arraylist

Nov 26 2008 12:12 PM

Hello,

I'm a student, who has to make a c# application, i'm not going to describe it here, just the problem that i'm facing at the moment.
What I'm trying to do, is generate a list of buttons, where each button allows me to remove a specific element from an ArrayList. Problem is, with my current approach, it doesn't seem to work. Well, it works, but for some reason, i have to click a button twice in order for the program to work properly. I can't seem to find the problem... So I turn to you people, experienced programmers, in hopes someone can put me on the right trail :)

Here's my code:
[Code]

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

namespace programmerenproject

{

public partial class WebForm1 : System.Web.UI.Page

{

ArrayList list = new ArrayList();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

list.Add("test1");

list.Add("test2");

list.Add("test3");

list.Add("test4");

Session["list"] = list;

}

if (Session["list"] != null)

{

list = (ArrayList)Session["list"];

generateButtons();

}

String temp = "";

for (int x = 0; x < list.Count; x++)

{

temp = temp + list[x].ToString();

}

lblTest.Text = temp;

Session["list"] = list;

}

private void ClickMe(object sender, System.EventArgs e)

{

MyButton who = (MyButton)sender;

this.list.RemoveAt(who.Element);

}

public void generateButtons()

{

ArrayList list = (ArrayList)Session["list"];

int number = list.Count;

MyButton[] myButtons = new MyButton[number];

for (int i = 0; i < number; i++)

{

myButtons[i] = new MyButton(i);

myButtons[i].Text = list[i].ToString() + i.ToString();

myButtons[i].Click += new System.EventHandler(this.ClickMe);

form1.Controls.Add(myButtons[i]);

}

}

}

 

}


[/Code]


[Code]

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Collections;

namespace programmerenproject

{

public class MyButton:Button

{

int element;

public int Element

{

get { return element; }

set { element = value; }

}

public MyButton(int e)

{

element = e;

}

}

}


[/Code]