By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

How to have DataGrid AutoScroll without stealing the focus ?

P: n/a
I have added an auto scroll feature to my DataGrid control like this:
private void DoAutoScroll()
{
DataView dv = m_DataGrid.DataSource as DataView;
DataGridCell cell = m_DataGrid.CurrentCell;
cell.RowNumber = dv.Count;
m_DataGrid.CurrentCell = cell;
}

But this the last line of this code is putting the focus on the current
cell, and it steal the focus from other control that uses the focus.

How can I still make have the auto scroll feature without stilling the focus?

If I new how has the focus before I call to the above DoAutoScroll(), then I
could give the focuses back.
---------
Thanks
Sharon
Jan 9 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
Hi Sharon,

Thanks for your post.

Yes, I think we can use Form.ActiveControl property to get and set the
focused control, code like this:

private void DoAutoScroll()
{
CurrencyManager cm = this.dataGrid1.BindingContext[this.dataSet11.jobs,
""] as CurrencyManager;
DataGridCell cell = this.dataGrid1.CurrentCell;
cell.RowNumber = cm.Count-1;
this.dataGrid1.CurrentCell = cell;
}

private void invokemethod()
{
Control focusControl=this.ActiveControl;
DoAutoScroll();
this.ActiveControl=focusControl;
}
Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 10 '06 #2

P: n/a
Additionally, in my code snippet, as you can see, I did not use DataView
way, but use the CurrencyMananger to get the row count. Also, we should use
cm.Count-1 to get the last row number.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 10 '06 #3

P: n/a
Thank a lot Jeffrey for your help.

It's very good that I can tell who has the focus so I'll be able to restore
it. But It's still causing the focus to blink away from the control that I
steal the focus from, and it causes the user very uncomfortable.

Therefore I wish to use the DataGrid vertical Scrollbar, So I did the
following:

++(m_DataGrid.VScrollBar.Maximum);
m_DataGrid.VScrollBar.Value = m_DataGrid.VScrollBar.Maximum;

But is causes the scrollbar to move down, but still may line are below and
not shown like they should.

How can I scroll all the way down using the vertical scrollbar of the
DataGrid?
---------
Thanks
Sharon
Jan 10 '06 #4

P: n/a
Hi Sharon,

Thanks for your feedback.

Then, we can get this done with the code snippet below

private void DoAutoScroll()
{
for(int i=0;i<this.dataGrid1.Controls.Count;i++)
{
if(this.dataGrid1.Controls[i] is VScrollBar)
{
SendMessage(this.dataGrid1.Handle, WM_VSCROLL, SB_BOTTOM,
this.dataGrid1.Controls[i].Handle);
}
}
}

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr
wParam, IntPtr lParam);

const int WM_VSCROLL=0x0115;
IntPtr SB_BOTTOM=(IntPtr)7;
private void callingmethod()
{
DoAutoScroll();
}

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 11 '06 #5

P: n/a
Hey Jeffrey, this post interested me.
How can I find out more about which DLLs (in your eg, user32.dll) can
do what?
With the SENDMESSAGE call you were able to interact with the window
(datagrid).
I would never have guessed that something like that would be in
user32.dll, but then I don't know anything about these DLLs.
But if I could learn which ones are more commonly used and are more
useful, I could improve my coding skills.

I have interest in creating a windows application that allows me to
click on a window and resize it. For example, I would like to be able
to click on an Internet Explorer window and then click on my
application (maybe from the systray) and choose a different size, thus
resizing the IE window. I assume it would be a similar call to the one
you made, except with different constants.

Many thanks,
Steven Nagy

Jan 11 '06 #6

P: n/a
Hi Steven,

Net winform is just a wrapper around legacy Win32 SDK GUI, this technology
I provided just send a WM_VSCROLL message(with SB_BOTTOM) to datagrid,
datagrid will response to this message and scroll the vertical scrollbar to
the bottom. For more information, please see WM_VSCROLL in MSDN.

If you are curious with Win32 SDK technology, "Programming Windows" writen
by "Charles Petzold" should be definite bible.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 11 '06 #7

P: n/a
Nice one, thanks.

What do you say about this one:

http://www.thescarms.com/dotNet/ScrollDataGrid.asp

--
Thanks
Sharon
Jan 11 '06 #8

P: n/a
Hi Sharon,

Thanks for your feedback.

Yes, I have seen this protected method yesterday. But this requires us
deriving from the DataGrid, which is not accepted by everyone. Anyway, if
you want to live with this solution, feel free to do it. Actually,
GridVScrolled protected method internally uses ScrollWindow API to do the
scrolling work.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 12 '06 #9

P: n/a
Hi Jeffrey,

Actually I am deriving from the DataGrid for other reasons, so did tried
this solution and the auto scrolling works, BUT still it steals the focus for
every scroll.

So I'll give a try to your suggestion earlier as well.

-------
Thanks
Sharon
Jan 12 '06 #10

P: n/a
Ok, if you still have anything unclear, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 13 '06 #11

P: n/a
Hi,

