Solved

how can i search textbox control on the current form in C#.net windows application ??

Posted on 2011-03-10
4
511 Views
Last Modified: 2012-05-11
i want to know that how can i search textbox control on the current form in C#.net windows application ??

without linq how can i do it ??
0
Comment
Question by:Parth48
4 Comments
 

Author Comment

by:Parth48
ID: 35093585
please refer the below code ...

private TextBox GetTextBox()
        {
            TextBox txtPasskey = new TextBox();
            txtPasskey.Name = "txtPasskey";
            txtPasskey.MaxLength = 8;
            txtPasskey.PasswordChar = '*';
            txtPasskey.TextAlign = HorizontalAlignment.Left;
            txtPasskey.Location = new Point(200, 200);
            txtPasskey.TextChanged += new EventHandler(this.txtPasskey_TextChanged);
            txtPasskey.Validating += new CancelEventHandler(this.txtPasskey_Validating);
            return txtPasskey;
        }

Open in new window


now how can i find above textbox(txtPasskey) without linq ??
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35093619
Why wouldn't you use LINQ ? If you really not want to do that, you'll need to implement a loop over all your controls

Example without linq

foreach (Control con in mainForm.Controls)
    {
        if ( control is Textbox  && control.Name = ""YourTextBox")
                 // now you have your textbox
    }


See also: http://www.dotnetperls.com/query-windows-forms
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35093786
You can also use the Old Way ;-)

TextBox MyTextBox = control.FindControl("YourTextBox") as TextBox;
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35095183
For WinForms, it would be similar:
string CtlName = "txtPasskey";
            Control[] matches = this.Controls.Find(CtlName, true);
            if (matches.Length > 0)
            {
                // ... do something with matches[0] ...
                if (matches[0] is TextBox)
                {
                    TextBox tb = (TextBox)matches[0];
                    tb.Text = "You found me!";
                }
            }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now