duant stp

duant stp

  • NA
  • 4
  • 12.1k

Pass textbox value from one form to another form

Nov 3 2010 11:29 PM
Hi All,

I don't know how to pass textbox value from child(simple keyboard) form to parent form.

this is the code:
 
//Window1.xaml(Parent) 
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Height="23" Margin="16,56,22,0" Name="textBox1" VerticalAlignment="Top" />
<Button Margin="100,128,104,111" Name="button1" Click="button1_Click">Cari</Button>
</Grid>
</Window>

form child
 //Window2.xaml(simple keyboard)
<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="148" Width="300">
<Grid Height="110" Width="277">
<Button Margin="8,39,0,46" Name="button1" Click="button1_Click" HorizontalAlignment="Left" Width="41">Q</Button>
<TextBox Height="23" Margin="8,14,9,0" Name="textBox1" VerticalAlignment="Top" />
<Button Margin="53,39,0,46" Name="button2" Click="button2_Click" HorizontalAlignment="Left" Width="40">W</Button>
<Button Margin="0,39,140,46" Name="button3" Click="button3_Click" HorizontalAlignment="Right" Width="40">E</Button>
<Button Margin="0,39,96,46" Name="button4" Click="button4_Click" HorizontalAlignment="Right" Width="40">R</Button>
<Button Margin="0,39,52,46" Name="button5" Click="button5_Click" HorizontalAlignment="Right" Width="40">T</Button>
<Button Margin="0,39,9,46" Name="button6" Click="button6_Click" HorizontalAlignment="Right" Width="41">Y</Button>
<Button Margin="8,67,0,23" Name="button7" Click="button7_Click" HorizontalAlignment="Left" Width="41">U</Button>
<Button Margin="0,0,183,23" Name="button8" Click="button8_Click" Height="20" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="41">I</Button>
<Button Margin="97,0,0,23" Name="button9" Click="button9_Click" Height="20" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="41">O</Button>
<Button Margin="0,68,96,22" Name="button10" Click="button10_Click" HorizontalAlignment="Right" Width="41">P</Button>
<Button Margin="184,67,52,23" Name="button11" Click="button11_Click">A</Button>
<Button Margin="0,67,9,23" Name="button12" Click="button12_Click" HorizontalAlignment="Right" Width="41">S</Button>
<Button Height="20" Margin="53,0,68,0" Name="button13" VerticalAlignment="Bottom" Click="button13_Click">Enter</Button>
</Grid>
</Window>

Window2.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

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

private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "Q";
}

private void button2_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "W";
}

private void button3_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "E";
}

private void button4_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "R";
}

private void button5_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "T";
}

private void button6_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "Y";
}

private void button7_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "U";
}

private void button8_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "I";
}

private void button9_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "O";
}

private void button10_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "P";
}

private void button11_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "A";
}

private void button12_Click(object sender, RoutedEventArgs e)
{
textBox1.Text += "S";
}

private void button13_Click(object sender, RoutedEventArgs e)
{
Window1 form1 = new Window1();
form1.textBox1.Text = textBox1.Text;
form1.Show();//This is the problem
Close();
}
}
}


in method  button13_Click
when I click button13(Enter) when it running, how to pass the textbox.text in Window2 to Window1 without call Window1 again or
form1.Show(). so i only have two form is running not three.

please help.........

Answers (2)