473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to have DataGrid AutoScroll without stealing the focus ?

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
21 9912
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
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
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
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
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
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
Nice one, thanks.

What do you say about this one:

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

--
Thanks
Sharon
Jan 11 '06 #8
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
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
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
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
One more solution that also steals the focus:

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

------
Thanks
Sharon
Feb 7 '06 #13
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: rawCoder | last post by:
Hi All, This is a repost of 'Disable DataGrid AutoScroll of 3/7/2005' Its related to "WinForm DataGrid" HOW can i force the grid NOT to Auto Scroll to the ( newly added item OR last...
1
by: John | last post by:
I'm trying to use the DrawText() method to draw some very long string text on the Panel with AutoScroll enabled. However, for some unknown reasons, I could not trigger the ScrollBar to show up. ...
2
by: Robert Misiak | last post by:
Is it possible to display a window on top without it stealing focus? (and I'm not talking about stealing focus from my application - I'm talking about it stealing focus from any application.) ...
5
by: MrNobody | last post by:
I want to know how to scroll a DataGrid so that a specific row index will be visible to the user?
0
by: rawCoder | last post by:
If an item is added in a grid, then the grid scrolls automatically either to bring the selected or the last entered item in focus. Is there a way to disable this scrolling. Means, If I am...
4
by: KB | last post by:
I realised that if I use datagrid and datasource is a datatable, when I created a new row and while on that row I exit the datagrid (ie. without moving into the next row) and focus onto another...
1
by: damonwischik | last post by:
I'm using GNU Emacs 21.3.1 with python-mode 1.0alpha under Windows XP. Whenever I execute a command in an edit window (with py-execute-region), the output window steals the focus. How can I stop...
7
by: Dave Booker | last post by:
I am using a WebBrowser object in my .NET 2.0 application, but it is not shown to the user. Every time a timer event triggers it to perform a m_WebBrowser.Navigate() I get that classic IE 'click'...
0
by: jereviscious | last post by:
Hi all I've added a combobox to the datagrid control using the following method dataGridTextBoxColumn7.TextBox.Controls.Add(comboControl); The problem is that when I tab to the Column...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.