Set values for buttonarray.

  • Thread starter Thread starter MA
  • Start date Start date
M

MA

Hi!

I have this code in a class called activity:

public Button[] getActivity(DateTime startDate, DateTime endDate)
{
TimeSpan ts = endDate-startDate;
int arraySize = ts.Days;
Button[] btnActivity = new Button[arraySize];
return btnActivity;
}

that I call with this:

activity objActivity = new activity();
Button[] btnActivity = objActivity.getActivity(DateTime.Today,
DateTime.Today.AddDays(6));


Is there a way to set for example onClick events, css, button text on each
button in the array before I call it (in getActivity)?

/Marre
 
MA said:
I have this code in a class called activity:

public Button[] getActivity(DateTime startDate, DateTime endDate)
{
TimeSpan ts = endDate-startDate;
int arraySize = ts.Days;
Button[] btnActivity = new Button[arraySize];
return btnActivity;
}

that I call with this:

activity objActivity = new activity();
Button[] btnActivity = objActivity.getActivity(DateTime.Today,
DateTime.Today.AddDays(6));


Is there a way to set for example onClick events, css, button text on each
button in the array before I call it (in getActivity)?

There would be if you were actually creating any buttons, but you're
not - just you're just creating an empty array.
 
Jon Skeet said:
MA said:
I have this code in a class called activity:

public Button[] getActivity(DateTime startDate, DateTime endDate)
{
TimeSpan ts = endDate-startDate;
int arraySize = ts.Days;
Button[] btnActivity = new Button[arraySize];
return btnActivity;
}

that I call with this:

activity objActivity = new activity();
Button[] btnActivity = objActivity.getActivity(DateTime.Today,
DateTime.Today.AddDays(6));


Is there a way to set for example onClick events, css, button text on each
button in the array before I call it (in getActivity)?

There would be if you were actually creating any buttons, but you're
not - just you're just creating an empty array.

Yes. But can I run thru this array with a loop (for), set all values and
send the array after that?

/Marre
 
Hi Marre,

It is a little unclear what you are doing.
For one Button[] btnActivity = new Button[arraySize] will be filled with null references and not really buttons.
You need to initialize new buttons with

for(int i = 0; i < btnActivity.Length; i++)
{
btnActivity = new Button();
}

Then you can loop through the buttons a change the values in a foreach (or set them in the above loop)

foreach(Button b in btnActivity)
{
b.Text = "Click Me";
b.Click += new EventHandler(ActivityButton_Click);
}


Happy coding!
Morten Wennevik [C# MVP]
 
MA said:
Yes. But can I run thru this array with a loop (for), set all values and
send the array after that?

If you run through the array and create the buttons, you can then add
the event handlers to those buttons, yes.
 
Jon Skeet said:
If you run through the array and create the buttons, you can then add
the event handlers to those buttons, yes.

Can I do like this:

public Button[] getActivity(DateTime startDate, DateTime endDate)
{
TimeSpan ts = endDate-startDate;
int arraySize = ts.Days;
Button[] btnActivity = new Button[arraySize];

for(int i=0; i<arraySize; i++)
{
//set all that I want here
}

//and then return the array with all this set
return btnActivity;
}


Ahh....I just try it out :)

Thanx
/Marre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top