Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: CS0019: Operator '+' cannot be applied to operands of type 'TextBox' and 'TextBox' WPF + c# application

So I’m working on a application that ask for age, weight, height, sex, name and after clicking a button makes a info.txt and writes the information given by the user

WPF:


<TextBox Margin="0,15,0,0" x:Name="txtName" Width="300" FontSize="18" materialDesign:HintAssist.Hint="Enter name" BorderThickness="2" BorderBrush="{DynamicResource MaterialDesignDivider}"  Style="{StaticResource MaterialDesignOutlinedTextBox}" />

<TextBox Margin="0,15,0,0" x:Name="txtWeight" Width="300" FontSize="18" materialDesign:HintAssist.Hint="Enter Weight (KG)" BorderThickness="2" BorderBrush="{DynamicResource MaterialDesignDivider}"  Style="{StaticResource MaterialDesignOutlinedTextBox}" />

<TextBox Margin="0,15,0,0" x:Name="txtHeight" Width="300" FontSize="18" materialDesign:HintAssist.Hint="Enter Height (CM)" BorderThickness="2" BorderBrush="{DynamicResource MaterialDesignDivider}"  Style="{StaticResource MaterialDesignOutlinedTextBox}" />

<TextBox Margin="0,15,0,0" x:Name="txtSex" Width="300" FontSize="18" materialDesign:HintAssist.Hint="Enter Sex (Male / Female)" BorderThickness="2" BorderBrush="{DynamicResource MaterialDesignDivider}"  Style="{StaticResource MaterialDesignOutlinedTextBox}" />

<Button  Margin="0,50,0,0" x:Name="loginBtn" Style="{StaticResource MaterialDesignFlatMidBgButton}" materialDesign:ShadowAssist.ShadowDepth="Depth0" Height="53" Width="300" materialDesign:ButtonAssist.CornerRadius="10" FontSize="18" Content="Create Profile" Click="doLogin"></Button> 

C#

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public void doLogin(object sender, RoutedEventArgs e)
        {
            try
            {
                TextWriter tw = new StreamWriter("info.txt", true);
                tw.WriteLine(txtAge + txtWeight + txtHeight + txtSex + txtName);
                tw.Close();

            }catch(Exception myE)
            {
                Console.Write(myE);
            }

            Console.Read();
        }

Error

Error: CS0019: Operator '+' cannot be applied to operands of type 'TextBox' and 'TextBox'

The error code is from the C# code

>Solution :

You need to access the text atribute of the TextBoxes not the TextBox themselves.

tw.WriteLine(txtAge.Text + txtWeight.Text + txtHeight.Text + txtSex.Text + txtName.Text);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading