Public needs more public!

Public needs more public!

Postby joppiesaus » 19 Jun 2013, 10:46

So I have a little problem.
Everything I put in the mainGui class, will not become visible, because it is an overide thinggy or something.
So, I need the textboxes, timers, and all the other stuff visible for the other classes!
So I created a class, called stuffWithGui.

now, in the mainGUI class I have this:
Code: Select all
public stuffWithGui g;


But that doesn't becomes visible for the other classes, and I need that(of course, it doesn't become visible, but I just want something so I can see my textboxes, timers, and all the stuff from the mainGui class in other classes).

Here's the source:
Code: Select all
using System;
using System.IO;
using System.Windows.Forms;


namespace MCDzienny.Plugins
{
    public class Plgn : Plugin
    {
        versionInfo info = new versionInfo("Repeater", "A simple text repeater for all your daily messages.(test plugin)", "joppiesaus", "1.0", 1);
        System.Windows.Forms.UserControl gui = new mainGUI();

        public override string Description
        {
            get { return info.descr; }
        }

        public override string Author
        {
            get { return info.auth; }
        }

        public override string Name
        {
            get { return info.name; }
        }

        public override System.Windows.Forms.UserControl MainInterface
        {
            get { return gui; }
        }

        public override string Version
        {
            get { return info.ver; }
        }

        public override int VersionNumber
        {
            get { return info.verNumber; }
        }

        public override void Initialize()
        {
            while (1 == 1)//infinite loop. Checks if  the timer said yes.
            {
               
                Player.GlobalMessage("");
            }
        }

        public override void Terminate()
        {
            // The clean up code. It may be include closing timers, ending threads, disposing
            // unmanaged resources, also, saving changes to a database or a config file.
        }
    }

    public class versionInfo
    {
        public string name { get; set; }
        public string descr { get; set; }
        public string auth { get; set; }
        public string ver { get; set; }
        public int verNumber { get; set; }
        public versionInfo(string nameOfAssembly, string description, string author, string version, int versionNumber)
        {
            name = nameOfAssembly;
            descr = description;
            auth = author;
            ver = version;
            verNumber = versionNumber;
        }
    }

    public class stuffWithGui
    {
        public Timer timer;
        public TextBox msgTextBox;
        public TextBox intervalTextBox;
        public stuffWithGui(Timer timerOfTextbox, TextBox text1, TextBox interval)
        {
            timer = timerOfTextbox;
            msgTextBox = text1;
            intervalTextBox = interval;
        }

        /// <summary>
        /// set the interval of the timer
        /// </summary>
        /// <param name="interval">interval of the timer. Int</param>
        /// <param name="turnOn">Turn on the timer? Bool.</param>
        public void setInterval(int interval, bool turnOn)
        {
            timer.Interval = interval;
            if (turnOn)
            {
                timer.Enabled = true;
            }
        }

        /// <summary>
        /// turn the timer on or off.
        /// </summary>
        /// <param name="onOff">True or false?</param>
        public void enableTimer(bool onOff)
        {
            if (onOff)
            {
                timer.Enabled = true;
            }
            else
            {
                timer.Enabled = false;
            }
        }

        public void timer_Tick(object sender, EventArgs e)
        {
            displayText();
        }

        /// <summary>
        /// display text. dontDoThat is optional.
        /// </summary>
        /// <param name="dontDoThat">dont do it!</param>
        /// <returns>bool</returns>
        public bool displayText(bool dontDoThat = false)
        {
            if (!dontDoThat)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string stringOfmsgTextBox;
        private void msgTextBox_TextChanged(object sender, EventArgs e)
        {
            stringOfmsgTextBox = msgTextBox.Text;
        }
    }

    #region gui
    public partial class mainGUI
    {
        public stuffWithGui g;
        public mainGUI()
        {
            InitializeComponent();
            g = new stuffWithGui(timer, msgTextBox, intervalTextBox);
        }
    }
    partial class mainGUI : UserControl
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.msgTextBox = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.intervalTextBox = new System.Windows.Forms.TextBox();
            this.timer = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            //
            // msgTextBox
            //
            this.msgTextBox.Location = new System.Drawing.Point(68, 13);
            this.msgTextBox.Name = "msgTextBox";
            this.msgTextBox.Size = new System.Drawing.Size(204, 20);
            this.msgTextBox.TabIndex = 0;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(50, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Message";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 40);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(42, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Interval";
            //
            // intervalTextBox
            //
            this.intervalTextBox.Location = new System.Drawing.Point(68, 37);
            this.intervalTextBox.Name = "intervalTextBox";
            this.intervalTextBox.Size = new System.Drawing.Size(204, 20);
            this.intervalTextBox.TabIndex = 3;
            // iodjfaopijasdpoifhasdpfoihasdpfoihasdf
            // mainGUI
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.intervalTextBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.msgTextBox);
            this.Name = "mainGUI";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public System.Windows.Forms.TextBox msgTextBox;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        public System.Windows.Forms.TextBox intervalTextBox;
        public System.Windows.Forms.Timer timer;
    }
#endregion
}
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Public needs more public!

Postby Conor » 19 Jun 2013, 20:41

When I try to access the labels and such in my main GUI class from another class, I do two things.

Firstly, I create a static object of the form in the mainGUI form, so like:
Code: Select all
public static mainGUI currentForm;

