Loop Through Textboxes

Loop through Textboxes

To get all controls and sub-controls recursively of specified type, use this extension method:

public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control
{
var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>();
return children.SelectMany(c => GetChildControls<TControl>(c)).Concat(children);
}

usage:

var allTextBoxes = this.GetChildControls<TextBox>();
foreach (TextBox tb in allTextBoxes)
{
tb.Text = ...;
}

How to loop through TextBox controls? (C# WinForms)

You can traverse through each control this way

foreach (var control in this.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
// do your stuff here
}
}

Loop through textboxes in vb.net

If the TextBox controls are just on the main form, then you can loop through them:

For Each tb As TextBox In Me.Controls.OfType(Of TextBox)()
tb.Text = String.Empty
Next

If they are in a panel, then replace the Me keyword with the name of the panel.

How to loop through the textboxes in JavaScript

If your html looks like this, and you do not have other <input type="text"> tags on your form:

<input type="checkbox" id="check1" value="text1" onclick='showHide(this);' /><input type="text" id="text1" style="display:none"/> <br>
<input type="checkbox" id="check2" value="text2" onclick='showHide(this);'/><input type="text" id="text2" style="display:none"/> <br>
<input type="checkbox" id="check3" value="text3" onclick='showHide(this);'/><input type="text" id="text3" style="display:none"/> <br>
<input type="checkbox" id="check4" value="text4" onclick='showHide(this);'/><input type="text" id="text4" style="display:none"/> <br>

<input type="button" onclick='alertChecked()' value='alert checked'/>

Then you can use javascript solution:

function showHide(source){ 
var textBox = document.getElementById(source.value);

if (isHidden(textBox)){
textBox.style.display = 'inline';
}
else{
textBox.style.display = 'none';
}
}

function alertChecked(){
var text = '';
var inputs = document.getElementsByTagName('input');
for (index = 0; index < inputs.length; ++index) {
if (inputs[index].type == 'text' && isHidden(inputs[index]) === false){
text += inputs[index].value;
}
}
alert(text);
}

function isHidden(el) {
return (el.offsetParent === null)
}

Demo fiddle: http://jsfiddle.net/ggcse3zz/1/

Or jQuery solution:

function showHide(source){    
$('#'+source.value).toggle();
}

function alertChecked(){
var text = '';
$('input[type=text]:visible').each(function(){
text += $(this).val();
});
alert(text);
}

Demo fiddle: http://jsfiddle.net/ggcse3zz/

EDIT based on added HTML in question

Based on your html, you can use array of ids to find all yours input tags and loop them:

function myFunction(){ 
var text = '';
var textboxes = ["textbox1", "textbox2", "textbox3", "textbox4"];
for (index = 0; index < textboxes.length; ++index) {
var input = document.getElementById(textboxes[index]);
if (isHidden(input) === false){
text += input.value;
}

}
alert(text);
}

function isHidden(el) {
return (el.offsetParent === null)
}

Note that this javascript relies on the visibility of the input tag, not if checkbox is checked or not.

Demo fiddle: http://jsfiddle.net/usvLe3r1/

How do I loop through textboxes and clear them out?

Well, you can use List<T>.ForEach and TextBox.Clear:

listoftextboxes.ForEach(txt => txt.Clear());

with a label(or TextBox) you can use:

listoflabels.ForEach(lbl => lbl.Text = "");

Initializing all textboxes with a for loop

In general, to refer to a TextBox of the form, you need Controls("TextboxN). If you want to loop, it is like this - Me.Controls("Textbox" & i), in case that you have not deleted any textboxes and they are following the default order
Thus, this is a possibility:

Private Sub UserForm_Initialize()

Dim i As Long
Dim ctl As Control
For i = 1 To 4
Me.Controls("Textbox" & i) = i
Me.Controls("Textbox" & i).BackColor = vbGreen
Next i

Debug.Print Me.TextBox3.Value

End Sub

With this screenshot, showing each TextBox getting a value of 1,2,3 or 4 and a green color:

Sample Image

Or even this, if you want to make the outlook of the controls a bit different:

Private Sub UserForm_Initialize()

Dim i As Long
Dim ctl As Control

For i = 1 To 4
With Me.Controls("Textbox" & i)
.Value = i
If i Mod 2 = 0 Then
.BackColor = vbBlue
.ForeColor = vbWhite
Else
.BackColor = vbGreen
.BackColor = vbRed
End If
End With
Next i

End Sub

Sample Image

If you are naming the textboxes, following your own programming logic, then looping through the collection of controls and checking the TypeName of the control is a better solution.

C# Looping through textboxes in a GroupBox

Here's a way: It assumes there is more than one type of control in the group box. If there are only textboxes, you can simplify it.

private void button1_Click(object sender, EventArgs e)
{
int Counter = 0;

foreach (Control control in groupBox1.Controls)
{
TextBox textBox = control as TextBox;

if (textBox != null)
textBox.Text = Counter++.ToString();
}
}

How to loop through cells & userform textboxes to enter data

You only need one loop:

For i = 6 To 14         
Cells(i, 3).Value = Me.Controls("score" & (i-5)).Value
Next i

(unsure exactly where your cells are, but you get the idea)



Related Topics



Leave a reply



Submit