Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:37:52+00:00 2026-06-06T23:37:52+00:00

I am trying to create a code-only WPF app but am getting the above

  • 0

I am trying to create a code-only WPF app but am getting the above error when I type in the textBox. This is though ALL my variables are initialized.

The windows1.xaml is this:

<?xml version="1.0" encoding="utf-8"?>

<Window>

    x:Class="BlendCatalogue.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BlendCatalogue"
    Height="300"
    Width="300">
</Window>

The code behind is this:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace BlendCatalogue
{

    public partial class Window1 : Window
    {
            private TextBlock textBlock;
            private TextBox textBox;

        public Window1()
        {
            InitializeComponent();
            Initialization();
        }

        public void Initialization()
        {


            this.Width=300;
            this.Height=200;
            this.Background =Brushes.Aquamarine;
            this.Title = "Only the best!";

            Grid layoutGrid = new Grid();
            StackPanel stackpanel = new StackPanel();
            layoutGrid.Children.Add(stackpanel);
            this.AddChild(layoutGrid);

            TextBlock textBlock = new TextBlock();
            textBlock.Margin = new Thickness(6);
            textBlock.Height = 20;
            textBlock.TextAlignment = TextAlignment.Center;
            textBlock.Text = "Hello my World!";
            stackpanel.Children.Add(textBlock);


            TextBox textBox = new TextBox();
            textBox.Margin = new Thickness(5);
            textBox.Width = 150;
            textBox.TextAlignment = TextAlignment.Center;
            textBox.Text = "";
            textBox.TextChanged += OnTextChanged;
            stackpanel.Children.Add(textBox);

            Button btnColor = new Button();
            btnColor.Margin = new Thickness(5);
            btnColor.Width = 150;
            btnColor.Content = "Change Text Color";
            btnColor.Click += btnChangeColor_Click;
            stackpanel.Children.Add(btnColor);

            Button btnSize = new Button();
            btnSize.Margin = new Thickness(5);
            btnSize.Width = 150;
            btnSize.Content = "Change Text Color";
            btnSize.Click += btnChangeSize_Click;
            stackpanel.Children.Add(btnSize);
        }

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {

            textBlock.Text = textBox.Text;
        }

        private void btnChangeColor_Click(object sender, RoutedEventArgs e)
        {
            if (textBlock.Foreground == Brushes.Black)
                textBlock.Foreground = Brushes.Red;
            else
                textBlock.Foreground = Brushes.Black;
        }
        private void btnChangeSize_Click(object sender, RoutedEventArgs e)
        {
            if (textBlock.FontSize == 11)
                textBlock.FontSize = 42;
            else
                textBlock.FontSize = 11;
        }

    }
}

This newbie simply does not know what is being done wrong and would really appreciate any help.
Thanks all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:37:53+00:00Added an answer on June 6, 2026 at 11:37 pm

    Your are creating two TextBlock's and two TextBox's one with Module level scope and one with Local scope. You then initialize the one with Local scope and try to use the one with Module level scope causing your error.

    Try changing your code in your Initialization Method to:

    textBlock = new TextBlock();
    textBlock.Margin = new Thickness(6);
    textBlock.Height = 20;
    textBlock.TextAlignment = TextAlignment.Center;
    textBlock.Text = "Hello my World!";
    stackpanel.Children.Add(textBlock);
    
    
    textBox = new TextBox();
    textBox.Margin = new Thickness(5);
    textBox.Width = 150;
    textBox.TextAlignment = TextAlignment.Center;
    textBox.Text = "";
    textBox.TextChanged += OnTextChanged;
    stackpanel.Children.Add(textBox);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a piece of code but cannot get it working. The
I'm trying to create code in AppleScript that will click my mouse randomly every
I'm trying to create a new function in SQL with the following code: CREATE
I am trying to create a 'confirmation code' for every user account created on
I am trying to create auto-vectorized code with clang-3.2 From the slides here -
Trying to create COM -object on server. code is: var myGuid = new Guid(530A1815-820C-11D3-BBB7-008048DE406A);
I am trying to create an application bar in code for WinPhone7. The XAML
I am trying to create a table within PHP code in order to open
I'm trying to create datagrid tooltip entirely in code behind file. Tooltip XAML code
I am trying to create DLL out of my C# code, I found some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.