Автопрокрутка Поля списка под определенной ситуацией

Попробуй вот так ...

Sub Transfer_Data()

Dim i As Long, j As Long

j = 1

For i = 1 To 6
    If Sheets("Sheet1").Cells(i, 1).Value <> "" Then
        Sheets("Sheet1").Cells(i, 1).Copy
        Sheets("Sheet2").Cells(j, 1).PasteSpecial xlPasteFormats
        Sheets("Sheet2").Cells(j, 1).PasteSpecial xlPasteValues
        j = j + 1
    End If

    If j > 3 Then Exit For
Next i
Application.CutCopyMode = False
End Sub

Отредактированный код в соответствии с новым требованием:

Sub Transfer_Data()

Dim i As Long, j As Long

j = 1

For i = 1 To 6
    If Sheets("Sheet1").Cells(i, 1).Value <> "" Then
        Sheets("Sheet2").Cells(j, 1).Value = Sheets("Sheet1").Cells(i, 1).Value
        Sheets("Sheet2").Cells(j, 1).Interior.Color = Sheets("Sheet1").Cells(i, 1).Interior.Color
        j = j + 1
    End If

    If j > 3 Then Exit For
Next i

End Sub
9
задан 27 January 2009 в 20:53
поделиться

2 ответа

Этот пример кода должен выручить Вас. Я много раз делал это с TextBox, но он взял некоторое время для понимания этого для ListBox

Очевидно, это - просто Форма с Кнопкой и ListBox. Измените для установки потребностям:

private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Add("Some Text " + listBox1.Items.Count.ToString());

    //The max number of items that the listbox can display at a time
    int NumberOfItems = listBox1.ClientSize.Height / listBox1.ItemHeight;

    if (listBox1.TopIndex == listBox1.Items.Count - NumberOfItems - 1)
    {
        //The item at the top when you can just see the bottom item
        listBox1.TopIndex = listBox1.Items.Count - NumberOfItems + 1;
    }
}
9
ответ дан 4 December 2019 в 10:34
поделиться

Я решил эту проблему, используя метод, аналогичный методу colithium, за исключением того, что затем я понял, что есть ошибка с одновременными обновлениями. Итак, есть член класса m_previousCount, который хранит количество элементов в ListBox до того, как произошло это обновление.

Я сделал это с помощью ListView, но он должен работать так же для ListBox.

В этом случае мой listView1 привязан к содержимому listViewModel1.Entries.

private void EventMessageViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    listView1.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ScrollToLastItemDelegate(ScrollToLastItem)); 
}

/// <summary>
/// Scroll to last item, unless you have scrolled up the list
/// </summary>
private void ScrollToLastItem()
{
    // Get scrollviewer
    Decorator border = System.Windows.Media.VisualTreeHelper.GetChild(listView1, 0) as Decorator;
    ScrollViewer scrollViewer = border.Child as ScrollViewer;

    double vo = scrollViewer.VerticalOffset;

    // assume they are all the same height
    ListBoxItem lbi = listView1.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;

    //The max number of items that the listbox can display at a time
    double NumberOfItemsOnScreen = listView1.ActualHeight / lbi.ActualHeight;

    // use previousCount in case we get multiple updates in one go
    if (m_previousCount > NumberOfItemsOnScreen) // scrollbar should be active
    {
        if (vo < (m_previousCount - NumberOfItemsOnScreen)) // you're not at the bottom
        {
            return; // don't scroll to the last item
        }
    }

    m_previousCount = listView1.Items.Count;

    // scroll to the last item
    listView1.SelectedItem = listView1.Items.GetItemAt(listViewModel1.Entries.Count - 1);

    listView1.ScrollIntoView(listView1.SelectedItem);
}
0
ответ дан 4 December 2019 в 10:34
поделиться
Другие вопросы по тегам:

Похожие вопросы: