Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find textbox in form in general function like
and textbox like
txtbarcode1
txtbarcode2
txtbarcode3
txtbarcode4
txtbarcode5
txtbarcode6

C#
private void lookControl()
{
      for(int i=1;1<=12;i++)

           {

                //// find control here with ("txtbarcode"+i) //// 

           }
} 


if anyway plz send me code


thanks you
Mohit
Posted
Updated 11-Aug-12 0:32am
v3
Comments
Kenneth Haugland 11-Aug-12 6:30am    
First, do you have any suggestions?
mohit.a44 11-Aug-12 6:35am    
i updated my qus..
Kenneth Haugland 11-Aug-12 6:40am    
use a foreach and loop through all the controls in your form, then do something when the control is a TextBox :)
Akinmade Bond 28-Aug-12 13:47pm    
A for loop seems pretty tedious for such an easy action. Attempt using the foreach loop, except there is any special reason you haven't mentioned that makes you want to do otherwise.

There is a code:

C#
public void LookControl()
{
    foreach (Control control in this.Controls)
    {
        if (control is TextBox)
            // You can check any other property here and do what you want
            // for example:
            if ((control as TextBox).Text == string.Empty)
                ;//Action
    }
}


Let me know if there is something else in your mind.
 
Share this answer
 
Comments
Kenneth Haugland 11-Aug-12 6:45am    
That would do the job. 5!
mohit.a44 11-Aug-12 6:47am    
i know this code but i want use for loop from 1 to 12 because my textbox name is same name txtbarcode only differ by count like txtbarcode1,txtbarcode2
so i want use for loop
Kenneth Haugland 11-Aug-12 6:56am    
You could use his code, just check the .Name property of the textbox...
[no name] 11-Aug-12 16:24pm    
If you can't write a simple for loop then you need to be reading a basic book on programming.
mohit.a44 11-Aug-12 6:59am    
i want for loop
it should be Work guys
C#
 public void LookControl()
       {
for (int i = 1; i <=12; i++)
           {
               MessageBox.Show(((TextBox)(this.Controls[("txtbarcode" + i.ToString())])).Text);
           }

       }

regards
sarva
 
Share this answer
 
v3
Comments
mohit.a44 11-Aug-12 7:30am    
for (int i = 1; i <= 12; i++)
{
// execute the array here



string tval = ((TextBox)Controls["txtbarcode" + i]).Text;
}


i got erorr

Object reference not set to an instance of an object.
Sarrrva 11-Aug-12 7:41am    
hi mohit.a44!!

this Answer is right. please review your Code line by line

regards
sarva
mohit.a44 11-Aug-12 7:42am    
No i am getting error "Object reference not set to an instance of an object."
mohit.a44 11-Aug-12 7:34am    
my textbox's on form that is (txt12barcode,txtquatity,txtbarcode1......12) i want only txtbarcode1......12
Sarrrva 11-Aug-12 8:36am    
This is Your Exact Answer mohit.a44 !!!
private void button3_Click(object sender, EventArgs e)
{
string name = "txtbarcode";

for (int i = 1; i <= 12; i++)
{
Control ctn = this.Controls[name+i];
MessageBox.Show(ctn.Text);
}
}

was this use ful?


Regards
sarva
All of you i got a right answer that is

C#
for(int i=1;i<=12;i++)
{
Control[] ctrls1 = this.Controls.Find("txtbarcode" + i, true);

      string a=ctrls1[0].Text;
}
 
Share this answer
 
Comments
Sarrrva 29-Aug-12 0:55am    
Good Job mohit
C#
private void lookControl()
{
      for(int i=1;i<=12;i++)

           {
  
//// find control here with ("txtbarcode"+i) ////

                Directcast(this.controls.find("txtbarcode" + i.ToString(),True).getvalue[0],Textbox).Text="";
              
           }
}


Happy Coding!
:)
 
Share this answer
 
This is my last Solution:


C#
public void LookControl()
       {
           foreach (Control ctrl in this.Controls)
           {
               if (ctrl.GetType().ToString() == "System.Windows.Forms.TextBox")
               {
                   MessageBox.Show(((TextBox)(ctrl)).Text);
               }
           }
       }



otherwise use this solution

C#
public void LookControl()
        {
 for (int i = 1; i <=12; i++)
            {
                MessageBox.Show(((TextBox)(this.Controls[("txtbarcode" + i.ToString())])).Text);
            }

        }





regards
sarva
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900