Check which dynamically created textbox is empty - c#

this question provides a solution for checking if any textbox on a window form is empty. My question extends to: if the textbox are dynamically created, is there a way to tell which textbox is empty and return the control (textbox)'s name?
Please advise if more info is required, thanks.
EDIT: codes: (the entire program is too large, I will add only the Text box portion)
private TextBox Department_Contact = new TextBox();
this.Department_Contact.Location = new System.Drawing.Point(lct1, lct2);
this.Department_Contact.Size = new System.Drawing.Size(s1, s2);
this.groupBox2.Controls.Add(this.Department_Contact);
This textbox loads depends on some comboBox selection; thus hardcoding
if (Department_Contact.Text == string.Empty)
is not a choice for my code
EDIT: Here's the complete answer for those looking for solution:
Department_Contact.name = "Department_Contact";
var txt = View.groupBox2.Controls.OfType<TextBox>().ToArray();
foreach (TextBox t in txt)
{
if (t.Text == "")
Console.WriteLine(t.Name);
}

What you can do, is pass all the textboxes into an array, then check each one.
var txt = this.groupbox.Controls.OfType<TextBox>().ToArray();
foreach (TextBox t in txt)
{
if (t.Text == "")
Console.WriteLine(t.Name);
}
View is a reference to the form. If you are doing this code in the form itself, you can use this instead.

Related

How to detect a control in a windows form and edit it c#

My form
So, i have this Form with a TableLayoutPanel witch contains text boxes.
I need the code to check if there is a textbox on my form and it is empty - then write text in it
else {keep looking for an existing, empty text box}
The order doesn't matter
foreach (Control contr in tableLayoutPanel1.Controls)
{
if (contr is RichTextBox)
{
if ((contr as RichTextBox).Text == string.Empty)
{
contr.Text = str_product;
}
}
}

Retrieve TextBox By Name

In my project i know the names of TextBoxes which are dynamically generated is there any solution to retrieve this TextBox text from other methods.In other sense i want to get TextBox by name and want to use in other part of code.
I have TextBox allocated like this...
private void Met(string rowNo)
{
TextBox t2 = new TextBox();
t2.Name = "itemAmt" + rowNo;
PurchaseItemEntryDyPanel.Controls.Add(t2);
}
Is there any way other than using name? Any Solution?
I personaly use name when I want to read posted data from a form.
And I would use Id when controls are supposed to be unique. So the code is a little different:
var t2 = new TextBox();
t2.ID = "itemAmt" + rowNo;
//since you mention in the comments, add it to the panel
yourPanel.Controls.Add(t2);
Then to get the textBox value
var controlId = "itemAmt" + rowNo;
var t2 = ((TextBox)(yourPanel.FindControl(controlId)));
if(t2 != null)
{
//do someting
//t2.Text = "something";
//t2.Enabled = true;
}
If you are not willing to make that change, go over the solution posted earlier.
You can get your TextBox from Controls collection of Form by it's name like this:
var myTextBox = this.Controls[textBoxName];
You don't show too much of your code, but I assume you're adding it to the collection of controls on your form. Otherwise, the TextBox you create in Met goes out of scope when your method ends, just like any other local variable.
private void Met(string rowNo)
{
TextBox t2 = new TextBox();
t2.Name = "itemAmt" + rowNo;
this.Controls.Add(t2); // need to add the TextBox to your form's controls
}
Then you can use Selman22's solution or, if the control might be added to a GroupBox or Panel, you'll want to search all child controls too:
var myControl = this.Controls.Find("itemAmt4", true);
if (myControl != null)
myControl.Enabled = true;
Use this in your Class:
foreach (Control tempCtrl in this.Controls)
{
// Determine he control is textBox1,
if (tempCtrl.Name == "itemAmt" + rowNo)
{
this.Controls.Remove(tempCtrl);
}
}

Find a textbox based on string and change its Text

I did this in C# -
foreach (Control ctl in this.groupBox3.Controls)
{
if ((ctl is Textbox) && (ctl.Name.Substring(0, 1) != "l"))
{
Textbox tmp= (Textbox)ctl;
tmp.text = "whatever";
Im trying to do something similar in WPF but this time i want to find the textbox based on a string.
So I tried
TextBox temp = (TextBox).Findcontrol("txtboxNumbers");
but it complaints that the "(Textbox)" is a type but its used like a variable and it cant find the Findcontrol method :'(
Ofcource you cannot. Doing this
(TextBox).Findcontrol("txtboxNumbers");
You try to invoke method Findcontrol on Type. Instead try (in window or control *.cs file):
TextBox oTextBox = FindName("txtboxNumbers") as TextBox;
You can find the control with this.FindControl:
TextBox txt = this.FindControl("txtboxNumbers") as TextBox;
// check if the control was found
if(txt != null)
{
txt.Text = "whatever you want";
}

What is the easy way to set all empty string textbox ("") to null value in Csharp winform?

Suppose I don't want to use
if (string.IsNullOrEmpty(textbox1.Text))
{
textbox1.Text = null;
}
for every textbox controls in form, is there a easier way to do it ?
Simple way is Loop through every control, see the below code
foreach (Control C in this.Controls)
{
if (C is TextBox)
{
if (C.Text == "")
{
C.Text = null;
}
}
}
It is one more way
foreach(Control txt in this.Controls)
{
if(txt.GetType() == typeof(TextBox))
if(string.IsNullOrEmpty(txt.Text))
txt.Text = null;
}
Hope it helps
You can iterate through the ControlCollection of the given form, e.g. frmMain.Controls
Now this will be the basic Control object, so you would need a test to see if it is of type TextBox.
.NET 2.0 - you'll have to check this manually
.NET 3.0+ - use the .OfType<TextBox> extension method to give you only a list of IEnumerable<TextBox>
Note that iterating through this from the form will only give you text boxes on that form. If you bind text boxes to a container it won't show up there.
Safest bet would be to write a recursive function that walks through all the control collections and passes the reference to your test function to perform your test and update.
Try this:
foreach(Control c in this.Controls)
{
if (c.GetType().FullName == "System.Windows.Forms.TextBox")
{
TextBox t = (TextBox)c;
t.Clear();
}
}
You can create a derived control from textbox control, and override its text property.

How to access a dynamically created text box in C# whose name is available in string?

I have a dynamically created (runtime creation) textbox whose name is available as a string.What i want to do is access this textbox like we do as normal textboxes .Can any one tell me how to cast it as textbox or any other solution
If you know the name of the textbox and its parent controls, you can do like this:
TextBox tb = (TextBox )parent.Controls["name"];
In addition to Iordan's answer, if you don't know exactly where on your form the textbox is, then this extension method should help alot. Note, Form's inherit from Control somewhere down the track too, so you can call it from that, or any control on your form.
public static class ExtensionMethods
{
public static Control FindControl(this Control root, string name)
{
foreach (Control c in root.Controls)
{
// Check this control
if (c.Name == name) return c;
// Check this controls subcontrols
Control tmp = c.FindControl(name);
if (tmp != null) return tmp;
}
return null;
}
}
If this still isn't flexible enough for you, then you can iterate over System.Windows.Forms.Application.OpenForms
Since you seem to have control over the creation process, put a reference to it in a dictionary.
TextBox txt = DynamicCreate(name);
map[name] = txt;
this.Controls.Add(txt);
All you have to do is look it up in your dictionary, instead of loop through all the controls on the form.
TextBox txt = map["name"];

Categories

Resources