Home > Blockchain >  How can i change TextString when TextBox is changed in WPF?
How can i change TextString when TextBox is changed in WPF?

Time:10-18

i am making C# WPF Program. I am trying to code which change TextString (It is TextBlock) whenever TextBox's context is changed. But Whenever i build my programm, It happen ! "System.NullReferenceException: 'Object reference not set to an instance of an object.'" Please help me :D

<Window x:Class="TextInputOutput.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TextInputOutput"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="858">
    <Grid>
        <Button Content="클릭버튼" HorizontalAlignment="Left" Margin="346,243,0,0" VerticalAlignment="Top" Height="35" Width="70"/>
        <Border BorderThickness="1" BorderBrush="Black"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="296,161,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
        <TextBlock x:Name="TextString" HorizontalAlignment="Left" Margin="459,163,0,0" Text="Dd" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>

    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TextInputOutput
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            TextString.Text = "Hello Buddy!";

            this.textBox.TextChanged  = TextBox_TextChanged;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextString.Text = textBox.Text;
        }
    }
}

CodePudding user response:

The problem is, that you create your textbox before you create the textblock. Because you set the text in the XAML code, the Textchanged event will be raised before the textblock is created, then your error message occurs that the textblock is null. So you have to make an if statement and check whether your textbox is null and if it's not null you can change the text:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if(TextString != null)
        TextString.Text = textBox.Text;
}

Or the second option is, that you remove the Textchangedevent from the textbox in your XAML code.

CodePudding user response:

I will complement the answer from @FrozenAssassine. You are attaching the TextBox_TextChanged twice:

  1. In XAML TextChanged="TextBox_TextChanged";
  2. In Code Behind textBox.TextChanged = TextBox_TextChanged;.

The first attach (in XAML), you also immediately set the Text = "TextBox" property. And assigning this value will immediately raise the TextChanged event and call the TextBox_TextChanged method. But at this point the TextBlock has not yet been initialized and the field is empty. Therefore, you get an exception.

@FrozenAssassine suggested a solution. But in this case, you will not get the TextBlock initialized. It will only be executed after the first change to the TextBox.

There are several ways to solve it.

First:

    <!-- Removing value assignment and event handling -->
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="296,161,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            TextString.Text = "Hello Buddy!";

            this.textBox.TextChanged  = TextBox_TextChanged;
            textBox.Text = "TextBox";
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextString.Text = textBox.Text;
        }
    }

Second:

    <Grid>
        <Button Content="클릭버튼" HorizontalAlignment="Left" Margin="346,243,0,0" VerticalAlignment="Top" Height="35" Width="70"/>
        <Border BorderThickness="1" BorderBrush="Black"/>

    <!-- First the declaration of the TextBlock, and after that the TextBox. -->
        <TextBlock x:Name="TextString" HorizontalAlignment="Left" Margin="459,163,0,0" Text="Dd" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="296,161,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>

    </Grid>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextString.Text = textBox.Text;
        }
    }
  • Related