How to code it in VC#? (interactive between two listbox)
-
Monday, June 11, 2007 2:16 PM
I have two listbox and I want that when the user touch the scroll in one Listbox the other listbox will move to the same row..?
furthermore, these two listbox display the same line by both clicking scrollbar and mid-wheel, could you help me?
3q.
Answers
-
Monday, June 11, 2007 7:18 PMModerator
I assume you are talking about this thread. Here's the same code in C#:
using System;
using System.Windows.Forms;
public class MyListBox : ListBox {
public delegate void ScrollEventHandler(Message m);
public event ScrollEventHandler Scroll;
protected override void WndProc(ref Message m) {
if (m.Msg == 0x115 && Scroll != null) Scroll.Invoke(m);
base.WndProc(ref m);
}
}
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private void myListBox1_Scroll(Message m) {
SendMessage(myListBox2.Handle, m.Msg, m.WParam, m.LParam);
}
}
All Replies
-
Monday, June 11, 2007 4:26 PM
Moved to VC# forums
-
Monday, June 11, 2007 7:18 PMModerator
I assume you are talking about this thread. Here's the same code in C#:
using System;
using System.Windows.Forms;
public class MyListBox : ListBox {
public delegate void ScrollEventHandler(Message m);
public event ScrollEventHandler Scroll;
protected override void WndProc(ref Message m) {
if (m.Msg == 0x115 && Scroll != null) Scroll.Invoke(m);
base.WndProc(ref m);
}
}
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private void myListBox1_Scroll(Message m) {
SendMessage(myListBox2.Handle, m.Msg, m.WParam, m.LParam);
}
} -
Tuesday, June 12, 2007 5:40 AM
Hi nobugz
Thank you for your reply.
I am new for c# program, so I can't continue performing the solution. However, from the codes above, I think there maybe some more codes. could you please give me a comprehensive and succesfull statments?
My development enviroment is MS Visual Studio 2005.
Best regards,
wisn
-
Saturday, June 16, 2007 8:29 AM
I hope that the codes above are capable of complete my requirement: listBox2 will scroll to the same row to listBox1 when clicking the vertical scrollbar of listBox1.
This feature exists in C++ builder and I have got it. However, I can't complete it in Visual C# 2005.
I have tried these codes, but I haven't gotten the synchronization of two listBoxs. I've no idea where is the problem. Could you help me? Here is my test codes:
//-----------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}public class MyListBox : ListBox
{
public delegate void ScrollEventHandler(Message m);
public event ScrollEventHandler Scroll;protected override void WndProc(ref Message m)
{
if (m.Msg == 0x115 && Scroll != null) Scroll.Invoke(m);
base.WndProc(ref m);
}
}[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);private void listBox1_Scroll(Message m)
{
SendMessage(listBox2.Handle, m.Msg, m.WParam, m.LParam);
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
listBox1.Items.Add(i + 1);
listBox2.Items.Add(i + 1);
}
}private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Brush FontBrush = null;
ListBox listBox1 = sender as ListBox;
FontBrush = Brushes.Black;//set overall brush
if (e.Index > -1)
{
if (e.Index % 5 == 0)
{
FontBrush = Brushes.Red;//set local brush
}
e.DrawBackground();
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, FontBrush, e.Bounds);
e.DrawFocusRectangle();
}}
private void listBox2_DrawItem(object sender, DrawItemEventArgs e)
{
Brush FontBrush = null;
ListBox listBox2 = sender as ListBox;
FontBrush = Brushes.Black;//set overall brush
if (e.Index > -1)
{
if (e.Index % 4 == 0)
{
FontBrush = Brushes.Red;//set local brush
}
e.DrawBackground();
e.Graphics.DrawString(listBox2.Items[e.Index].ToString(), e.Font, FontBrush, e.Bounds);
e.DrawFocusRectangle();
}
}
}
}//---------------------------------------------------------------------
-
Saturday, June 16, 2007 11:50 AMModeratorYeah, this is not correct. Check your other thread for the instructions.