I tried all the solution I could find to do the auto scroll, but they all
still focus.
(1) Using the CurrentCell steals focus and it also mentioned in the MSDN
documentation.
see
http://msdn2.microsoft.com/en-us/lib...rrentcell.aspx
(2) Using the vertical scroll bar also fails:
++(DataGrid.VScrollBar.Maximum);
DataGrid.VScrollBar.Value = DataGrid.VScrollBar;
(see my earlier 2'nd post)
(3) Using the SendMessage() also steals the focus.
(4) Deriving from the DataGrid and using the GridVScrolled() also steals the
focus
(see http://www.thescarms.com/dotNet/ScrollDataGrid.asp)

So I run out of options and the users are very angry.

Does anybody knows how to auto scrolling WITHOUT stealing focus (attention
freak feature).

-------
Thanks
Sharon
Feb 7 '06 #12

P: n/a
One more solution that also steals the focus:

DataGrid.CurrentRowIndex = (DataGrid.DataSource as DataView).Count - 1;

------
Thanks
Sharon
Feb 7 '06 #13

P: n/a
Hi Sharon,

I am not sure why SendMessage does not work for you. As we negotiated in
the original replies, this solution should work for you.(Actually, it works
well on my side.) If not, can you show me a little sample to demonstrate
the problem?

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Feb 8 '06 #14

P: n/a
I have this ElementsDataGrid class which is in ElementList class:

public class ElementList : System.Windows.Forms.UserControl
{
private class ElementsDataGrid : System.Windows.Forms.DataGrid
{
internal void ScrollToRow(int aRowNum)
{
this.GridVScrolled(this, new
ScrollEventArgs(ScrollEventType.LargeIncrement, aRowNum));
}
}

private bool m_autoScroll;
private InfraControls.ElementList.ElementsDataGrid m_DataGrid;

[Description("Determines whether the grid will scroll down when a new row
is added."),
Category("Layout")]
public override bool AutoScroll
{
get { return m_autoScroll; }
set { m_autoScroll = value; }
}

public void SetDataSource(DataView data)
{
m_DataGrid.DataSource = data;
data.AllowNew = false;
}

private void DoAutoScroll()
{
if( m_autoScroll )
{
m_DataGrid.ScrollToRow((m_DataGrid.DataSource as DataView).Count-1);
}
}

private void AddDefects(object aSender, ArrayList aDefectRows)
{
DoAutoScroll();
}
}

Defects are added to the DataSet, causing the ElementsDataGrid to be added
with more rows and invoking the AddDefects() that invoke the DoAutoScroll().
And the DoAutoScroll() is invoked causing the vertical scrollbar of the
ElementsDataGrid to move --> the focus is taken to the ElementsDataGrid. And
this is the problem I wish to avoid.

The code is larger then what I have posted in here, I couldn’t post all of
it in here (much too much). I hope this post can help resolving this problem.
-----
Thanks
Sharon
Feb 8 '06 #15

P: n/a
Hi Sharon,

I have added a reply to you in post "Re: How do I scroll DataGrid to
specified row index?". Please check it there, I will follow up with you.
Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Feb 9 '06 #16

P: n/a
Hi Jeffrey,

I tried to change the code to do the AutoScroll using the SendMessage (for
the second time) as you posted before. But still the DataGrid steals the
focus whenever the AutoScroll is moving the vertical scrollBar.

If I remove the line of code with the SendMessage() --> The focus stays
where is should be.

But I found somthing new:

I'm handeling the event :
MyDataGrid.ControlAdded += new
System.Windows.Forms.ControlEventHandler(this.OnCo ntrolAdded);

private void OnControlAdded(object sender,
System.Windows.Forms.ControlEventArgs e)
{
e.Control.Enter += new EventHandler(OnGotFocus);
}

private void OnGotFocus(object sender, EventArgs e)
{
if( ((Control)sender).Focused )
{
// And in here I can tell that the DataGridTextBox stole the focus
when it was added.
}
}

This scenario:
One of the DataGris cells (DataGridTextBox ) is selected, but then I clicked
on another control outside the DataGrid. And now when the AutoScroll is
invoked, the focus is moved to the newly added. I can tell that because I
inserted a brakepoint to OnGotFocus() (see above) an in there I see that the
sender, which is a DataGridTextBox, has the focus.

I don't know how to convince the focus thief to stop it.

Can you???

--------
Thanks
Sharon
Feb 9 '06 #17

P: n/a
Hi Sharon,

Sorry, but have you tried the sample project I attached in the post titled
"Re: How do I scroll DataGrid to specified row index" ? Does it work on
your side?

I have to get this confirmation first to do further process. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Feb 10 '06 #18

P: n/a
No, I didn't try the sample project you attached in the post titled
"Re: How do I scroll DataGrid to specified row index"
(http://msdn.microsoft.com/newsgroups...-5eb8ba00b55d).

I simply do not know how to download something from this discussions forum.
I have Outlook 2003 and not Outlook Express.
Can you be more specific on how can I download it?
-------
Thanks
Sharon
Feb 12 '06 #19

P: n/a
Hi Sharon,

As I stated in the original post, we should use Outlook Express, not
Outlook. Outlook Express is a freeware installed with Windows, so every
machine should have it.

In the OE, we can select Tools->Account..., Add->News..., input your
display name etc...

The most important section is NNTP server, we should input
"msnews.microsoft.com". With this you can access the newsgroup using OE
now.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Feb 13 '06 #20

P: n/a
Hi Jeffrey,

Ok, I installed the OE and subscribed to the
"microsoft.public.dotnet.languages.csharp" newsgroup, But in the downloaded
conversation / messages I can not find the "How do I scroll DataGrid to
specified row index?" discussions
(http://msdn.microsoft.com/newsgroups...-5eb8ba00b55d).

I did found the "Re: How to have DataGrid AutoScroll without stealing the
focus ?"
(http://msdn.microsoft.com/newsgroups...f-cf23374d0ab4)
and "Re: How to have DataGrid AutoScroll without stealing the focus ?"
(http://msdn.microsoft.com/newsgroups...a-429af747e7ba)
But not all the other posts of the "How to have DataGrid AutoScroll without
stealing the focus ?" discussions.
How can I go directly to your post with the attachment?

--
Thanks
Sharon
Feb 13 '06 #21

P: n/a
Hi Sharon,

The post with the attachment is sent at 2.9. Your OE may not show posts in
that day. You can select Tools->"Get Next 300 Headers" option several times
to get the 2.9 replies.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Feb 14 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.