-1

I am working in a simple program with some button, one text box, and also some combo box.

I need help in my code of text BOX. What I am trying to do is when I click button again results have to appear again in text box, and without deleting previous results:

<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" />
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
berat
  • 31
  • 1
  • 7
  • Could you post what you have in your 'btnclick_Click' event handler? – goobering Mar 27 '15 at 12:11
  • @berat what is the desired outcome when the button is pressed? is it supposed to set the text of the textbox to `combobox1 + combobox2` text. or is it supposed to place them behind each other and just keep adding more. or perhaps you want them to add `combobox1.value + combobox2.value` each on a new line. could you elaborate a little more on how you would like the output to be. – maam27 Mar 27 '15 at 12:40

3 Answers3

0

Well all you have to do is

txt1.Text+= result.Tostring();

or if you wanna leave a space between results

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

Tomtom
  • 9,087
  • 7
  • 52
  • 95
Coder1409
  • 523
  • 4
  • 12
-1

If what you want is that when the button is pressed the value from the combobox gets added to the textbox then you could use something like this:

//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;

Since I work in Winforms I don't know all the settings of the WPF controls, so I thought I'd lookup for the multiline. Source

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

If this is not the thing you want but you are looking for it to work in a diffrent way, please explain the question a bit further in that case.

I hope this was of help to you.

Community
  • 1
  • 1
maam27
  • 444
  • 3
  • 21
  • for example in combo box 1 i select bmw and in combo box 2 i select germany after i click button the text is showed in text box, and i want to choose again other items, but the previous item (bmw and germany) should stay in textbox , not deleting. i dont know if you are understanding me – berat Mar 27 '15 at 13:59
  • I suggest doing it like this: `TextBox1 +=comboBox1.selectedIndex.ToString() + " "+comboBox2.selectedIndex.ToString()+Environment.NewLine;` using this it puts the value of box1 then a space then box2 and then tells it to put a newline. – maam27 Mar 27 '15 at 14:25
-1

and there is behind code

    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 = "";
    }
Billy Moukas
  • 47
  • 1
  • 12
berat
  • 31
  • 1
  • 7