public mainGUI()
{
   InitializeComponent();
   currentForm = this;
}


So now the form which is loaded is assigned to this static mainGUI object so you can access it as:
Code: Select all
mainGUI.currentForm.VARIABLE


However, you can't access private variables like this, which is what the default modifier is for the components of the form. I use the 'internal' modifier in this case rather than the 'public', as it enables a group of components to cooperate in a private manner without being exposed to the rest of the application code.

So, for a timer as an example:
Code: Select all
internal System.Windows.Forms.Timer timer;


Finally, now you should be able to call this from another class.
Code: Select all
public class otherClass
{
   mainGUI.currentForm.timer.Start();
}


This is how I've always done it, but I'm not sure about its reliability as such, and I could be doing something stupid, but hey, it works ;)

I hope that helps you, if it doesn't work or you were looking for something else, get back to me.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Public needs more public!

Postby joppiesaus » 20 Jun 2013, 10:01

Thank you Connor!
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Public needs more public!

Postby joppiesaus » 20 Jun 2013, 11:02

It doesn't work Connor, I think it is because it is a UserControl, and everything you put in that class, will be ignored sorta.
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Public needs more public!

Postby dzienny » 20 Jun 2013, 11:52

Here's an example how to access these fields:
Code: Select all
public override void Initialize()
{
    mainGUI mg = (mainGUI) gui;

    // Start a timer.
    mg.timer.Start();

    // Set a text of msgTextBox.
    mg.msgTextBox.Invoke(new Action(() => mg.msgTextBox.Text = "My text"));
}


Alternatively you can change
System.Windows.Forms.UserControl gui = new mainGUI();
to
mainGUI gui = new mainGUI();
and then you can directly access gui object, for example:
gui.timer.Start();

Mind that you have to use Control.Invoke to change controls states from a thread other than the one these controls were created on. You should read more about it, for example there's a fairly good article Threading in .NET and WinForms.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Public needs more public!

Postby joppiesaus » 20 Jun 2013, 13:49

dzienny wrote:Here's an example how to access these fields:
Code: Select all
public override void Initialize()
{
    mainGUI mg = (mainGUI) gui;

    // Start a timer.
    mg.timer.Start();

    // Set a text of msgTextBox.
    mg.msgTextBox.Invoke(new Action(() => mg.msgTextBox.Text = "My text"));
}


Alternatively you can change
System.Windows.Forms.UserControl gui = new mainGUI();
to
mainGUI gui = new mainGUI();
and then you can directly access gui object, for example:
gui.timer.Start();

Mind that you have to use Control.Invoke to change controls states from a thread other than the one these controls were created on. You should read more about it, for example there's a fairly good article Threading in .NET and WinForms.


Surprised! It works. But this: System.Windows.Forms.UserControl gui = new mainGUI(); doesn't work. But that's OK because mainGUI mg = (mainGUI) gui; works.
Thank you, and also Conor, I spelled your name wrong :P
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Public needs more public!

Postby joppiesaus » 20 Jun 2013, 19:05

I figured it out.
I had made a form instead of a usercontrol, so I generated a new userconrol and what dzienny said, didn't work.
So this was the problem all the time:
Code: Select all
        private System.Windows.Forms.TextBox msgTextBox;
        private System.Windows.Forms.TextBox intervalTextBox;
        private System.Windows.Forms.Timer timer;
        private System.Windows.Forms.Label lbl1;
        private System.Windows.Forms.Label lbl2;
        private System.Windows.Forms.Button startButton;
        private System.Windows.Forms.Button stopButton;

this is always at the end of the generated code.
Replace everything to public, and you can acces it using dzienny's trick. :D
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Public needs more public!

Postby Conor » 20 Jun 2013, 20:30

joppiesaus wrote:It doesn't work Connor, I think it is because it is a UserControl, and everything you put in that class, will be ignored sorta.


Yeah I had a hunch it may not, I don't know if I gave you good information, I just use that trick when I am coding things with multiple forms and classes in a normal project.

And don't worry about the name, its a common mistake ;)
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Public needs more public!

Postby dzienny » 20 Jun 2013, 21:00

joppiesaus wrote:I had made a form instead of a usercontrol

Well, the plugins support only a UserControl, and not a Form.The method works for UserControl. Just make sure that the fields you want to access are public. I haven't mentioned about that before, because I noticed you already set some fields access modifiers to public.

@Conor
The method that you described will also work, but it creates a public static instance of the UserControl and it's best to be avoided, because it's more error prone than the other method.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Public needs more public!

Postby Conor » 20 Jun 2013, 21:07

dzienny wrote:
joppiesaus wrote:@Conor
The method that you described will also work, but it creates a public static instance of the UserControl and it's best to be avoided, because it's more error prone than the other method.


Oh okay, that's interesting.

I thought that every time you create a new object of a class, it will have all of the variables with the starting/default values though.

So I would have thought that when you create a new object for the form like 'mainGUI m = new mainGUI();', if you called its timer 'm.timer1.Start();' then it would be for a different object of the form, not for the one that the user is focusing on (displayed).

I don't know if that makes any sense at all, but where am I wrong in my thinking? I must be missing something as the way you described it obviously works, but I thought you couldn't do that. I'm confusing myself anyway :lol:
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Next

Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 6 guests

cron