How to have DataGrid AutoScroll without stealing the focus ?

G

Guest

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.
 
J

Jeffrey Tan[MSFT]

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.
 
J

Jeffrey Tan[MSFT]

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.
 
G

Guest

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?
 
J

Jeffrey Tan[MSFT]

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 is VScrollBar)
{
SendMessage(this.dataGrid1.Handle, WM_VSCROLL, SB_BOTTOM,
this.dataGrid1.Controls.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.
 
S

Steven Nagy

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
 
J

Jeffrey Tan[MSFT]

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.
 
J

Jeffrey Tan[MSFT]

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.
 
G

Guest

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.
 
J

Jeffrey Tan[MSFT]

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.
 
G

Guest

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/library/system.windows.forms.datagrid.currentcell.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).
 
G

Guest

One more solution that also steals the focus:

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

Jeffrey Tan[MSFT]

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.
 
G

Guest

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.
 
J

Jeffrey Tan[MSFT]

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.
 
G

Guest

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.OnControlAdded);

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???
 
J

Jeffrey Tan[MSFT]

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.
 
J

Jeffrey Tan[MSFT]

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.
 

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

Top