Results 1 to 8 of 8

Thread: Command buttons from upgrade and newly added in Visual Studio 2019 are different

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    39

    Command buttons from upgrade and newly added in Visual Studio 2019 are different

    There is a difference in appearance of the new command buttons that have been added in Visual Studio from the upgraded ones from VB6. What is the best way to make all of them look similar?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: Command buttons from upgrade and newly added in Visual Studio 2019 are different

    Make sure that they are the same type and then make sure that they all have their appearance-related properties set to the same values.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    39

    Re: Command buttons from upgrade and newly added in Visual Studio 2019 are different

    Quote Originally Posted by jmcilhinney View Post
    Make sure that they are the same type and then make sure that they all have their appearance-related properties set to the same values.
    I had looked at the properties. FlatAppearance settings are the same, FlatStyle is Standard, Modifiers were different but I made them all Public. UseVisualStyleBackColor was different. This might be it.

    However, I cannot put it in a for loop like For each ctrl in Form.Controls, ctrl.UseVisualStyleBackColor = True. Is there another way or will I have to manually change all of them on each form?
    Last edited by VisualBeginner.NET; Sep 10th, 2021 at 12:47 AM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: Command buttons from upgrade and newly added in Visual Studio 2019 are different

    If you're still having issues, post a screenshot of the UI and also post the contents of your designer code file. If you don't know how to find that, click the Show All Files button at the top of the Solution Explorer and then expand the node for your form.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: Command buttons from upgrade and newly added in Visual Studio 2019 are different

    Quote Originally Posted by VisualBeginner.NET View Post
    However, I cannot put it in a for loop like For each ctrl in Form.Controls, ctrl.UseVisualStyleBackColor = True. Is there another way or will I have to manually change all of them on each form?
    If you do it in code then it would have to be done on a per-form basis, even if you do use a loop. If that property is set to False in the designer code file then you can do a Find & Replace In Files. You can do a Replace All if you do want them all replaced, otherwise you can Find Next and Replace as required.

    If you want to do it in the designer itself, open a form and select all the Buttons, then set the property to False in the Properties window. Again, that's a per-form operation.

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    39

    Re: Command buttons from upgrade and newly added in Visual Studio 2019 are different

    Quote Originally Posted by jmcilhinney View Post
    If you do it in code then it would have to be done on a per-form basis, even if you do use a loop. If that property is set to False in the designer code file then you can do a Find & Replace In Files. You can do a Replace All if you do want them all replaced, otherwise you can Find Next and Replace as required.

    If you want to do it in the designer itself, open a form and select all the Buttons, then set the property to False in the Properties window. Again, that's a per-form operation.
    Thanks. Now I understand (I think) what is going on. Even manually replacing is not difficult because I could select a lot of buttons using the pointer (and ctrl to add more) and then set the Visual style back color once for each form. I also see that I could have done it in the form designer but as a beginner, I am not supposed to mess with the designer files.

    I still don't figure out how I could do it in code on each form because I still cannot write If TypeOf ctrl is button then ctrl.UseVisualStyleBackColor = True.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: Command buttons from upgrade and newly added in Visual Studio 2019 are different

    Quote Originally Posted by VisualBeginner.NET View Post
    I still don't figure out how I could do it in code on each form because I still cannot write If TypeOf ctrl is button then ctrl.UseVisualStyleBackColor = True.
    UseVisualStyleBackColor is a member of ButtonBase, so you can only access it on a reference of that type or a derived type. If you wanted to do this on every Button then you would need a Button reference. You can get that in a number of ways:
    vb.net Code:
    1. For Each cntrl In Controls
    2.     If TypeOf cntrl Is Button Then
    3.         DirectCast(cntrl, Button).UseVisualStyleBackColor = True
    4.     End If
    5. Next
    vb.net Code:
    1. For Each cntrl In Controls
    2.     Dim btn = TryCast(cntrl, Button)
    3.  
    4.     If btn IsNot Nothing Then
    5.         btn.UseVisualStyleBackColor = True
    6.     End If
    7. Next
    vb.net Code:
    1. For Each btn In Controls.OfType(Of Button)()
    2.     btn.UseVisualStyleBackColor = True
    3. Next

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    39

    Re: Command buttons from upgrade and newly added in Visual Studio 2019 are different

    Quote Originally Posted by jmcilhinney View Post
    UseVisualStyleBackColor is a member of ButtonBase, so you can only access it on a reference of that type or a derived type. If you wanted to do this on every Button then you would need a Button reference. You can get that in a number of ways:
    vb.net Code:
    1. For Each cntrl In Controls
    2.     If TypeOf cntrl Is Button Then
    3.         DirectCast(cntrl, Button).UseVisualStyleBackColor = True
    4.     End If
    5. Next
    vb.net Code:
    1. For Each cntrl In Controls
    2.     Dim btn = TryCast(cntrl, Button)
    3.  
    4.     If btn IsNot Nothing Then
    5.         btn.UseVisualStyleBackColor = True
    6.     End If
    7. Next
    vb.net Code:
    1. For Each btn In Controls.OfType(Of Button)()
    2.     btn.UseVisualStyleBackColor = True
    3. Next
    Thank you very much. I have to learn what these DirectCast and TryCast are. The third alternative is easier (but that too was new for me). For the time being, I just manually selected all the buttons and changed the Visual style back color.

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
  •  



Click Here to Expand Forum to Full Width