-1

I try to access to inputs value in wpf project but get me error that :

The name 'txt_SlaveNumber' does not exist in the current context

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

            }

            private void btn_RunModbus_Click(object sender, RoutedEventArgs e)
            {
                byte SlaveNumber = byte.Parse(txt_SlaveNumber.Text);
                byte FunctioCode = byte.Parse(txt_FunctionCode.Text);
                int StartIndex = Int32.Parse(txt_StartIndex.Text);
                int DataLength = Int32.Parse(txt_DataLength.Text);
                Modbus modbus = new Modbus();
                modbus.ModBusCommand(SlaveNumber,FunctioCode,StartIndex,DataLength);

            }
        }
    }

<Window x:Class="SPMS_WIN.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:SPMS_WIN"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="30,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_SlaveNumber"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="196,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_FunctionCode"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="361,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_StartIndex"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="543,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_DataLength"/>
        <Label Content="Slave Address&#xD;&#xA;" HorizontalAlignment="Left" Margin="30,72,0,0" VerticalAlignment="Top" Width="120" Height="26"/>
        <Label Content="Function Code" HorizontalAlignment="Left" Margin="196,72,0,0" VerticalAlignment="Top" Width="120"/>
        <Label Content="Start Index" HorizontalAlignment="Left" Margin="361,72,0,0" VerticalAlignment="Top" Width="120"/>
        <Label Content="Data Length" HorizontalAlignment="Left" Margin="543,72,0,0" VerticalAlignment="Top" Width="120"/>
        <Button Content="Run" HorizontalAlignment="Left" Margin="30,142,0,0" VerticalAlignment="Top" Width="75" AutomationProperties.Name="btn_RunModbus" Click="btn_RunModbus_Click"/>

    </Grid>
</Window>

What is problem?

hmahdavi
  • 2,250
  • 3
  • 38
  • 90

2 Answers2

1

Maybe if you change :

<TextBox HorizontalAlignment="Left" Height="23" Margin="30,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_SlaveNumber"/>

to :

<TextBox HorizontalAlignment="Left" Height="23" Margin="30,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"  Name="txt_SlaveNumber"/>

It would resolve your problem.

1

The problem is the tag generated for the name

AutomationProperties.Name

Replace it with

x:Name

and it should be ok.

Lupu Silviu
  • 1,145
  • 11
  • 23
  • I am surprised by the answer of @VitaliyZayarniy, `Name` works for me too. I've always used `x:Name`. https://stackoverflow.com/questions/589874/in-wpf-what-are-the-differences-between-the-xname-and-name-attributes – marbel82 Jul 19 '19 at 10:22