-2

How can I use my UI form controls from another class? I want to work with these controls in another class, then call it into my main form. The problem is that the new class I created can't seem to access the label or the textboxes of these controls and I keep getting an error.

How would i solve this issue?

Error Messages:

FirstName_text is inaccessible due to its protection level

First_Name_label is inaccessible due to its protection level

Color does not exist in the current context

public void Checker() 
{
    //First Name Validation
    if (Regex_Static_Class.FirstNameRgex.IsMatch(FirstName_text.Text) == false)      
    {
        First_Name_label.Text = "invalid first name";
        Error_Lable.ForColor = Color.Pink;     
    }
}

3 Answers 3

0

I guess First_Name_label is a UI label and it must be accessible form your other class too.

Maybe make a setter method to fill in text into your label.

0

I would say that you have to set Modifiers property for your controls to public or internal in Forms Designer to access it from another class. Control's instance is protected by default.

That's the answer to your question. Another thing is that it's not the best idea to do that. You shouldn't access form's controls outside of form directly. Form class should encapsulate its controls and expose an interface to change them, for example.

Also Color doesn't exists most probably because you don't have proper using in your another class.

It has nothing to do with conditions (references to conditions removed from the original question).

0

Use FormCollection to perform a iteration

Use FormCollection to hold all present and future open forms, these open forms can be accessed via Tag Name. Lest say the form to be accessed is Form1 and the textbox and labels tag names are "FirstName_text" and "First_Name_label".

// All open forms in your application will be placed in fcOpenForms.  
FormCollection fcOpenForms = Application.OpenForms;

// Perform iteration for the form to be accessed by it's tag name.  
foreach(Form fOpenForm in fcOpenForms.OfType<Form>().ToList()) {

  if(fOpenForm.Tag != null && fOpenForm.Tag.Equals("Form1")) {
   
  //Now iterate for the textbox tag name.
  //
   foreach(TextBox tFirstNameTextBox in fOpenForm.Controls.OfType<TextBox>().ToList()) {

    if(tFirstNameTextBox.Tag != null && tFirstNameTextBox.Tag.Equals("FirstName_text")) {

    //Now iterate for the label's tag names.
    //
    foreach(Label lFirstNameLabel in fOpenForm.Controls.OfType<Label>().ToList()) {

     if(lFirstNameLabel.Tag != null && lFirstNameLabel.Tag.Equals("First_Name_label")) {

      // You can now access these two controls "FirstName_text" and 
      // "First_Name_label" by calling it's variable name not the controls name.
      //
        tFirstNameTextBox.Text = "Your Text Here";
       
        lFirstNameLabel.Text = "invalid first name";

       // Any other control can be accessed using the same technique. You can either iterate
       // for "Error_Label" here or up under where "First_Name_label" was accessed, it depends
       // on where you see fit, to suite your needs at the time of accessing these controls in
       //your application.

         foreach(Label lErrorLabel in fOpenForms.Controls.OfType<Label>().ToList()) {

          if(lErrorLabel .Tag != null && lErrorLabel .Tag.Equals("Error_Lable")) {

           lErrorLabel.ForColor = Color.Pink;           
        }
       }
     }
    }
   }
  }
 }
}

If any control resides in a child container of the form being accessed, then that child container will have to be accessed respectively, before the controls can be accessed, for instance a groupbox e.g. foreach(GroupBox gGroupBox in fOpenForms) and then foreach(Control cChildControl in gGroupBox.Controls) etc. Some properties of a control being accessed in this manner will have to be casted, for example to Cast ((TextBox)tFirstNameTextBox).Text = "Your Text Here";

Hope this helps ...

Not the answer you're looking for? Browse other questions tagged or ask your own question.