-1

I made a class of label class label

class label
{
    public Label l; 
    public Label acess()
    {
        l = new Label();
        l.Text = "asdad";
        l.Left=100;
        l.Top =100;
        return l;
    }
    public Label lab 
    {
        get
        {
            return l;
        }
        set
        {
            l = value;
        }
    }

}

And called this method and initialized it on the form

Label l;
    label cls;
    public MainForm()
    {

        InitializeComponent();




        cls = new label();
        l = new Label();
        l =cls.acess();
        this.Controls.Add(l);

    }

Now i can access my label("l") ".Click" option through "lab" like

cls.lab.Click = //anything

but i dont know how to use this statement, i just knew how to use click events going through the label events, but i dont know how to use this one(which made through the code). How can i use it if i want to check label's text, like

 cls.lab.Click = {

if(lab.text=="i am the old label")
{
   lab.text = "i am the new label";
}
  }

Please Explain Me, Give a Detailed Answer.

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • what framework are you using, WPF, winforms? You need most likely to inherit from some control, like `UserControl` to get the event – thumbmunkeys Nov 14 '16 at 14:52
  • Are you asking about [event handlers](http://stackoverflow.com/q/803242/1997232)? – Sinatr Nov 14 '16 at 14:58
  • @thumbmunkeys I m using Winforms – ShoaibSivany Nov 14 '16 at 15:08
  • do you want to call the raise the `Click` event? – Mong Zhu Nov 14 '16 at 15:10
  • @Sinatr Yes! i am asking about using Click event handler through code(not directly by clicking on label and go to the events and then click, because as you see i made this label through code and not dragged and dropped). – ShoaibSivany Nov 14 '16 at 15:11
  • @MongZhu Sorry i didnt get you. I want to use click event of the label(which is made through code, and not dropped on the form from toolbox. Since it is not created that way, i cant just click on to it and go to events section(as we normally do) so i want to access it's click event), in one of the posts i saw that it is used as (class.label.click = ...) but i dont know how to use this statement for further operations. – ShoaibSivany Nov 14 '16 at 15:18
  • There is no difference between the event you get from the designer and the one you create in code yourself. For the *inline* version, you probably want something like `l.Click += ((s, e) => { MessageBox.Show("Hello"); });` – LarsTech Nov 14 '16 at 15:26
  • @LarsTech Thanks alot :) – ShoaibSivany Nov 14 '16 at 16:05

1 Answers1

1

You add an event listener to the label like this:

public MainForm()
{
    InitializeComponent();

    cls = new label();
    l =cls.acess();
    l.Click += cls_Clicked;
    this.Controls.Add(l);
}

private void cls_Click(object sender, EventArgs e)
{
    Label clickedLabel = sender as Label;
    if(clickedLabel == null) return;

    if(clickedLabel.Text=="i am the old label")
    {
        clickedLabel.Text = "i am the new label";
    }
}

I wasn't able to test it right now, but it should work, assuming you are using WinForms.

ChronosMOT
  • 341
  • 5
  • 17
  • I made a small mistake, please check the edited version. – ChronosMOT Nov 14 '16 at 15:15
  • Is there any option to view edited version or should I simply refresh my page? – ShoaibSivany Nov 14 '16 at 15:28
  • I'm not sure, refresh should work. The change was in the MainForm constructor. I missed that cls.access() returned a Label. So l.lab.Click failed. Still untested though – ChronosMOT Nov 14 '16 at 15:31
  • Also in the MainForm constructor l = new Label(); is not necessary, because it is overridden by the return value of access(). – ChronosMOT Nov 14 '16 at 15:33
  • There is no ".Text" of clickedLabel – ShoaibSivany Nov 14 '16 at 15:39
  • @ShoaibSivany Right, clickedLabel is of type label not Label. There you have to access lab. I've updated the answer again – ChronosMOT Nov 14 '16 at 15:42
  • I debugged it line by line the code executed to if(clickedLabel == null) return; and then gets out of the scope(did'nt check the remaining if condition). What does " if(clickedLabel == null) return; " mean? and why is it not running? – ShoaibSivany Nov 14 '16 at 15:57
  • @ShoaibSivany 'as' casts a variable to the given Type. If the variable is not of that type it will put 'null' (no value) in the variable. the if condition checks that the clickedLabel was converted correctly. But since the click event comes from the Label (inside your label) and not the label itself, the sender is of type Label. So clickedLabel is always null so it always leaves the method (returns). Will edit the answer right away – ChronosMOT Nov 14 '16 at 16:17
  • Thankyou very much! :) – ShoaibSivany Nov 14 '16 at 16:27
  • @ShoaibSivany I'm sorry it took so many tries, I shouldn't answer without testing. However if the answer now solves your problem please mark it as accepted answer – ChronosMOT Nov 14 '16 at 16:31