Results 1 to 15 of 15

Thread: [RESOLVED] Is this possible?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Resolved [RESOLVED] Is this possible?

    Is it possible to replace "button1.backcolor" or just "button1" in this code with a variable(which will contain a buttons name):

    private sub()
    button1.backcolor = color.blue
    end sub


    So instead the code would be :

    private sub()
    variablename = color.blue
    end sub

    Thanks!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Is this possible?

    try this:

    vb.net Code:
    1. directcast(me.controls("button name"), button).backcolor = color.blue

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: Is this possible?

    Thank you paul for another great answer, will try this tomorrow!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Is this possible?

    Quote Originally Posted by .paul. View Post
    try this:

    vb.net Code:
    1. directcast(me.controls("button name"), button).backcolor = color.blue
    Just one point to note: the Controls collection returns a Control reference and BackColor is a property of the Control class, so no cast is actually required. It's only if you wanted to use a member of that specific type of control that you'd have to cast.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: [RESOLVED] Is this possible?

    I know that this post isn't in the c# section, but what's the C# way of doing this exact thing?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [RESOLVED] Is this possible?

    ((button)this.controls("button name")).backcolor = color.blue;

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [RESOLVED] Is this possible?

    Quote Originally Posted by .paul. View Post
    ((button)this.controls("button name")).backcolor = color.blue;
    @JM. is the cast situation the same in c#?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Is this possible?

    Quote Originally Posted by .paul. View Post
    @JM. is the cast situation the same in c#?
    Yes it is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [RESOLVED] Is this possible?

    ok thanks

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: [RESOLVED] Is this possible?

    Quote Originally Posted by .paul. View Post
    ((button)this.controls("button name")).backcolor = color.blue;
    Code:
    block15.Top = ((Panel)this.controls(posY)).Top;
    This doesnt seem to work..

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Is this possible?

    Quote Originally Posted by ErikJohansson View Post
    Code:
    block15.Top = ((Panel)this.controls(posY)).Top;
    This doesnt seem to work..
    Then you're doing it wrong. What's the value of posY? Does it contain the name of a Panel on your form? What actually happens when you run that code? Is an exception thrown? Please provide us with all the relevant information rather than expect us to divine it from one line of code that we know would work if used properly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: [RESOLVED] Is this possible?

    Quote Originally Posted by jmcilhinney View Post
    Then you're doing it wrong. What's the value of posY? Does it contain the name of a Panel on your form? What actually happens when you run that code? Is an exception thrown? Please provide us with all the relevant information rather than expect us to divine it from one line of code that we know would work if used properly.
    posY is a string and it's value is "panel16". "controls" in the line of code is underlined red and the error in the Error List is:

    Error 1 'Slider_Puzzle.Form1' does not contain a definition for 'controls' and no extension method 'controls' accepting a first argument of type 'Slider_Puzzle.Form1' could be found (are you missing a using directive or an assembly reference?) C:\Users\Erik\documents\visual studio 2010\Projects\Slider Puzzle\Slider Puzzle\Form1.cs 32 44 Slider Puzzle

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Is this possible?

    Aha, you're coding in C#, which is case-sensitive, and you just copied and pasted .paul.'s code. If you had typed it then Intellisense would have shown you that, like all properties, Controls begins with an upper case letter. It does in VB as well but VB is not case-sensitive so the IDE changes the case for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: [RESOLVED] Is this possible?

    Quote Originally Posted by jmcilhinney View Post
    Aha, you're coding in C#, which is case-sensitive, and you just copied and pasted .paul.'s code. If you had typed it then Intellisense would have shown you that, like all properties, Controls begins with an upper case letter. It does in VB as well but VB is not case-sensitive so the IDE changes the case for you.
    hmmm... I'm getting this error now:

    Non-invocable member 'System.Windows.Forms.Control.Controls' cannot be used like a method.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Is this possible?

    This is why you should post C# questions in the C# forum. Do you know how to write C# code or not? Controls is a collection and if you want to index a collection in C# you use brackets [], not parentheses ().
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width