Results 1 to 5 of 5

Thread: Several buttons doing the same actions on different controls

  1. #1
    Join Date
    Jun 2009
    Posts
    404

    Several buttons doing the same actions on different controls

    I am a beginner in programming and I would like you to help me to perform an action certainly basic. I created a sort of calendar with buttons to delete the appointment. Click on the button 9h delete rdv of 9h, 10h deletes rdv 10h and so on. The code is super heavy whether to put each time on each button.

    Example:
    Code:
     Private Sub btnSuppr9h_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuppr9h.Click
            btnrdv1.Text = "none rdv"
            lblProblem1.Text = ""
            Panel9h.BackColor = Color.ForestGreen
            btnSuppr9h.Enabled = False
        End Sub
     
        Private Sub btnSuppr10h_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuppr10h.Click
            btnrdv2.Text = "none rdv"
            lblProblem2.Text = ""
            Panel10h.BackColor = Color.ForestGreen
            btnSuppr10h.Enabled = False
        End Sub
     
        Private Sub btnSuppr11h_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuppr11h.Click
            btnrdv3.Text = "none rdv"
            lblProblem3.Text = ""
            Panel11h.BackColor = Color.ForestGreen
            btnSuppr11h.Enabled = False
        End Sub
    Is there a way to make such a function that puts a number (1, 2, 3, 4, ..., 10) after the name of a control? (btnrdv1.text, btnrdv2.text, btnrdv10.text). I searched a lot but I have not found a solution corresponding to my case, even in the MSDN.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Several buttons doing the same actions on different controls

    Here is an example on a button
    Code:
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_1.Click, Button_1.Click, Button_2.Click, Button_3.Click, .......
    You must then retrieve/use sender who will have to click and action will be taken on your other objects.

  3. #3
    Join Date
    Jun 2009
    Posts
    404

    Re: Several buttons doing the same actions on different controls

    Thank you very much, it taught me to use the sender. It gives me this:
    Code:
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuppr9h.Click, btnSuppr10h.Click, btnSuppr11h.Click
     
            If DirectCast(sender, Button) Is btnSuppr9h Then
                btnrdv1.Text = "none rdv"
                lblProblem1.Text = ""
                Panel9h.BackColor = Color.ForestGreen
                btnSuppr9h.Enabled = False
            ElseIf DirectCast(sender, Button) Is btnSuppr10h Then
                btnrdv2.Text = "none rdv"
                lblProblem2.Text = ""
                Panel10h.BackColor = Color.ForestGreen
                btnSuppr10h.Enabled = False
            ElseIf DirectCast(sender, Button) Is btnSuppr11h Then
                btnrdv3.Text = "none rdv"
                lblProblem3.Text = ""
                Panel11h.BackColor = Color.ForestGreen
                btnSuppr11h.Enabled = False
            End If
    There is not a means to further reduce the code?

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Several buttons doing the same actions on different controls

    Avoid using If...Then...Else Statement. The Select...Case Statement might be more useful when evaluating a single expression that has several possible values. You can use multiple expressions or ranges in each Case clause.

  5. #5
    Join Date
    Jun 2009
    Posts
    404

    Re: Several buttons doing the same actions on different controls

    Thank you very much, it reduces the code and makes it more readable.
    Code:
    Select Case True
                Case sender Is btnSuppr9h
                    btnrdv1.Text = "none rdv"
                    lblProblem1.Text = ""
                    Panel9h.BackColor = Color.ForestGreen
                    btnSuppr9h.Enabled = False
                Case sender Is btnSuppr10h
                    btnrdv2.Text = "none rdv"
                    lblProblem2.Text = ""
                    Panel10h.BackColor = Color.ForestGreen
                    btnSuppr10h.Enabled = False
                Case sender Is btnSuppr11h
                    btnrdv3.Text = "none rdv"
                    lblProblem3.Text = ""
                    Panel11h.BackColor = Color.ForestGreen
                    btnSuppr11h.Enabled = False
    End Select
    Is there still something to be done to improve a little?

Similar Threads

  1. Beyond compare 3 doing actions without asking user
    By pele1200 in forum Windows Software
    Replies: 3
    Last Post: 13-08-2012, 01:31 PM
  2. how to use voice actions on Ice cream Sandwich
    By Popeyee in forum Portable Devices
    Replies: 9
    Last Post: 14-12-2011, 02:24 PM
  3. How to use Actions in Java?
    By Beter 2 Burn Out in forum Software Development
    Replies: 4
    Last Post: 14-02-2010, 06:40 AM
  4. Multiple actions of a form
    By Sadiee in forum Software Development
    Replies: 3
    Last Post: 29-06-2009, 07:14 PM
  5. Actions depending on the length of the press of a button
    By Elijah in forum Software Development
    Replies: 3
    Last Post: 16-03-2009, 10:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,873,030.22603 seconds with 17 queries