Results 1 to 2 of 2

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

  1. #1

    Thread Starter
    Addicted Member Kram Kramer's Avatar
    Join Date
    Dec 2016
    Posts
    131

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    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.

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