473,394 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to access form elements from outside the form class

28
I want to change the text of a textfield of a c# form object, but from outside that class. How can I do it?
By passing that form field as a reference? How can I pass a reference of that form or textfield to that function if I am running that function in a separate thread?

Thanx
Mar 13 '09 #1
8 29245
PRR
750 Expert 512MB
I would rather keep UI and logic separate.... its better that way....
as for your question

a. You can pass as parameter the control id which you intend to change
Expand|Select|Wrap|Line Numbers
  1. public void ChangeText(System.Windows.Forms.Button t)
  2.        {
  3.            t.Text = "Bytes";
  4.        }
  5.  
  6. Class my = new Class();           
  7.  
  8.             my.ChangeText(this.button1);
  9.  
  10.  
b. Or you could use Controls.Find method ...or you can loop through the controls in your class method ... You should give the form object as input method ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void ChangeSomething(System.Windows.Forms.Form myForm)
  3.        {
  4.  
  5.            System.Windows.Forms.Control[]  bb = myForm.Controls.Find("button1", true);
  6.  
  7.            foreach (System.Windows.Forms.Control c in bb)
  8.            {
  9.                if (c.Name == "button1")
  10.                {
  11.                    c.Text = "CCC";
  12.                }
  13.            }
  14.  
  15.            //OR
  16.  
  17.            foreach (System.Windows.Forms.Control c in myForm.Controls)
  18.            {
  19.                if (c.Name == "button1")
  20.                {
  21.                    System.Windows.Forms.Button b = c as System.Windows.Forms.Button;
  22.                    //b.Text = "Changed from class";
  23.                }
  24.            }
  25.        }
  26.  
In your form.cs on button click, you can call
Expand|Select|Wrap|Line Numbers
  1. Class my = new Class();
  2.             my.ChangeSomething(this);
  3.  
Mar 13 '09 #2
aatif
28
How can I check that the control which I am looking for exists in the 'Controls' list? The foreach loop fails to search by name.
Mar 13 '09 #3
tlhintoq
3,525 Expert 2GB
When you make the control on the target form, give it a meaningful name that you can look for from the other class.
Mar 13 '09 #4
Bassem
344 100+
Don't forget,
If you're going to access a control from another thread than the one the control is created on, you'll face the cross-thread exception.
Check it by using IsInvokeRequired property!
Mar 13 '09 #5
You can use a delegate to get access to a control that is in another thread:

Expand|Select|Wrap|Line Numbers
  1. // This is an extract from a server/client socket program
  2.  
  3. delegate void SetTextCallback(String Text);
  4.  
  5. private void SetText(String Text)
  6. {
  7.     if (textBox1.InvokeRequired)
  8.     {
  9.         SetTextCallback d = new SetTextCallback(SetText);
  10.         this.Invoke(d, new object[] { Text });
  11.     }
  12.     else
  13.     {
  14.         this.textBox1.Text = this.textBox1.Text + Text + "\r\n";
  15.     }
  16. }
  17.  
  18. public void OnDisconnect(IAsyncResult asyn)
  19. {
  20.     ((Socket)asyn.AsyncState).EndDisconnect(asyn);
  21.     SetText(clientSocket.RemoteEndPoint.ToString() + " has disconnected!");
  22. }
  23.  
I hope this helps!!

Alex G.
Mar 13 '09 #6
aatif
28
Alex,
Sorry I couldn't get it, do you have an example?

Bassem,
where is IsInvokeRequired...?
Mar 16 '09 #7
aatif
28
ok, I am done.

Expand|Select|Wrap|Line Numbers
  1. public void UpdateText()
  2. {
  3.     Control[] ctrlResponse = isoAgentForm.Controls.Find("textStatus", true);
  4.     if (!ctrlResponse[0].InvokeRequired)
  5.     {
  6.         ctrlResponse[0].Text = statusMsg;
  7.     }
  8.     else
  9.     {
  10.         ctrlResponse[0].Invoke(new ThreadStart(UpdateText));
  11.     }
  12. }
I also changed the textfield from private to public.



Any good suggestion?
Mar 16 '09 #8
Most easy way is to change your element private to public on projects.cs

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public System.Windows.Forms.Label label1;
Oct 26 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Richard Cornford | last post by:
I am interested in hearing opinions on the semantic meaning of FORM (elements) in HTML. I have to start of apologising because this question arose in a context that is not applicable to the...
5
by: TheFerryman | last post by:
How bad is it to include a non const accessor to a container of objects? For instance template <class WheelType> class CarBase { typedef std::list<WheelType*> WheelList; private:
10
by: tom | last post by:
Hey All, While running some code, I'd like to open a form as a dialog box (so that the code will wait for the form to be closed/hidden -- easy to do using the acDialog argument), but I would...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
12
by: Tobias Froehlich | last post by:
Hi, I have a form called form1 on which there are a lot of checkBoxes (they form a 8x6 field). I'd like to create a class in a seperate .cs file which gives some control over these checkBoxes...
2
by: authorking | last post by:
How to access a control in another form and how to make a form become a MDI container.
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
5
by: wpmccormick | last post by:
What is the cleanest way to gain access to object methods and properties across classes and files in the same namespace? Example: A form object frmForm in file frmForm.cs creates obj1 defined in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.