0

I want to modify something in Mission Planner and I want to change button from another class/form in C# in this function :

public void CloseAllConnections()
{
    myButton1.Text = "Disconnecting all";

    ... 
}

function that is located in :

namespace MissionPlanner
{
    public partial class MainV2 : Form
    {
        ...
    }

    ...
}

the idea is that everything works perfectly when i am focused on that menu, but sometimes i get a error

i even made a function like this

public MyButton GetMyButton1 { get { return myButton1; } }

and also created new instance

var myObject = new MainV2(true);
myObject.myButton1.Text = "Disconnecting all";

nothing works ...

i don't even know where is the function called from, because is clear that is not called from MainV2 class ...

An exception of type 'System.NullReferenceException' occurred in MissionPlanner.exe but was not handled in user code

Any ideas? Thank you.

Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
Smithy
  • 19
  • 1
  • 6
  • Can we receive some clarification on why this secondary form will be modifying a button value on another form? – Greg Mar 19 '15 at 13:40

4 Answers4

0

You need to grab an instance of the form where the button resides. Do this by saving a static reference to the form in it's constructor:

namespace MissionPlanner
{
    public partial class MainV2 : Form
    {
        public static MainV2 CurrentForm;
        public MainV2()
        {
            CurrentForm = this;
            InitializeComponent();
        }

Then somewhere else in your code:

public void CloseAllConnections()
{
    MainV2.CurrentForm.myButton1.Text = "Disconnecting all";

    ... 
}
F Chopin
  • 574
  • 7
  • 23
  • and in my case what will be `CurrentForm` ? the other form ? – Smithy Mar 19 '15 at 13:50
  • and also i get this `Error 10 'System.Windows.Forms.Form' does not contain a definition for 'myButton1' and no extension method 'myButton1' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?) C:\wzi5\ARDUINO\MissionPlanner-master\MissionPlanner-master\MainV2.cs 3014 32 MissionPlanner ` when i am calling `MainV2.CurrentForm.myButton1.Text = "Disconnecting all";` from `public void CloseAllConnections()` function ... what now ? – Smithy Mar 19 '15 at 13:56
  • CurrentForm will be the current instance of the MainV2 form if it is open. If the MainV2 form is not open then it will be null. – F Chopin Mar 19 '15 at 13:58
  • Sorry, the definition should be: public static MainV2 CurrentForm; Edited answer to correct – F Chopin Mar 19 '15 at 13:59
  • i made the modification, i get same error `An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code Additional information: Cross-thread operation not valid: Control 'myButton1' accessed from a thread other than the thread it was created on.` what to do?? i don't know ... – Smithy Mar 19 '15 at 14:25
  • I didn't realise that the CloseAllConnections method is being executed from another thread. This post will solve your problem: http://stackoverflow.com/questions/10775367/cross-thread-operation-not-valid-control-textbox1-accessed-from-a-thread-othe – F Chopin Mar 19 '15 at 15:37
0

It appears that your click event form object is called (name) myButton1, and that you are calling the following to change it: myobject.myButton1.Text = "Disconnecting all". Try using myButton1.Text = "Disconnecting all" instead.

KevinT
  • 21
  • 3
0

One thing that you could try is to pass the button from the form to the class that will be modifying the button.

public class Example
{
    public Button MyButton1 { get; set; }

    public Example(Button myButton1)
    {
        MyButton1 = myButton1;
    }

    public void CloseAllConnections()
    {
        MyButton1.Text = "Disconnecting all";
    }
}

This should successfully set the button's text on MainV2.

  • thanks for the response, but i get this error even from the beginning `An exception of type 'System.NullReferenceException' occurred in MissionPlanner.exe but was not handled in user code Additional information: Object reference not set to an instance of an object.` .... what to do? – Smithy Mar 19 '15 at 14:29
  • Quick question. Where are you creating the new instance of the class that contains the `CloseAllConnections` method? – Dazed and Confused Mar 19 '15 at 14:39
  • here is the explination : http://pastebin.com/Es0f8Y6z what do you think about this? – Smithy Mar 19 '15 at 15:37
  • User2747636, I'm not sure if it's just the way it's been pasted into pastebin, but it looks like your `public void CloseAllConnections()` method is within the MainV2 class itself. Is MainV2 the form which has your button? If so, then you should just be able to change the text with `myButton1.Text = "Disconnecting all"` without anything I suggested above. – Dazed and Confused Mar 19 '15 at 16:13
0

Are you using any sort of multi-threading in your app? if so: Make sure you either only change the button from the same thread it was created by, or simply use the ControlInstance.Invoke() method to delegate the change.

gwasterisk
  • 49
  • 3