tags:

views:

54

answers:

5

I have 14 command buttons on my form. what i want to do is change the text of the form based on the current date. button1 should have todays date. button2 should have tommorows date. button3 should have day after tomorrows date and so on. I want this for fourteen buttons.

I can do it manually by assigning each button.text to each date... i want to do it using a loop. is it possible. my buttons are named , button1,button2,button3,button4, and so on toll button 14. and the text i want on them is from the current date to 14 days later... basiocally want to display the dates on the button.. is it possible though a loop. m using visual studio and vb.net

+1  A: 
For Each b As Control In Me.Controls
    If TypeOf b Is Button Then
        Dim i As Integer = CInt(b.Name.Replace("Button", ""))
        If i <= 14 Then
            Dim d As DateTime = DateTime.Now.AddDays(i - 1)
            b.Text = d.ToString("dd MMM yyyy")
            d = d.AddDays(1)
        End If
    End If
Next

Place the code in the load event.

dbasnett
You don't need to check with <=14 as he only has 14 buttons. You're already checking if its a button. See my answer for more info.
JonH
@JonH I respectfully disagree for 2 reasons: 1. it's good practice to be specific and in case more buttons are added later this explicitly covers the named buttons intended. 2. If the answer didn't cover this I'm pretty sure someone else would've said "you need to limit it, just in case more buttons exist!" :)
Ahmad Mageed
Any app with 14 buttons with days on them seems silly enough, providing two checks makes it all the more complicated. Even if more buttons are added that extra if will NO longer work.
JonH
If he had said "I have only 14 command buttons on my form." I was simply being cautious, if the OP only has 14 buttons they can remove the check.
dbasnett
A: 

You can loop through the controls collection on your form, and if the control type is a button, you can code some rules around that.

Dave7896
+1  A: 

You could try something like

Dim dateVal As DateTime = DateTime.Today
For i As Integer = 1 To 14
    Dim but As Control = Controls("button" & i)
    but.Text = dateVal.ToString("dd MMM yyyy")
    dateVal = dateVal.AddDays(1)
Next
astander
You don't need to check with <=14 as he only has 14 buttons. You're already checking if its a button. See my answer for more info.
JonH
I aggree, but the OP only has the 14 buttons with Names known. *Why over complicate?*
astander
My policy when replying is not to ASSUME. The OP may or may not have more than 14 buttons and may or may not have controls that aren't buttons that start with Button.
dbasnett
A: 
Private Sub ApplyDateLabelsToButtons()

    Dim tmpDate As Date = DateTime.Today

    For i As Integer = 1 To 14
        Me.Controls("Button" & i.ToString()).Text = tmpDate.ToShortDateString()
        tmpDate = DateAdd(DateInterval.Day, 1, tmpDate)
    Next

End Sub
Tim Lentine
This won't be right as you are adding one day to the initial day. See my answer for more info.
JonH
@JonH: Are you sure about that?
Tim Lentine
The code for the date looks OK.
dbasnett
A: 

Try something like this:

I wrote it in C#:

private void Form1_Load(object sender, EventArgs e)
        {
            int v = 0;

            foreach(Control myBtn in this.Controls)
                if (myBtn is Button)
                {
                    myBtn.Text = DateTime.Today.AddDays(v).ToString();
                    v++;
                }
        }

Then was reminded you need it in VB.net, so here it is in vb.net:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) 
    Dim v As Integer = 0 

    For Each myBtn As Control In Me.Controls 
        If TypeOf myBtn Is Button Then 
            myBtn.Text = DateTime.Today.AddDays(v).ToString() 
            v += 1 
        End If 
    Next 
End Sub 
JonH
Tags says VB.net. You should try out http://www.developerfusion.com/tools/convert/csharp-to-vb/
astander
Thanks edited. (15 chars)
JonH
How does this solution guarantee that the order of dates matches the button "number" in the collection?
Tim Lentine
@tim - it doesn't, it might but it might not.
dbasnett