Error: CS0019: Operator '+' cannot be applied to operands of type 'TextBox' and 'TextBox' WPF + c# application
Error: CS0019: Operator '+' cannot be applied to operands of type 'TextBox' and 'TextBox' WPF + c# application
所以我正在开发一个应用程序,该应用程序询问年龄、体重、身高、性别、姓名,并在单击按钮后生成 info.txt 并写入用户提供的信息
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#
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: CS0019: Operator '+' cannot be applied to operands of type 'TextBox' and 'TextBox'
错误代码来自C#代码
您需要访问文本框的文本属性,而不是文本框本身。
tw.WriteLine(txtAge.Text + txtWeight.Text + txtHeight.Text + txtSex.Text + txtName.Text);
所以我正在开发一个应用程序,该应用程序询问年龄、体重、身高、性别、姓名,并在单击按钮后生成 info.txt 并写入用户提供的信息
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#
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: CS0019: Operator '+' cannot be applied to operands of type 'TextBox' and 'TextBox'
错误代码来自C#代码
您需要访问文本框的文本属性,而不是文本框本身。
tw.WriteLine(txtAge.Text + txtWeight.Text + txtHeight.Text + txtSex.Text + txtName.Text);