C#,How to clear all textbox text???????

Posted by Sudhakar_A under WPF on 11/20/2013 | Points: 10 | Views : 14647 | Status : [Member] | Replies : 12
I have 10 text boxes...........on button click,I want to clear text of all textboxes




Responses

Posted by: Bandi on: 11/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://social.msdn.microsoft.com/Forums/vstudio/en-US/e3bd6d27-b65c-488a-aaa7-7f4adcbe264b/clearing-all-textboxes-in-wpf-window?forum=wpf
http://stackoverflow.com/questions/4569992/clear-multiple-textbox-value-in-wpf


Sample code:
XAML:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="250" Width="325">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="187*" />
<ColumnDefinition Width="316*" />
</Grid.ColumnDefinitions>
<StackPanel x:Name="StackPanel1" Grid.ColumnSpan="1">
<TextBox Margin="5" BorderBrush="#FF171212" />
<TextBox Margin="5" BorderBrush="#FF171212" />
<TextBox Margin="5" BorderBrush="#FF171212" />
<TextBox Margin="5" BorderBrush="#FF171212" />
</StackPanel>

<Button x:Name="btnClearText" Grid.Column="1" Width="99" Height="33" Content="Clear Text"
Click="btnClearText_Click"/>
</Grid>


Code Behind:
Class MainWindow

Private Sub btnClearText_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
ClearTextBoxes(StackPanel1)
End Sub

Private Sub ClearTextBoxes(ByVal depObject As DependencyObject)
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(depObject) - 1
If TypeOf depObject Is TextBox Then
CType(depObject, TextBox).Text = Nothing
End If

ClearTextBoxes(VisualTreeHelper.GetChild(depObject, i))
Next
End Sub
End Class




Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Reference:
http://vbcity.com/blogs/xtab/archive/2011/04/27/wpf-how-to-clear-text-from-textboxes-and-textblocks.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 11/20/2013 [Member] Starter | Points: 25

Up
0
Down
@Chandu........Thnks for your reply ,I have not used any stack panel.
Is der any other way to do it without using stack panel.

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
this??
http://stackoverflow.com/questions/6783509/clear-the-text-in-multiple-textboxs-at-once

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://social.msdn.microsoft.com/Forums/vstudio/en-US/51e152cd-832a-4312-b7c6-0b17d8b2ad02/wpf-clear-all-textbox-content?forum=wpf
http://www.codeproject.com/Tips/255136/How-to-Clear-all-the-TextBox-value-in-Single-Click


http://stackoverflow.com/questions/9579339/raising-a-button-clicked-event-from-a-style-in-a-wpf-text-box

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 11/20/2013 [Member] Starter | Points: 25

Up
0
Down
@chandu...........i used u r link:-http://stackoverflow.com/questions/6783509/clear-the-text-in-multiple-textboxs-at-once .

private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (UIElement control in myGrid.Children)
{
if (control.GetType() == typeof(TextBox))
{
TextBox txtBox = (TextBox)control;
txtBox.Text = null;
}
}
}

The above function is giving me an error in Grid.Children in ma code

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
use this.GridName.Children

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 11/20/2013 [Member] Starter | Points: 25

Up
0
Down
this.GridName.Children is giving me the above error:-

Error 12-'Update.Window5' does not contain a definition for 'GridName' and no extension method 'GridName' accepting a first argument of type 'Update.Window5' could be found (are you missing a using directive or an assembly reference?)


Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
hi sudhakar,
Place all text boxes inside Grid and then apply above approach for clear functionality
<Grid Name="YourGrid ">
<TextBox .... >
<TextBox .... >
<TextBox .... >
</Grid>

this.YourGrid .Children


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 11/20/2013 [Member] Starter | Points: 25

Up
0
Down
@Chandu..............error got cleared,but nothing seems to work.
When clicked on the button.
I mean textbox not got cleared.

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
can you send the code which you have now...

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
MainWindow.xaml.cs
public MainWindow()

{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (UIElement control in MyGrid.Children)
{
if (control.GetType() == typeof(TextBox))
{
TextBox txtBox = (TextBox)control;
txtBox.Text = null;
}
}
}


MainWindow.xaml

<Window x:Class="clearAllTextBox.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="MyGrid">
<TextBox Height="24" HorizontalAlignment="Left" Margin="56,26,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="56,73,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="218,26,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="56,206,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="56,158,0,0" Name="textBox5" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="56,116,0,0" Name="textBox6" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="234,107,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>


This code is working for me...


References:
http://stackoverflow.com/questions/5332013/wpf-clear-all-textboxes-in-tabcontrol-not-working
http://stackoverflow.com/questions/5474173/i-want-to-clear-all-values-in-grid-not-datagrid-children-in-wpf
http://stackoverflow.com/questions/2756683/wpf-textbox-trigger-to-clear-text

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response