Как я могу установить другой текст всплывающей подсказки для каждого элемента в списке?

Используя событие MouseMove, вы windows-form можете отслеживать индекс c# элемента, над которым находится win32api указатель мыши, и сохранять winform его в переменной, которая dotnet сохраняет свое значение между windows-api MouseMoves. Каждый раз, когда c-sharp запускается MouseMove, он dot-net проверяет, не изменился ли win32 индекс. Если это так, он .net отключает всплывающую подсказку, изменяет winapi текст всплывающей подсказки visual-c# для этого элемента управления, а dot-net затем повторно активирует winform его.

Ниже приведен пример, в .net-framework котором одно свойство класса windows-form Car отображается в ListBox, но visual-c# затем отображается полная winforms информация при наведении listbox курсора на любую строку. Чтобы visual-c# этот пример работал, все, что .net-framework вам нужно, это ListBox с winapi именем lstCars с событием windows-api MouseMove и текстовый компонент windows-form ToolTip с именем tt1 в вашей winapi WinForm.

Определение класса csharp автомобиля:

    class Car
    {
        // Main properties:
        public string Model { get; set; }
        public string Make { get; set; }
        public int InsuranceGroup { get; set; }
        public string OwnerName { get; set; }
        // Read only property combining all the other informaiton:
        public string Info { get { return string.Format("{0} {1}\nOwner: {2}\nInsurance group: {3}", Make, Model, OwnerName, InsuranceGroup); } }
    }

Событие загрузки dotnet формы:

    private void Form1_Load(object sender, System.EventArgs e)
    {
        // Set up a list of cars:
        List allCars = new List();
        allCars.Add(new Car { Make = "Toyota", Model = "Yaris", InsuranceGroup = 6, OwnerName = "Joe Bloggs" });
        allCars.Add(new Car { Make = "Mercedes", Model = "AMG", InsuranceGroup = 50, OwnerName = "Mr Rich" });
        allCars.Add(new Car { Make = "Ford", Model = "Escort", InsuranceGroup = 10, OwnerName = "Fred Normal" });

        // Attach the list of cars to the ListBox:
        lstCars.DataSource = allCars;
        lstCars.DisplayMember = "Model";
    }

Код всплывающей подсказки dotnet (включая создание переменной dotnet уровня класса с именем hoveredIndex):

        // Class variable to keep track of which row is currently selected:
        int hoveredIndex = -1;

        private void lstCars_MouseMove(object sender, MouseEventArgs e)
        {
            // See which row is currently under the mouse:
            int newHoveredIndex = lstCars.IndexFromPoint(e.Location);

            // If the row has changed since last moving the mouse:
            if (hoveredIndex != newHoveredIndex)
            {
                // Change the variable for the next time we move the mouse:
                hoveredIndex = newHoveredIndex;

                // If over a row showing data (rather than blank space):
                if (hoveredIndex > -1)
                {
                    //Set tooltip text for the row now under the mouse:
                    tt1.Active = false;
                    tt1.SetToolTip(lstCars, ((Car)lstCars.Items[hoveredIndex]).Info);
                    tt1.Active = true;
                }
            }
        }

c#

.net

winforms

winapi

listbox

2022-10-21T22:30:40+00:00