Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I have a form with a tab control with multiple tabs, and I would like to put the code of each tab in a diferent file (partial classes of the form class); I tried to do that but I can not access the objects of the mais class /form from the other files. Any one knows why ? thanks

Here is a sample of the code (Form):
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimplyApp
{
  public partial class frmPlanning : Form
  {
    public frmPlanning()
    {
      InitializeComponent();
      //lstvOutputs_Circuits.ColumnWidthChanging += new ColumnWidthChangingEventHandler(lstvOutputs_Circuits_ColumnWidthChanging);
    }
    SQLutils mySQL = new SQLutils();
    static byte Actuator_NumOutputs = 0;
    int prjActuatorId = 0;
    string CircuitStatus = "";
    string ActuatorStatus = "";
    public void frmPlanning_Load(object sender, EventArgs e)
    {
      Init();
    }
 
    private void Init()
    {
      mySQL.SetConn();  
      tabOptions.Visible = false;

      cboProjectCodes.Items.Clear();
      tabOptions.SelectTab(0);
      FillModuleTypes();
      FillPanelColors();
      FillImageList();
      dgvPrjCircuits.BackgroundColor = this.BackColor;
      dgvPrjActuators.BackgroundColor = this.BackColor;
      tabAct_dgvActuatorSelection.BackgroundColor = this.BackColor;
      dgvPrjRelActuatorOutputs_Circuits.BackgroundColor = this.BackColor;
      dgvPrjPanelPrgs.BackgroundColor = this.BackColor;
      dgvPrjActuatorsPrgs.BackgroundColor = this.BackColor;
      AssignImgsToTs();
      cont_Planeamento.SplitterDistance = 425;
    }

    private void tsPrj_Select_Click(object sender, EventArgs e)
    {
      if (cboDivisions.Text != "")
      {
        cboProjectCodes.Enabled = false;
        cboPlans.Enabled = false;
        cboDivisions.Enabled = false;
        tabOptions.Visible = true;
        RefreshDgvPrjCircuits();
        dgvPrjCircuits.ClearSelection();
        RefreshDgvPrjActuators();
        dgvPrjActuators.ClearSelection();
        TagUsedCircuits();
      }
    }

other file code ......

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SimplyApp
{
  public partial class frmPlanning
  {
   //I do not have any access to any control in the form ... why ?
  }
}



Thanks
Posted
Updated 8-Mar-11 6:39am
v3
Comments
Albin Abel 8-Mar-11 12:49pm    
Check any bugs in your main form

Those controls were declared already in the other partial class pages. You cannot access them in a declaritive context, but you can in a functional one.
C#
public partial class frmPlanning
  {
   //I do not have any access to any control in the form ... why ?
   // cannot do anything with cboProjectCodes here, must put in a funtion
   public void Test()
   {
       cboProjectCodes.Items.Clear();
   }
  }

Remember that all pages of the partial class are just continuations of each other, so you cannot just use variables/contraols that have been declared, but rather must put them in functions. You can also declare new variables that do not exist on other pages. Same with methods, you can call them from inside another method or overload the existing.
 
Share this answer
 
Comments
generalcosta 8-Mar-11 15:10pm    
thanks it works fine ..
Sergey Alexandrovich Kryukov 8-Mar-11 15:16pm    
Of course.
--SA
JOAT-MON 8-Mar-11 15:22pm    
I'm glad it helped. Thank you for accepting as answer. :)
Sergey Alexandrovich Kryukov 8-Mar-11 15:21pm    
You got my 5, but this is because I beat OP until some code sample was finally posted... :-)
Everyone before you advised blindfolded...
Cheers,
--SA
JOAT-MON 8-Mar-11 15:27pm    
Lol, thanks! I hope your hand is okay... :P
You should be able to access all methods within your partial class.

You will not be able to access main form methods in other files as your methods appear to be all private.
 
Share this answer
 
Comments
Albin Abel 8-Mar-11 12:48pm    
I guess, partial classes have access to private methods whichever the file is.
Sergey Alexandrovich Kryukov 8-Mar-11 12:54pm    
...whichever the class has. It's not related to the file except different "uses" clauses.
--SA
Sergey Alexandrovich Kryukov 8-Mar-11 12:55pm    
Yes, Abhinav, if you're discussing the OP subject, partial class declaration, you last sentence is incorrect. I would advice to fix it or re-formulate to make it clear.
--SA
generalcosta 8-Mar-11 12:57pm    
thanks
Anyway even if I put them public I´m not able to access them neither the form controls from the new file
Sergey Alexandrovich Kryukov 8-Mar-11 14:06pm    
As I say, this is not about "public" (by the way, don't use "public" when "internal" works). Show some code sample, really... (Please, don't post as Answer, use "Improve quesion").
--SA
I don't know because you don't show the code, but partial class implementation cannot cause any problem in principle. The partial class declarations behave precisely as if they were in one file.

Rather, you need to check up "using" clauses where you have a problem and add what's missing.

—SA
 
Share this answer
 
Comments
rigamonk 8-Mar-11 13:37pm    
Do you mean your Intellisense won't work, or it won't compile? If it won't compile, whats the error?
Are these class file under the same assembly?
I know these may seem like elementary questions, but, as SAKryukov noted, partial class implementation is very straightforward.
Every form you create using the forms template is implemented as a partial class (e.g. Form1.cs and Form1.Designer.cs
Sergey Alexandrovich Kryukov 8-Mar-11 14:04pm    
You probably misplaced your comment, it's not for me :-)
--SA
Abhinav S 8-Mar-11 23:48pm    
Good answer.5.
Sergey Alexandrovich Kryukov 9-Mar-11 0:26am    
Thank you,
It was before we got some code...
--SA

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