views:

414

answers:

9

Hi all,

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

+1  A: 

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
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
+1  A: 

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
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
+1  A: 

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

rahul
No button is not placed inside any Panel control or Container
sheetal
+1  A: 

Are you sure the obj is the form that is bought to the front? Can you post a sample app with the code not working?

Mark
A: 

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

Varun Mahajan
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
No the button is not locked. I mean the locked property of the button is set to false.
sheetal
+1  A: 

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.

qauaan
HiThanks 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
There is very good answer to prevent open form again and againhttp://stackoverflow.com/questions/677056/opening-forms-in-cFor 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().
qauaan
+1  A: 

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
+1  A: 

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
+1  A: 

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