PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] TextBlock1.Background = “myBrush” & 1 is not working-VBForums
Results 1 to 2 of 2

Thread: [RESOLVED] TextBlock1.Background = “myBrush” & 1 is not working

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    78

    Resolved [RESOLVED] TextBlock1.Background = “myBrush” & 1 is not working

    xaml codes are here;

    Code:
    <Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock x:Name="TextBlock1" Width="100" Height="20" Background="Blue"/>
    </Grid>
    </Window>
    vb.net codes are here;

    Code:
    Class MainWindow 
    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Dim myBrush1 As New SolidColorBrush(CType(ColorConverter.ConvertFromString("#FF0000"), Color))
        TextBlock1.Background = myBrush1
    End Sub
    End Class
    The codes above are okey.

    My question is here;

    I want to use
    Code:
    TextBlock1.Background = "myBrush" & 1
    instead of
    Code:
    TextBlock1.Background = myBrush1

    I think I need a code something like this;
    Code:
    Dim myBrush As SolidColorBrush = CType("myBrush" & 1, SolidColorBrush)
    TextBlock1.Background = myBrush
    Last edited by Kram Kramer; Nov 28th, 2018 at 04:56 PM.

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

    Re: TextBlock1.Background = “myBrush” & 1 is not working

    Quote Originally Posted by Kram Kramer View Post
    I want to use
    Code:
    TextBlock1.Background = "myBrush" & 1
    instead of
    Code:
    TextBlock1.Background = myBrush1

    I think I need a code something like this;
    Code:
    Dim myBrush As SolidColorBrush = CType("myBrush" & 1, SolidColorBrush)
    TextBlock1.Background = myBrush
    Quote Originally Posted by Kram Kramer View Post
    I want to use
    Code:
    TextBlock1.Background = "myBrush" & 1
    instead of
    Code:
    TextBlock1.Background = myBrush1

    I think I need a code something like this;
    Code:
    Dim myBrush As SolidColorBrush = CType("myBrush" & 1, SolidColorBrush)
    TextBlock1.Background = myBrush
    No, because that would be magic. In your code, 'myBrush' is a variable. You are expecting to convert a String object containing the name of a variable into the object referred to by the variable with that name. Not happening.

    Why exactly do you think that this is a good idea to begin with? That is important information that you should have included in the question. Why you're trying to do something is important because then we can determine the best way to achieve your actual aim. My guess would be that you have multiple brushes so you want to be able to just use a single name and then append the number of the desired brush. If that's so then the thing to do would be to put all the brushes in an array or collection and you can then access them by index, e.g.
    vb.net Code:
    1. Dim myBrushes As SolidColorBrush() = {New SolidColorBrush(CType(ColorConverter.ConvertFromString("#FF0000"), Color))}
    2.  
    3. TextBlock1.Background = myBrushes(0)
    You can add as many brushes as you like to the array and then access each one by index. Just note that the first index is 0, not 1. If you want to add some brushes now and some later, use a List(Of T) rather than an array. Also note that you will probably have to declare the variable for the array or collection at the class level, so that you can access it in other methods.
    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
  •  



Featured


Click Here to Expand Forum to Full Width