Frage

I'm attempting to make a sort of AI that is going to gather information from my web server.

I have a Button that toggles the AI on and off and A couple of methods that pass around args to gather information. When the ai is powered on it passes an event system I made with an event called powerOn. Im trying to set a richtextbox to say hello or something like that but the text box isn't updated when its told to

Program Class with the main method:

namespace Universe_AI
{
public static class Program
{
    public static Boolean aiRunning = false;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    public static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public static void writeMemory(string header, string value)
    {

    }

    public static void readMemory()
    {

    }

    public static void AiProccess(string pType, String[] pArgs)
    {
        if (pType == "event")
        {
            string pEvent = pArgs[0];
            aiEvent(pEvent);
        }
    }

    public static void aiEvent(string pEvent){
        if (pEvent == "powerOn")
        {
            Form1 ele = new Form1();
            ele.Mind.Text = "test";
            ele.Mind.AppendText("Are you my Creator?");
        }
    }
}
}

Form1 Class

namespace Universe_AI
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (Program.aiRunning == false)
        {
            Program.aiRunning = true;
            label2.Text = "ON";
            String[] eventArgs = new String[] {"powerOn"};
            Program.AiProccess("event", eventArgs);
        }
        else
        {
            Program.aiRunning = false;
            label2.Text = "OFF";
            Mind.Text = "";
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Mind.Text = "test";
    }
}
}

The richtextbox witch is called Mind is set to public and doesn't return errors. The test button updates it but when trying to access it from another class doesn't seam to work

War es hilfreich?

Lösung

This line:

Form1 ele = new Form1();

Creates a new form.. and everything under that is also new. That means you have a new, completely separate form with its own RichTextBox sitting in memory. That is what you are appending text to.

What you need to do, is pass the instance of the form you're currently working with.. in. Read the comments here:

// Add the Form as an argument at the end --------------->  ___here___
public static void AiProccess(string pType, String[] pArgs, Form1 form)
{
    if (pType == "event")
    {
        string pEvent = pArgs[0];
        aiEvent(pEvent, form); // pass it in
    }
}

public static void aiEvent(string pEvent, Form1 form){
    if (pEvent == "powerOn")
    {
        // use the "form" variable here
        form.Mind.Text = "test";
        form.Mind.AppendText("Are you my Creator?");
    }
}

Read the comments in the code. You can then pass the current instance in like this:

String[] eventArgs = new String[] {"powerOn"};
Program.AiProccess("event", eventArgs, this); // <---- pass "this"

Andere Tipps

This is referencing a completely separate instance of Form1 than the one the user sees:

public static void aiEvent(string pEvent)
{
    if (pEvent == "powerOn")
    {
        Form1 ele = new Form1();  // new instance, unrelated to the form displayed
        ele.Mind.Text = "test";
        ele.Mind.AppendText("Are you my Creator?");
    }
}

You're creating a local instance that goes out of scope when the if block ends.

At the very least, you'll have to pass a reference to the current Form1 instance through the other methods (AiProccess and aiEvent), in order to access the current RichTextBox.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top