How to fix an instance when trying to access a button of another winform?











up vote
0
down vote

favorite












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.










share|improve this question
























  • Please show your code showing the problem, we can't guess.
    – Poul Bak
    Nov 11 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 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 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 at 23:53















up vote
0
down vote

favorite












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.










share|improve this question
























  • Please show your code showing the problem, we can't guess.
    – Poul Bak
    Nov 11 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 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 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 at 23:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question















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.







c# winforms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 23:56

























asked Nov 11 at 23:12









Red

96




96












  • Please show your code showing the problem, we can't guess.
    – Poul Bak
    Nov 11 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 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 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 at 23:53


















  • Please show your code showing the problem, we can't guess.
    – Poul Bak
    Nov 11 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 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 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 at 23:53
















Please show your code showing the problem, we can't guess.
– Poul Bak
Nov 11 at 23:25




Please show your code showing the problem, we can't guess.
– Poul Bak
Nov 11 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 at 23:48




my error is an instance (Owner as ReadBarCodeInMenu).btnContainerInquiry.Enabled = true; {"Object reference not set to an instance of an object."}
– Red
Nov 11 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 at 23:50




Try to start debugging and step through you code. (Either 'Owner' is null or btnContainer.Inquiry is null)
– Poul Bak
Nov 11 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 at 23:53




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 at 23:53












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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






share|improve this answer























  • 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 at 0:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254187%2fhow-to-fix-an-instance-when-trying-to-access-a-button-of-another-winform%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










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






share|improve this answer























  • 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 at 0:19















up vote
0
down vote



accepted










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






share|improve this answer























  • 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 at 0:19













up vote
0
down vote



accepted







up vote
0
down vote



accepted






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 13:00

























answered Nov 12 at 0:01









Olivier Jacot-Descombes

65.1k885136




65.1k885136












  • 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 at 0:19


















  • 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 at 0:19
















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 at 0:19




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 at 0:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254187%2fhow-to-fix-an-instance-when-trying-to-access-a-button-of-another-winform%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."