Reputation: 463
I am very new to C# and am just trying to learn the basics. What I am creating is a program that reads a .txt file (log file) for 2 games I play. Everquest and a MUD. What I am wanting to do is when I load the .txt file up, it detects if its an EQ log or a MUD log and then shows the proper data fields it will begin to populate (text boxes, etc)
At first, I was going to use a groupBox and just hide/show the proper groupbox depending on which log was loaded up. However in my research I learned about UserControls.
I like the idea of loading a usercontrol into a panel so that the textbox and button to browse to the text file are always constant, then it just changes the data below them depending on which text file is loaded.
My problem is all my code is in my "form1.cs" and when I try to update code in the usercontrol, it throws errors about protection level.
My question is, the code I use to open and read the log file is a FileSystemWatcher. does this code have to be on the usercontrol.cs file or is it okay to be on the form1.cs file and if its okay to be on the form1.cs file how do I populate text boxes or any other types of fields on the usercontrol?
I was trying:
myControl.textbox1.Text = "Test";
I was doing this from the form.cs code and after it read a line of the log and it matched a certain phrase, but it just says the protection level is wrong and wont work. I defined everything on the usercontrol as public but obviously I'm not thinking about this properly.
do I have to somehow "include" the usercontrol.cs on the form1.cs? I have been trying to research this but I can't find anything that helps me so either a) its real obvious and I'm just not getting it or b) i'm doing it totally wrong and not understanding at all.
maybe groupBox's are best for what i'm trying to do? are usercontrols designed to work independently with their own code and not as things that get turned on and off?
I come from a PHP/Web background so i'm trying to think of this like how i'd turn DIV's visible and not visible depending on what was happening. maybe usercontrols don't work like that and are not supposed to be interacted with by the main form and are supposed to have their own code?
I can provide code if needed but this is more of a understanding the concept problem rather than a code problem. I can probably figure out the code once I understand how its supposed to be acting.
EDIT: Here is some code showing what I'm trying to learn. I've simplified it to not include the FileWatcherSystem because the same error is generated for both.
I have the form1.cs and inside that i'm defining my UserControl2 which is just a usercontrol box and a text box named "textBox1"
UserControl2 myControl = new UserControl2();
This control is loaded into panel1 which is on form1.cs. I also have a button on form1.cs that when I click it, I am trying to fill the textBox1 with the word "test"
The code looks like this:
private void button1_Click(object sender, EventArgs e)
{
myControl.textBox1.Text = "Test";
}
in this scenario, "textBox1" from above is underlined red and the error when you hover over it is:
"UserControl2.textBox1" is inaccessible due to its protection level.
I can use the button to run the following command successfully:
panel1.Controls.Clear();
and that will clear the pre-loaded control (myControl) out of the panel so I believe i'm starting to understand how to make controls appear/dissappear inside panels but I still can't get the form1.cs code to interact with the controls inside the UserControl2.cs file.
Upvotes: 0
Views: 108
Reputation: 181
Make sure textBox1 within your user control has the "public" access modifier rather than "private" or "protected" where it is defined.
To help with the concept bit, your User Control is its own class. textbox1 is a member of this class, thus only your class will be able to access it unless it is marked as public. (If it's protected then classes that inherit from your user control can also access it but that's not really relevant here.)
Upvotes: 1