Clicking a column by means of code

Z

Zach

private void makeStockList()
{
listView1_ColumnClick(listView1, ?
listView1.ColumnHeaderCollection[5]) ? ;
// more code
}

#region Sorting columns.
private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
listView1.Sort();
}
#endregion

Re the above, I want to click column header 5 by means of code. What I have
between ?? doesn't work. Which parameter would work?

Zach.
 
J

Jeff Johnson

private void makeStockList()
{
listView1_ColumnClick(listView1, ?
listView1.ColumnHeaderCollection[5]) ? ;
// more code
}

#region Sorting columns.
private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
listView1.Sort();
}
#endregion

Re the above, I want to click column header 5 by means of code. What I
have between ?? doesn't work. Which parameter would work?

Notice how in the event handler you are MAKING the list view sort itself by
calling the Sort() method. In fact, if you don't do this, you can click the
column all day long and nothing will happen. Therefore the list view getting
sorted it has absolutely nothing to do with the column click event per se
but rather because Sort() is called. And you can call that method at any
time in your code. See where I'm going with this?
 
Z

Zach

Either of us is not getting the point. I need to sort collumn 5 of the
listview and the rest of the list content accordingly. I cannot do that by
just calling Sort(). The way things are coded now is that clicking a
collumn header will sort that column and the rest of the listview content
accordingly, i.e., in that sequence. I want to use the method, which is
activated by a column click. So I need code to simulate a click of column
five. That was the question.

Zach


Jeff Johnson said:
private void makeStockList()
{
listView1_ColumnClick(listView1, ?
listView1.ColumnHeaderCollection[5]) ? ;
// more code
}

#region Sorting columns.
private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
listView1.Sort();
}
#endregion

Re the above, I want to click column header 5 by means of code. What I
have between ?? doesn't work. Which parameter would work?

Notice how in the event handler you are MAKING the list view sort itself
by calling the Sort() method. In fact, if you don't do this, you can click
the column all day long and nothing will happen. Therefore the list view
getting sorted it has absolutely nothing to do with the column click event
per se but rather because Sort() is called. And you can call that method
at any time in your code. See where I'm going with this?
 
R

Registered User

private void makeStockList()
{
listView1_ColumnClick(listView1, ?
listView1.ColumnHeaderCollection[5]) ? ;
// more code
}

#region Sorting columns.
private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
listView1.Sort();
}
#endregion

Re the above, I want to click column header 5 by means of code. What I have
between ?? doesn't work. Which parameter would work?

The second argument in call to the event handler must be of type
ColumnClickEventArgs. Try something like the untested code below.

ColumnClickEventArgs args = new ColumnClickEventArgs();
args.Column = 5;
listView1_ColumnClick(this, args);

regards
A.G.
 
H

Harlan Messinger

Zach said:
Jeff Johnson said:
private void makeStockList()
{
listView1_ColumnClick(listView1, ?
listView1.ColumnHeaderCollection[5]) ? ;
// more code
}

#region Sorting columns.
private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
listView1.Sort();
}
#endregion

Re the above, I want to click column header 5 by means of code. What
I have between ?? doesn't work. Which parameter would work?

Notice how in the event handler you are MAKING the list view sort
itself by calling the Sort() method. In fact, if you don't do this,
you can click the column all day long and nothing will happen.
Therefore the list view getting sorted it has absolutely nothing to do
with the column click event per se but rather because Sort() is
called. And you can call that method at any time in your code. See
where I'm going with this?
Either of us is not getting the point. I need to sort collumn 5 of the
listview and the rest of the list content accordingly. I cannot do that
by just calling Sort().

Right, you have to create set the ListViewItemSorter? Do you think that
the click event handler is the only place in which you can do that?
The way things are coded now is that clicking a
collumn header will sort that column and the rest of the listview
content accordingly, i.e., in that sequence. I want to use the method,
which is activated by a column click.

So call that method in a context other than the one invoked in response
to a column click.
So I need code to simulate a click
of column five.

Thats like saying, if you've got a TextBox containing a number and a
button that causes the value in the TextBox to increase by 1 each time
you click it,

private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
TextBox1.Text = (int.Parse(TextBox1.Text) + 1).ToString();
}

that it somehow becomes impossible to have the statement

TextBox1.Text = (int.Parse(TextBox1.Text) + 1).ToString();

anywhere else in your code.
 
Z

Zach

private void makeStockList()
{
this.listView1.ListViewItemSorter = new ListViewItemComparer(5);
listView1.Sort();
// more code
}

What you suggested didn't work. The sollution turned out to be the above.
Many thanks,
Zach.

Registered User said:
private void makeStockList()
{
listView1_ColumnClick(listView1, ?
listView1.ColumnHeaderCollection[5]) ? ;
// more code
}

#region Sorting columns.
private void listView1_ColumnClick(object sender,
ColumnClickEventArgs e)
{
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
listView1.Sort();
}
#endregion

Re the above, I want to click column header 5 by means of code. What I
have
between ?? doesn't work. Which parameter would work?

The second argument in call to the event handler must be of type
ColumnClickEventArgs. Try something like the untested code below.

ColumnClickEventArgs args = new ColumnClickEventArgs();
args.Column = 5;
listView1_ColumnClick(this, args);

regards
A.G.
 
J

Jeff Johnson

private void makeStockList()
{
this.listView1.ListViewItemSorter = new
ListViewItemComparer(5);
listView1.Sort();
// more code
}

What you suggested didn't work. The sollution turned out to be the above.

Exactly. The ONLY thing the event handler was giving you was the
ColumnClickEventArgs instance which told you which column was being clicked.
You then pass that index to the ListViewItemComparer constructor. If you
know the index beforehand (which you do), you can simply pass that known
value.

Event handlers are never magical. The closest thing to a "magical" event
handler is the Paint event handler, because you're passed a PaintEventArgs
instance that you normally wouldn't have otherwise. So anything that goes on
in an event handler can usually be duplicated somewhere else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top