0

hello i have this code here (Owner as ReadBarCodeInMenu).btnContainerInquiry.Enabled = true; it keeps me throwing an instance whenever i tried to access a button of another page it tried this code in a test 2 forms and it works perfectly fine but when i put it in system i gave me an error.Please help me

edit:

form ReadBarCodeInMenu

private void btnContainerInquiry_Click(object sender, EventArgs e)
    {
        inquiry.Owner = this;
        btnContainerInquiry.Enabled = false;
    }

form ContainerInquiry

private void logoutBtn_Click(object sender, EventArgs e)
    {
      (Owner as ReadBarCodeInMenu).btnContainerInquiry.Enabled = true; 
      error// {"Object reference not set to an instance of an object."}
      close.      
    }

this is how i access the bottom of an another form if i do this in new 2 form it works with no error.

Red
  • 11
  • 6
  • Please show your code showing the problem, we can't guess. – Poul Bak Nov 11 '18 at 23:25
  • my error is an instance (Owner as ReadBarCodeInMenu).btnContainerInquiry.Enabled = true; {"Object reference not set to an instance of an object."} – Red Nov 11 '18 at 23:48
  • Try to start debugging and step through you code. (Either 'Owner' is null or btnContainer.Inquiry is null) – Poul Bak Nov 11 '18 at 23:50
  • Because `Owner` is probably not a `ReadBarCodeInMenu`. "Owner" is strange anyway. What do you mean by another page? You need a reference to the form containing button. – Olivier Jacot-Descombes Nov 11 '18 at 23:53

1 Answers1

0

Assuming that you open form ContainerInquiry from withing form ReadBarCodeInMenu, you can do the following. In form ContainerInquiry, add a parameter to the constructor, accepting a reference to the first form

private ReadBarCodeInMenu _readBarCodeInMenu;

public ContainerInquiry(ReadBarCodeInMenu readBarCodeInMenu)
{
    InitializeComponent();
    _readBarCodeInMenu = readBarCodeInMenu;
}

private void logoutBtn_Click(object sender, EventArgs e)
{
    _readBarCodeInMenu.btnContainerInquiry.Enabled = true; 
}

Also, change the accessibility of btnContainerInquiry from private to internal (or public).


Then in form ReadBarCodeInMenu

// Pass a reference of ReadBarCodeInMenu to ContainerInquiry.
var frm = new ContainerInquiry(this);
...

You cannot access the UI from another thread than the UI thread. If try to do so, you are getting the exception

Cross-thread operation not valid: Control 'btnContainerInquiry' accessed from a thread other than the thread it was created on.

In this case you must invoke the control or form you are accessing. This mechanism passes a delegate to the right thread and executes it there.

var btn = _readBarCodeInMenu.btnContainerInquiry;
if (btn.InvokeRequired) {
    btn.Invoke(new Action(() => btn.Enabled = true));
} else {
    btn.Enabled = true; 
}

You could also create an extension method that automates this process as shown here: https://stackoverflow.com/a/12179408/880990

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • it works but i need to access to another form in order to close it or else it give me an error of {"Cross-thread operation not valid: Control 'btnContainerInquiry' accessed from a thread other than the thread it was created on."} example if i open form 1 and click the button for form2 i can't close the form to i need to press the button for form3 in form 1 then close the form 2. – Red Nov 12 '18 at 00:19