4

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!!

MiffTheFox
  • 21,302
  • 14
  • 69
  • 94
sheetal
  • 387
  • 4
  • 7
  • 13
  • 2
    Check the enabled status of the parent elements of the button – rahul Jul 03 '09 at 06:52
  • Hit another breakpoint just after the assignment obj.btnRtn.Enabled = true; and then check the status. If it returns false then any one the parent is not enabled. – rahul Jul 03 '09 at 06:53

11 Answers11

4

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.

Marcelo
  • 41
  • 1
2

Is the button placed inside a panel or any container. If yes then please check the enabled status of the container also.

rahul
  • 184,426
  • 49
  • 232
  • 263
2

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.

  • Hi Thanks for your reply. But if i use the Show method the details I have entered in the TextBox before clicking on the button in first form are not retained. The show method again opens up the Form and again I have to re-enter the values entered in textbox. Is there any way I can temporarily hide this form and retain the values when Form2 is closed. Please help me out!! – sheetal Jul 03 '09 at 09:50
  • There is very good answer to prevent open form again and again http://stackoverflow.com/questions/677056/opening-forms-in-c For hide the form u can use the form_name.hide() method and for again show it again use formname.show() eg; in ur context u use the obj.hide() and then obj.show(). –  Jul 03 '09 at 11:18
1

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:

  • Check that you're looking at the right button. Change its text as well as enabling it.
  • Check that its container is enabled, as suggested by phoenix. Not just its direct parent, but all the way up.
  • Check what else your UI thread is doing afterwards - are you blocking it for some reason? Does the rest of the UI work at that point?
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for your prompt reply. I just added the BreakPoint and I find that these statements are being executed but still the button is not enabled. What should I do? How can I acheieve desired task? Please somebody help me out!! – sheetal Jul 03 '09 at 05:35
1

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

Larry Watanabe
  • 10,126
  • 9
  • 43
  • 46
  • Thanks for your prompt reply. I just added the breakpoint and I found that the statements are executed but the button is not enabled. What should I do to perform the desired task? Please help me out!! – sheetal Jul 03 '09 at 05:39
1

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

Beth
  • 9,531
  • 1
  • 24
  • 43
1

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.

Sivvy
  • 853
  • 9
  • 28
1

If the form that your are trying to show on the screen is properly shown, may be you can try this:

  1. Create a public method in the form that will set the button property enabled = true;
  2. After creating the form, and showing, you can call that public method;

frmRb obj = new frmrb(); obj.EnableButton

ZokiManas
  • 732
  • 5
  • 8
1

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!

Sisyphus
  • 679
  • 6
  • 21
0

Just a hunch. May be the 'Locked' property of button is 'true'

Varun Mahajan
  • 7,037
  • 15
  • 43
  • 65
  • Locked property has nothing to do with the enabled status of a button. It just specifies whether the button can be moved or resized. – rahul Jul 03 '09 at 06:37
  • No the button is not locked. I mean the locked property of the button is set to false. – sheetal Jul 03 '09 at 06:51
0

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