C# referencing components in interface class
I'll start with this: I'm no good at C#. I don't for the most part know what I'm doing.
Now the disclaimer is out the way, I'm trying to access a TabControl in my Interface (made using VS2008, so parts are in Interface.Designer) from another class - except using Interface.tabControl1
tells me i need an object reference, and if I type .
after Interface
I just get a list of stuff, not any of the objects within the interface. Using Interface.ControlCollection
doesn't help either.
Help is 开发者_运维知识库appreciated, and I'm sure this is really basic and I'm just a spanner, but oh well.
EDIT: in Program.cs
(which i would guess was autocreated when I started building the app), the interface is created nameless:
static void Main()
{
Application.Run(new Interface());
}
Interface()
is called from Interface.cs
:
public partial class Interface : Form
{
public Interface()
{
InitializeComponent();
}
}
The rest of the partial class is created in Interface.Designer.cs
- which was autogenerated when I was building the interface in the VS2008 designer.
I'm not sure if that helps, or is of no use. But there it is anyway. =)
EDIT #2:
I think i should explain what I'm doing, maybe then people will understand what I need out of this:
I had an interface, with a tabcontrol, and tabs generated on-the-fly based on whatever the user was doing. This was causing problems because i wasn't making each tab its own object, so when it came to accessing stuff in the pages without affecting other tabs, it didn;t work. As such, i removed all the code from a method in Interface.cs
that was used to build a tab, and made a new class called Tab.cs
- This is where the problem of broken references appeared. Relevant code, moved from Interface into Tab:
public Tab(string inputstring)
{
.....
//call this method if button is clicked
cancel.Click += new System.EventHandler(this.close_Click);
//ridiculous calculations to get the buttons in the right place
int leftside = ((tabControl1.Width / 100) * 96);
int bottomside = ((tabControl1.Height / 100) * 93);
int bottomside2 = ((tabControl1.Height / 100) * 99);
.....
//call this method if button is clicked
save.Click += new System.EventHandler(this.save_Click);
.....
//call this method when tab is entered
newtab.Enter += new System.EventHandler(this.tabSelect);
}
Of particular relevance is the code with tabControl1.Width. This then leads back to what I started trying to ask about before.
I hope that maybe makes things clearer.
EDIT3: (for Henk Holterman) I've renamed Interface to MainForm. VS2008 decided it should be called interface, I simply didn't change that. But I see why change is good, thank you =P
Looks to me like Interface is the name of the class, and you have an instance of that class. You need to find out what the instance is called (let's say myInstance), then you should be able to call myInstance.tabControl1.
Hope this helps, and thanks for a rare use of the word 'spanner' on this site.
David
tabControl1 is a control created in the Designer-part of your main form (in Interface.designer.cs). So tabControl1 refers to an instance field. You will have to give your Tab class in Tab.cs your instance of the main form (such as in the constructor) so you can then reference that. Also, you have to check that those fields are public/internal so you can access them.
If you are creating code in the Interface.cs
file, then instead of typing Interface
, type this
to reference the actual object you are using.
If not, then when you call the other class from Interface
, pass it a Form
object type, with the actual reference being this
.
To make it work, change your method to be:
public Tab(string inputstring, MainForm formInstance)
{
.....
//call this method if button is clicked
formInstance.cancel.Click += new System.EventHandler(formInstance.close_Click);
//ridiculous calculations to get the buttons in the right place
int leftside = ((formInstance.tabControl1.Width / 100) * 96);
int bottomside = ((formInstance.tabControl1.Height / 100) * 93);
int bottomside2 = ((formInstance.tabControl1.Height / 100) * 99);
.....
//call this method if button is clicked
formInstance.save.Click += new System.EventHandler(formInstance.save_Click);
.....
//call this method when tab is entered
formInstance.newtab.Enter += new System.EventHandler(formInstance.tabSelect);
}
Then to call this:
// (somewhere in MainForm.cs)
Tab("MyString", this);
精彩评论