Disable specific button from set of programatically created buttons?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Disable specific button from set of programatically created buttons?

  1. #1
    Join Date
    Jan 2010
    Posts
    130

    Question Disable specific button from set of programatically created buttons?

    Not knowing the exact number of labels needing to be created, I have created multiple instances of Linkbuttons "mLbtnPage" and have assigned each a different ID. On clicking a certain linklabel it successfully changes to gray. All the other linklabels are then supposed to turn blue. My method however only checks if the last created linklabel needs to be turned to blue instead of checking every label. I have been stuck on this for AGES and the only way I can think of tackling this problem is by sticking all the labels in a Gridview but Im hoping there is a simpler way!!

    Code:
            protected void CreateSetOfLinklabels()
            {
                    for (int i = 1; i < miPages + 1; i++)
                    {
                        // Add label with corresponding page no
                        mLbtnPage = new LinkButton();
                        mLbtnPage.ID = i.ToString();
                        mLbtnPage.Text = i.ToString();
                        holderPages.Controls.Add(mLbtnPage);
                        mLbtnPage.Click += new EventHandler(lbtnPage_Click);
    
                        Literal literalSpace = new Literal();
                        literalSpace.Text = "&nbsp; &nbsp;";
                        holderPages.Controls.Add(literalSpace);
    
                        // Current page
                        if (Convert.ToInt32(mLbtnPage.ID) == miSelectedPage)
                        {
                            mLbtnPage.ForeColor = System.Drawing.Color.Gray;
                        }
                    }
    
            protected void lbtnPage_Click(object sender, EventArgs e)
            {
                if (sender is LinkButton)
                {
                    LinkButton linkbutton = sender as LinkButton;
                    miSelectedPage = int.Parse(linkbutton.ID);
    
                    SetDefaultLinkButtonProperties();
    
                    // Set properties of selected linkbutton
                    linkbutton.ForeColor = System.Drawing.Color.Gray;
    
                    // Create a table with the loaded details
                    CreateTblReportSelection(miSelectedPage);
                }
            }
    
            protected void SetDefaultLinkButtonProperties()
           {
                 // Return all other linklabels to default color
                mLbtnPage.ForeColor = mLbtnPage.ID != Convert.ToString(miSelectedPage) ? System.Drawing.Color.Blue : System.Drawing.Color.Gray;
            }
            #endregion

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Disable specific button from set of programatically created buttons?

    I would suggest to use the Controls collection of that form. There i would fo through that collection and and checking the type of the control if it is a link label and doesn't have the ID of the actual control I would change the color. You dont need to do your own collection as there is already 'Controls' there to use it

    In the creation method you need to add the labels positions !! and additional add
    Code:
    mLbtnPage.Name = "LinkButton";
    Then in the click event do
    Code:
    void mLbtnPage_Click(object sender, EventArgs e) {
        LinkButton lb = sender as LinkButton;
        string id = lb.ID;
        foreach (Control c in this.Controls ) {
            if (c.GetType().Name  == "LinkButton" ){
                if (((LinkButton)c).ID != id ) { 
                    ((LinkButton)c).ForeColor = Color.Red; 
                }else{
                    ((LinkButton)c).ForeColor = Color.Gray; 
                }
            }
    
        }
    }
    This checks which control is the selected and coloers all others to red in my example.
    BTW I dont know if you are able to change LinkButtons color. ( I did no test ) LinkLabels can not change the color. So simple test it
    Last edited by JonnyPoet; May 16th, 2010 at 06:09 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Re: Disable specific button from set of programatically created buttons?

    The following code is not recognized: mLbtnPage.Name = "LinkButton". I tried using the NamingContainer instead but I received a syntax error:

    Code:
    mLbtnPage.NamingContainer = "LinkButton";

  4. #4
    Join Date
    May 2010
    Location
    Montréal, Québec
    Posts
    9

    Re: Disable specific button from set of programatically created buttons?

    And if you don't want to do the parsing of the Controls, a simple member variable that keeps a reference on the currently selected button would do the job. On a click, just disable the old button and replace it with the new.

  5. #5
    Join Date
    Jan 2010
    Posts
    130

    Re: Disable specific button from set of programatically created buttons?

    I think thats what I was originally trying to do.... I have replaced the ".Name" of JonnyPoet&#180;s method with a different method to check if the control that is currently been looped through is a Linkbutton. Only one control is however found and that is of type "ASP.input_master_master" (my master page!), not of type LinkButton! I have tried various other methods of reaching the LinkButton, incl. the use of "is LinkButton" but each time I receive the response that none exist.... strange...

    Code:
     void mLbtnPage_Click(object sender, EventArgs e)
            {
                LinkButton lb = sender as LinkButton;
                string id = lb.ID;
    
                foreach (LinkButton lbtn in Controls)
                {
                    if (lbtn.ID == id)
                    {
                        lbtn.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        lbtn.ForeColor = System.Drawing.Color.PaleGoldenrod;
                    }
              }

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Disable specific button from set of programatically created buttons?

    Quote Originally Posted by stephsh View Post
    The following code is not recognized: mLbtnPage.Name = "LinkButton". I tried using the NamingContainer instead but I received a syntax error:

    Code:
    mLbtnPage.NamingContainer = "LinkButton";
    Tobe able to be recognized as "LinkButton" you need to set the name Property during creation of that buttons as I have already told you You need to add that line into your CreateSetOfLinkLabels Method
    Code:
    protected void CreateSetOfLinklabels() {
       for (int i = 1; i < miPages + 1; i++){
          // Add label with corresponding page no
          mLbtnPage = new LinkButton();
          mLbtnPage.ID = i.ToString();
          mLbtnPage.Text = i.ToString();
          mLbtnPage.Name ="LinkButton"; 
          holderPages.Controls.Add(mLbtnPage);
          mLbtnPage.Click += new EventHandler(lbtnPage_Click);
          Literal literalSpace = new Literal();
          literalSpace.Text = "&nbsp; &nbsp;";
          holderPages.Controls.Add(literalSpace);
       }
    }
    If you dont add this, my other code will not work! If you do it will !
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Disable specific button from set of programatically created buttons?

    Quote Originally Posted by TechnoGilles View Post
    And if you don't want to do the parsing of the Controls, a simple member variable that keeps a reference on the currently selected button would do the job. On a click, just disable the old button and replace it with the new.
    Sorry this is shooting with a gun to ants, like if I neeed the wall painted in green let the bricklayers bbuilt up a new one
    And BTW it will not hange all other controls to blue !!
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Jan 2010
    Posts
    130

    Re: Disable specific button from set of programatically created buttons?

    The point is that "Name" is not in the LinkLabel's property list, it's not a recognized command... Maybe Im missing an import? My current imports are:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Text;
    using System.Drawing;
    using System.IO;
    using System.Collections;
    Quote Originally Posted by JonnyPoet View Post
    Tobe able to be recognized as "LinkButton" you need to set the name Property during creation of that buttons as I have already told you You need to add that line into your CreateSetOfLinkLabels Method
    Code:
    protected void CreateSetOfLinklabels() {
       for (int i = 1; i < miPages + 1; i++){
          // Add label with corresponding page no
          mLbtnPage = new LinkButton();
          mLbtnPage.ID = i.ToString();
          mLbtnPage.Text = i.ToString();
          mLbtnPage.Name ="LinkButton"; 
          holderPages.Controls.Add(mLbtnPage);
          mLbtnPage.Click += new EventHandler(lbtnPage_Click);
          Literal literalSpace = new Literal();
          literalSpace.Text = "&nbsp; &nbsp;";
          holderPages.Controls.Add(literalSpace);
       }
    }
    If you dont add this, my other code will not work! If you do it will !

  9. #9
    Join Date
    Dec 2007
    Posts
    234

    Re: Disable specific button from set of programatically created buttons?

    Maybe it's my simple mind at work, but what about putting your created LunkButtons into a List<LinkButton> ... in the click event, you have the Sender object, type it to a LinkButton, then loop through the List... if it matches, set the link color to gray... if it doesn't match, set it to blue.

    Or am I oversimplifying it?

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  10. #10
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Disable specific button from set of programatically created buttons?

    Quote Originally Posted by TechGnome View Post
    Maybe it's my simple mind at work, but what about putting your created LunkButtons into a List<LinkButton> ... in the click event, you have the Sender object, type it to a LinkButton, then loop through the List... if it matches, set the link color to gray... if it doesn't match, set it to blue.

    Or am I oversimplifying it?

    -tg
    This is impossible as this would only add buttons which are clicked and you would not be able to set all the not selected buttons to blue as he wanted to do. And as we already have a Controls Collection why not simpleusing it ??? Sorry, Strange idea.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  11. #11
    Join Date
    May 2010
    Location
    Montréal, Québec
    Posts
    9

    Re: Disable specific button from set of programatically created buttons?

    Sorry this is shooting with a gun to ants
    Hum... Parsing through the Controls is somewhat dangerous. You never know what you'll find... What if other LinkButtons are also present in the page ? You'll be shooting at flies with the intent of killing ants !!

    I stick to what I said. A simple member variable will do the job with the less amount of code, memory, and processing. And maximum robustness !!

    Code:
    private LinkButton currentlySelectedLinkLabel;
    
    protected void CreateSetOfLinklabels()
    {
        // Add label with corresponding page no
        for (int i = 1; i < miPages + 1; i++)
       {
          ...
          mLbtnPage.ForeColor = your "not selected" color;
          ...
    
          // Current page
          if (Convert.ToInt32(mLbtnPage.ID) == miSelectedPage)
          {
             SelectLinkLabel(mLbtnPage);
          }
       }
    }
    
    void mLbtnPage_Click(object sender, EventArgs e)
    {
       SelectLinkLabel(sender as LinkButton);
    }
    
    private SelectLinkLabel(LinkButton linkLabel)
    {
       if (currentlySelectedLinkLabel != null)
       {
          currentlySelectedLinkLabel.ForeColor = your "not selected" color;
    
          ... other processing...
       }
    
       currentlySelectedLinkLabel = linkLabel;
    
       if (currentlySelectedLinkLabel != null) // This allows to call this method with null to deselect everything
       {
          currentlySelectedLinkLabel.ForeColor = your "selected" color;
    
          ... other processing...
       }
    }

  12. #12
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Disable specific button from set of programatically created buttons?

    Quote Originally Posted by stephsh View Post
    The point is that "Name" is not in the LinkLabel's property list, it's not a recognized command... Maybe Im missing an import? My current imports are:
    The missing information is another thing. Is LinkButton something created by you or is it a LinkLabel?
    But if you miss that Property simple add it
    Code:
    public class MyLinkButton: LinkButton {
     
    public string Name{get;set;}
     
    }
    BTW LinkLabel has a Name Property so please explain what Framework are you using?
    Looking at your references this basically seems to be a Web page, but the method of adding controls to the form seems to be done as we do in Windows Form projects. But in Windows form we are using LinkLabels and in Web you are using LinkButtons. So maybe show your project or what already is done and works.
    Last edited by JonnyPoet; May 17th, 2010 at 05:09 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  13. #13
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Disable specific button from set of programatically created buttons?

    Quote Originally Posted by TechnoGilles View Post
    Hum... Parsing through the Controls is somewhat dangerous. You never know what you'll find...
    What if other LinkButtons are also present in the page ? ...
    Thats a simple question. Who has coded the project? That one normally SHOULD know which control are available on his form. There is no 'mystery' in coding never. If you are not sure all controls would have Name Property put the code into a try-catch and you will be secure to only get controls which have a Name Property and also where the name is set to "LinkButton" You also can use another name if you want. Simple set the name and ask for that name, thats all.

    But there are other moe important questions than this, so what type of project this is?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  14. #14
    Join Date
    May 2010
    Location
    Montréal, Québec
    Posts
    9

    Talking Re: Disable specific button from set of programatically created buttons?

    No mystery ? Obviously !! ... All I'm saying is that the day you need to add a LinkButton in that page for whatever reason, in your case, you'll have to refactor the code somehow. In my case you won't.

    I just think it is a good development rule not to assume things. I like plain and simple code that can resist to changes over time !!!

  15. #15
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Disable specific button from set of programatically created buttons?

    Quote Originally Posted by TechnoGilles View Post
    No mystery ? Obviously !! ... All I'm saying is that the day you need to add a LinkButton in that page for whatever reason, in your case, you'll have to refactor the code somehow. In my case you won't.

    I just think it is a good development rule not to assume things. I like plain and simple code that can resist to changes over time !!!
    Basically I can agree with your code in post #11
    As this will work to set a selection color and to reset unselected buttons if he wants something like that. Bus as seen in your code this per sure doesn't need a List<LinkButton>

    And if you would have done using such a list you would have a great need of code changes when adding something in your page too. So thats not the point IMHO.

    IMHO the point is that the shown code seeml me like a code for Widows Forms, but the References he showed point to a web application. So what is he really doing here.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center