文本框保留结果c#wpf

时间:2015-03-27 11:59:25

标签: c# wpf textbox

我正在一个简单的程序中使用一些按钮,一个文本框和一些组合框。

我的文本代码BOX需要帮助。我想要做的是当我再次点击按钮时,结果必须再次出现在文本框中,而不删除以前的结果:

<StackPanel>
    <ComboBox Name="cb1" Margin="0,10,0,0" Width="100">
        <ComboBoxItem Content="Peugeot"/>
        <ComboBoxItem Content="BMW"/>
        <ComboBoxItem Content="GOLF"/>
    </ComboBox>

    <ComboBox Name="cb2" Margin="0,10,0,0">
        <ComboBoxItem Content="Usa"/>
        <ComboBoxItem Content="Germany"/>
        <ComboBoxItem Content="France"/>
    </ComboBox>

    <TextBox Name="txt1" Margin="0,10,0,0" Width="200" Height="100"/>
    <Button Name="btnclick" Margin="0,10,0,0" Width="50" Height="30" Content="Click" Click="btnclick_Click" />

3 个答案:

答案 0 :(得分:0)

你需要做的就是

txt1.Text+= result.Tostring();

或者如果你想在结果之间留一个空格

txt1.Text+= result.Tostring()+" ";

答案 1 :(得分:-1)

如果你想要的是当按下按钮时,组合框中的值被添加到文本框中,那么你可以使用这样的东西:

//add combobox value behind existing text
textBox.Text+= comboBox.SelectedIndex.ToString();
//add the value behind existing text with a space or static string
textBox.Text+= " " + comboBox.SelectedIndex.ToString();
//add the value to the textbox on a new line (requires multiline to be true)
textBox.Text+= comboBox.SelectedIndex.ToString() + Environment.NewLine;

由于我在Winforms中工作,我不知道WPF控件的所有设置,所以我想我会查找多行。 Source

Set TextWrapping="Wrap" and AcceptsReturn="True" on your TextBox.

如果这不是你想要的东西,但你正在寻找它以不同的方式工作,请在这种情况下进一步解释这个问题。

我希望这对你有所帮助。

答案 2 :(得分:-1)

后面有代码

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnclcik_Click(object sender, RoutedEventArgs e)
    {
        if (cb1.IsArrangeValid == true)
            if (cb2.IsArrangeValid == true)
                txt1.Text = "Car:" + cb1.Text + "\n" + "state:" + cb2.Text;
        txt1.Text += " " + cb1.SelectedIndex.ToString();
    }

    private void btndel_Click(object sender, RoutedEventArgs e)
    {
        cb1.Text = "";
        cb2.Text = "";
    }