whats wrong with my button? C#

Discussion in 'OT Technology' started by Tk, Jun 4, 2003.

  1. Tk

    Tk Well-Known Member

    Joined:
    Dec 23, 2001
    Messages:
    23,075
    Likes Received:
    281
    Location:
    nw iowa
    Code:
    [size=1][color=#0000ff]private[/color][/size][size=1][color=#0000ff]void[/color][/size][size=1] btnEdit_Click([/size][size=1][color=#0000ff]object[/color][/size][size=1] sender, System.EventArgs e)
    {
    [/size][size=1][color=#0000ff]if[/color][/size][size=1] ([/size][size=1][color=#0000ff]this[/color][/size][size=1].Text == "Edit")
    {
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtTitle.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtRating.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtGenre.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtReleased.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtAdded.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtStarring.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtSynopsis.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].Text = "Save";
    }
    [/size][size=1][color=#0000ff]else[/color][/size][size=1][color=#0000ff]if[/color][/size][size=1] ([/size][size=1][color=#0000ff]this[/color][/size][size=1].Text == "Save")
    {
    [/size][size=1][color=#008000]// Store movie details
    [/color][/size][size=1][color=#0000ff]this[/color][/size][size=1].lstDVDs.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].cboGenres.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtAdd.Enabled = [/size][size=1][color=#0000ff]true[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtTitle.Enabled = [/size][size=1][color=#0000ff]false[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtRating.Enabled = [/size][size=1][color=#0000ff]false[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtGenre.Enabled = [/size][size=1][color=#0000ff]false[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtReleased.Enabled = [/size][size=1][color=#0000ff]false[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtAdded.Enabled = [/size][size=1][color=#0000ff]false[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtStarring.Enabled = [/size][size=1][color=#0000ff]false[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].txtSynopsis.Enabled = [/size][size=1][color=#0000ff]false[/color][/size][size=1];
    [/size][size=1][color=#0000ff]this[/color][/size][size=1].Text = "Edit";
    }
    }
    [/size]
     
    the button does now enabled or disable any of the text boxes, or change the caption of the button even. but when the 'add' button is pushed, it has *.enabled = false to the text boxes and they get disabled and the caption for the Edit button gets changed to 'save'.

    why wont this work in the edit button?
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    on your "if" and "else if" brackets, you're using this.Text which would be the form's .Text property. Change it to this.btnEdit.Text and it should work. Might want to change the last line in those bracekts too. You also have this.Text instead of this.btnEdit.Text
     
  3. Tk

    Tk Well-Known Member

    Joined:
    Dec 23, 2001
    Messages:
    23,075
    Likes Received:
    281
    Location:
    nw iowa
    Sled saves the day again! ..thanks man :)

    I was thinking 'this' referred to the current control, because you are in that controls code section. :sad2:

    Anyway, I get it now. Thanks :bigthumb:
     
  4. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    "this" actually refers to the current class you are in. The "sender" variable that is sent to that event will reference the calling control though (i.e. your edit button) so you could do something like this..

    Code:
    private void btnEdit_Click(object sender, System.EventArgs e)
    {
       Button myButton = (Button)sender;
       if (myButton.Text == "Edit")
       {
          ...
       }
       else if( myButton.Text == "Save" )
       ....
    }
    
    
     

Share This Page