Reputation: 387
I am creating an Windows Application. I have two buttons. I have written the following code snippet.
frmRb obj = new frmrb();
private void btnPd_Click(object sender, EventArgs e)
{
btnCancel.Enabled = true;
obj.btnRtn.Enabled = true;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
obj.BringToFront();
obj.Focus();
}
The above coding does not generate any error.
All the statements are working properly but the following statement is not working properly:
obj.btnRtn.Enabled = true;
is not executed.
The frmrb forms is bring to front and it is focussed but btnRtn is not Enabled that is the statement obj.btnRtn.Enabled = true; is not working.
By default I have set the property of btnRtn Enabled to false. And Please note the Modifier property of btnRtn button is set to PUBLIC.
Now how should I change the coding so that I can get this statement executed.
obj.btnRtn.Enabled = true;
Can anybody help me out?
Thanks in Advance!!
Upvotes: 4
Views: 19428
Reputation: 31
Hi I had the same problem, I created a void, it sounds crazy to me though but it works this way and then whenever I want to change the button Enable I use the void:buttonOnOff(true);
or buttonOnOff(false)
;
public void buttonOnOff(bool buttonCondition)
{
if (button1.InvokeRequired)
{
button1.Invoke(new MethodInvoker(delegate { button1.Enabled = buttonCondition; }));
}
else
{
button1.Enabled = buttonCondition;
}
}
Upvotes: 0
Reputation: 708
It's been a while since anyone commented or answered I thought I would pose this answer/comment as it may be be helpful to others who stumble on it.
I recently had some CheckBoxes and NumericUpDowns not changing enabled state but it was simply due to the fact they were in a GroupBox that had not been enabled. A forehead slapping moment for me, but took me 20 min to figure out why those controls weren't responding!
Upvotes: 1
Reputation: 51
SOLUTION You should never disable a button, or change it´s visibility before it is initialized, otherwise you won't be able to enable it again, or turn it visible again. Instead, you should disable it on it's own "Initialized" event, and then it will work properly! I had the same problem.
Upvotes: 5
Reputation: 1
Pl check btnRtn situation if btnRtn is in panel or open ,if it in the panel pl check the panel enabled property and your coding is ok
Upvotes: 0
Reputation: 722
If the form that your are trying to show on the screen is properly shown, may be you can try this:
frmRb obj = new frmrb(); obj.EnableButton
Upvotes: 1
Reputation: 851
Your code really should produce an error... C# is case sensitive, meaning frmRb is not the same as frmrb. Anyway, I copied it, created 2 forms and 3 buttons, and set up the handler, and it worked fine.
private void InitializeComponent()
{
this.btnPd = new System.Windows.Forms.Button();
this.btnPd.Location = new System.Drawing.Point(90, 116);
this.btnPd.Name = "btnPd";
this.btnPd.Size = new System.Drawing.Size(75, 23);
this.btnPd.TabIndex = 1;
this.btnPd.Text = "button1";
this.btnPd.UseVisualStyleBackColor = true;
this.btnPd.Click += new System.EventHandler(this.btnPd_Click);
}
public System.Windows.Forms.Button btnRtn;
Are you sure you were handling btnPd? Perhaps you may have locked your enabling code inside a disabled button? Hopefully this small working sample helps you find the problem. As for the rest of the code, All I changed was the frmRb to frmrb so they match.
Upvotes: 1
Reputation: 9607
I would try just switching the sequence of statements to: private void btnPd_Click(object sender, EventArgs e) { obj.btnRtn.Enabled = true; btnCancel.Enabled = true; }
and see if that helps you debug
Upvotes: 1
Reputation:
You not mention that where you obj(which is the instance of frmRb) show. because it is very important point. from your coding it seem that frmRb is already visible. so u never called the
obj.Show() ;
instead you call the
obj.BringToFront();
so the problem is that you never show the frmRb object. which is you create u in 1st line. each time u write the line
frmRb obj = new frmrb();
new instance of frmrb is created. So u must again show it, with the line obj.Show() ; Now u rewrite ur code as ::
frmRb obj = new frmrb();
private void btnPd_Click(object sender, EventArgs e)
{
btnCancel.Enabled = true;
obj.btnRtn.Enabled = true;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
obj.Show();
obj.BringToFront();
obj.Focus();
}
I hope it is helpful for u and solve ur problem.
Upvotes: 2
Reputation: 7297
Just a hunch. May be the 'Locked' property of button is 'true'
Upvotes: 0
Reputation: 1499800
I strongly suspect that either the click handler isn't being called or you're not looking at the form you think you are.
Where are you setting up the click handler for btnPd_Click? Check that's still wired up correctly.
Then put a breakpoint on the first line of the handler and run it in the debugger - if you don't hit the breakpoint when you click the button, that's the problem.
EDIT: Okay, next steps:
Upvotes: 1
Reputation: 187020
Is the button placed inside a panel or any container. If yes then please check the enabled status of the container also.
Upvotes: 3
Reputation: 10184
I've been using VB rather than C#, but the languages are very similiar. in VB, you have to add "handles SomeButton.Click" to make it handle the Click events.
According to google, the equivalent in C# is to go into the Designer.cs file, find where the controls are, and manually change the click event hookup to point to your new event handler.
As mentioned by the previous poster, use a breakpoint (F9) and the debugger to see if that method is ever called when you execute the event. If it is not called, then the problem is probably not with the enabled property, but the wiring of the method so that it is invoked when the event occurs.
Here's a reference:
http://www.tek-tips.com/viewthread.cfm?qid=1442702&page=5
Upvotes: 1