Grab text from Linklabel created in Runtime C#

Asked

Viewed 30 times

0

I’m creating some LinkLabel at runtime with the code below:

for (int i = 0; i < qtdresp; i++)
{
    System.Windows.Forms.LinkLabel Lbl = 
        new System.Windows.Forms.LinkLabel();
        
    pnlFull.Controls.Add(Lbl);
    Lbl.Text = trilhas.Rows[i]["duvida"].ToString();
    Lbl.Top = pos;
    pos += 30;
    Lbl.Left = 20;
    Lbl.ForeColor = Color.Black;
    Lbl.Width = 250;
    Lbl.TextAlign = ContentAlignment.TopRight;
    Lbl.Font = new Font("Raleway", 10);
    
    Lbl.LinkClicked += 
        new LinkLabelLinkClickedEventHandler(Lbl_LinkClicked);
}

and the result is this one:

inserir a descrição da imagem aqui

  • Now how do I know which of the two the person clicked on?

  • You need to take this text of LinkLabel to make my next select?

2 answers

0

use the Lbl_linkclicked.text object in the Linklabellinkclickedeventhandler function compare it to your Strings list;

int identificador = -1;
for (int i = 0; i < qtdresp; i++)
{
    if(trilhas.Rows[i]["duvida"].ToString() == Lbl_LinkClicked.Text)
     {
        identificador = i;
        break;
     }

}

if(identificador  != -1)
{
  //o i será seu identificador da string clicada.

  trilhas.Rows[identificador]["duvida"].ToString()
}

0


And click event gets two parameters, sender and e which are respectively who shot the event and his arguments.

So if you access Sender, it will be the linklabel itself:

for (int i = 0; i < qtdresp; i++)
{
    System.Windows.Forms.LinkLabel Lbl = 
        new System.Windows.Forms.LinkLabel();
        
    pnlFull.Controls.Add(Lbl);
    Lbl.Name = "link"+i; //Definindo um nome chave para o controle, também é possível acessa-lo posteriomente.
    Lbl.Text = trilhas.Rows[i]["duvida"].ToString();
    Lbl.Tag = "http://google.com"; //Use a Tag para algum dado associado ao controle
    Lbl.Top = pos;
    pos += 30;
    Lbl.Left = 20;
    Lbl.ForeColor = Color.Black;
    Lbl.Width = 250;
    Lbl.TextAlign = ContentAlignment.TopRight;
    Lbl.Font = new Font("Raleway", 10);
    
    Lbl.LinkClicked += (_sender,_e)=>{
         System.Windows.Forms.LinkLabel linkLabel = _sender as System.Windows.Forms.LinkLabel;
         string url = linkLabel.Tag.ToString();
         string texto = linkLabel.Text;
         string nome = linkLabel.Name;
         
    };
}

You can use a Lambda operator and mount the event expression still within the scope of the control, and can access the variables within the for loop.

for (int i = 0; i < qtdresp; i++)
{
    System.Windows.Forms.LinkLabel Lbl = 
        new System.Windows.Forms.LinkLabel();
        
    pnlFull.Controls.Add(Lbl);
    Lbl.Name = "link"+i; //Definindo um nome chave para o controle, também é possível acessa-lo posteriomente.
    Lbl.Text = trilhas.Rows[i]["duvida"].ToString();
    Lbl.Top = pos;
    pos += 30;
    Lbl.Left = 20;
    Lbl.ForeColor = Color.Black;
    Lbl.Width = 250;
    Lbl.TextAlign = ContentAlignment.TopRight;
    Lbl.Font = new Font("Raleway", 10);
    
    Lbl.LinkClicked += (_sender,_e)=>{
       MessageBox.Show("Clicou no link" + i + "Url = "+trilhas.Rows[i]["url"].ToString());
    };
}

Other ways of doing:

...

Lbl.LinkClicked += LinkLabel1_LinkClicked;

...

private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        System.Windows.Forms.LinkLabel linkLabel = sender as System.Windows.Forms.LinkLabel;
         string url = linkLabel.Tag.ToString();
         string texto = linkLabel.Text;
         string nome = linkLabel.Name;
    }
  • Excellent explanation! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.