C# how to enable button from another class
I have a form1, there's a "download" button, and a "next" button. By default, the "next" button is disabled. When I click "download" button, I will call a method from download.cs, different from form1.cs. After downloading, I want the "next" button to be enabled, so in download.cs, after receiving all files, I put
form1.btnNext.Enabled = true;
But it doesn't work. I also put this in form1.cs
public void enableButton(bool enabled)
{ btnNext.Enabled = enabled; }
and in download.cs I put:
pr开发者_Go百科ivate form1 form1; ...
form1.enableButton(true);
but it doesn't work either. Could any1 show me where I'm wrong? thanks in advance.
Your question needs some more information to be sure, but I'm guessing that the instance of form1
in your downloader class is not the same as the instance that's shown on your screen.
If your downloader should be able to call back to the form that started it up, the easiest way would be to pass a reference to that form along to the downloader class.
If you are freezing your UI while download than
1- Define a event in your Downloader class
public delegate void EnableUI(bool shdEnable);
public event EnableUI MakeUIEnabled;
2- Hook this event in your form1 class , i assume that you are creating an object of download class in the download button or it is created at class level , lets say this object as customDownloader
customDownloader.MakeUIEnabled+=new EnableUI(EnableUIControls);
3- Define a event hander in form1 class as shown below.
public void EnableUIControls(bool shdEnable)
{
btnNext.Enabled = shdEnable;
}
4- Let's sya that there is a downloadfile() method in download.cs file so when your download is completed than raise this event.
public void DownLoadFile()
{
if(MakeUIEnabled!=null)
MakeUIEnabled(true);
}
tha's it
I remember facing something like this , and you may try to make your (next) buttonpublic instead of private , but this is not a very adorable approach ,but I guess if you do hat then your first suggestion will work.
and if it doesn't you can try doing this :
Form1 myForm = new Form1; myform.nxtButton.enabled = true;
it will definitely work
You can pass through a reference to the Button to the new class. So when you call the method from download.cs, pass the name of the button through as a parameter and then you can use it within the new class as you would any other reference.
You could do it via a Callback. So in your Download.cs, you have something like this:
public delegate void EnableNextCallback (bool enable);
public static void Download(EnableNextCallback n) {
// stuff
// when you're done
n(true);
}
That way, you can pass your EnableButton method to your download method.
You need to have a reference to Form1
within Form2
. Is Form2
a dialog window, MDI child window, etc.? Is Form1
set as parent window? Does Form1
open Form2
?
If Form1
does in fact open Form2
, you can set the owner property to have a reference to Form1
from within Form2
. Also, ensure that button10
in Form1
is set to PUBLIC (it's private by default).
Within Form1
, you'll have code like this:
Form2 MyForm2 = new MyForm2();
MyForm2.Owner = this; // "this" being Form1
MyForm2.Show();
In Form2
, whenever you want to access button10
on Form1
, do this:
(this.Owner as Form1).button10.Enabled = false;
精彩评论