Solved

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

Posted on 2011-03-10
4
513 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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