Search code examples
c#winformstooltipoledb

Creating tooltip for multiple buttons


This code(below) suppose to add information to ToolTips which are taken from database(and the class Codons does it(it is the part that actually works)). I tried to do it in FOR loop, but it is warning me about this line:

toolTip1.SetToolTip(Convert.ToString(letter),"Name: "+fullname+" ("+cdn.GetCodon1()+")"
                          +"\n Begin: "+cdn.GetStart()+", End: "+cdn.GetEnd()+"");

I have 20 buttons which are named in a-z letters, except 6 specific letters(see the IF inside the FOR)

Here is the CODE:

private void UpdateToolTipButton()
{
  string fullname;
  Codons cdn;
  char letter='a';
  //get info about every amino acid from database
  OleDbConnection  dataConnection = new OleDbConnection();
  dataConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
  dataConnection.Open();
  for(int i=1;i<=26;i++,letter++)
  {
    if((letter!='b')&&(letter!='e' )&& (letter!='j') && (letter!='o')&& (letter!='u') && (letter!='z'))
    {
      OleDbCommand datacommand = new OleDbCommand();
      datacommand.Connection = dataConnection;
      datacommand.CommandText = "SELECT tblCodons.codonsFullName"
                     +" FROM tblCodons"
                     +" WHERE tblCodons.codonsCodon1="+letter;   

      OleDbDataReader dataReader = datacommand.ExecuteReader();
      dataReader.Read();
      fullname = dataReader.GetString(0);
      cdn = new Codons(fullname);

      toolTip1.SetToolTip(Convert.ToString(letter),"Name: "+fullname+" ("+cdn.GetCodon1()+")"
                          +"\n Begin: "+cdn.GetStart()+", End: "+cdn.GetEnd()+"");
    }
  }
}

Solution

  • SetToolTip is looking for a control as the first argument. You are supplying Convert.ToString(letter).

    The first argument needs to be the button you want to have the tooltip:

    toolTip1.SetToolTip(button1, "Name: " + fullname);
    

    I'm guessing you were trying to set the title of the ToolTip, in which case, that's not part of the SetToolTip method. You would have to set the property yourself:

    toolTip1.ToolTipTitle = Convert.ToString(letter);
    

    If your buttons are those letters, then you would reference them by their name as the control key:

      if (this.Controls.ContainsKey(Convert.ToString(letter))) {
        toolTip1.SetToolTip(this.Controls[Convert.ToString(letter)], "Name: " + fullname + " etc(";
      }