Click here to Skip to main content
15,892,805 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? Pin
dojohansen4-Nov-09 10:08
dojohansen4-Nov-09 10:08 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? Pin
Pete O'Hanlon4-Nov-09 10:21
mvePete O'Hanlon4-Nov-09 10:21 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? Pin
PIEBALDconsult4-Nov-09 10:45
mvePIEBALDconsult4-Nov-09 10:45 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? Pin
dojohansen4-Nov-09 13:22
dojohansen4-Nov-09 13:22 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? [modified] Pin
PIEBALDconsult4-Nov-09 15:07
mvePIEBALDconsult4-Nov-09 15:07 
Questionhow to use towords() function in crystal reports...... Pin
PravinYog4-Nov-09 0:14
PravinYog4-Nov-09 0:14 
QuestionChanging the colour of scroll bar Pin
deeptishete4-Nov-09 0:10
deeptishete4-Nov-09 0:10 
AnswerRe: Changing the colour of scroll bar Pin
dojohansen4-Nov-09 0:57
dojohansen4-Nov-09 0:57 
Hi,

I only had a look at DataGridView but suppose it is similar for the other controls. The grid does not expose the scrollbar objects, it only exposes the ScrollBars property which is just an enum to define if you want no scrollbars, horizontal/vertical only, or both.

Hence, there is no API for doing what you want to. You can access the scrollbar via the Controls collection, but be aware that if the implementation details of the DataGridView changes, your code may break.

The absolutely easiest and worst way is to simply index into this collection. I tried this code:

void foo()
{
   var g = new DataGridView();
   ToString();
}


The second line is just a dummy for me to set a breakpoint so I could have a look in the watch list. Set a breakpoint there, make sure something calls foo(), and run. Then add the watch g.Controls and you'll see that by default the grid contains two controls, the horizontal and the vertical scrollbar. Hence, this code would succeed for such a grid:

g.Controls[0].BackColor = Color.Red;

However, the scrollbar may of course not be the first control of the datagrid in all situations, such as when we've databound it. A more robust solution would be to search a part of the control tree for a given type and use that to find the scrollbar. Then your code would depend only on the grid or whatever control contains it having one and only one sub-control of type HScrollBar (for the horizonal one).

Let's put this general code in a separate class so it can be reused, and call it Util for now (you may want to bundle this method with related functionality and make it into a ControlUtil or WinFormsUtil or whatever).

static public class Util
{
    static public T Find<T>(Control container) where T : Control
    {
        foreach (Control child in container.Controls)
            return (child is T ? (T)child : Find<T>(child));
        // Not found.
        return null;
    }
}


Now you can use this to find and access subcontrols not directly exposed by the container. You may also want to add a FindAll method to find all subcontrols of a particular type, or one that searches by using a predicate function so you can select a subcontrol based on it's property values rather than only on type. But you get the idea, hopefully.

Usage: Util.Find<HScrollBar>(dataGridView1).BackColor = Color.Red;

Remember however that this approach is not guaranteed to work if and when you move to a new framework version. The grid does not expose any HScrollBar and is free to replace this with some other implementation in the future, in which case your code would break.

Also, for the Panel or other container controls, this won't be enough if the control contain any other controls that have scrollbars! Find here simply returns the first subcontrol found of the given type, and it searches depth first (i.e. if the first subcontrol is not the given type, search it's subcontrols before continuing to the next sibling). But you can adapt this to search level by level if you want, or restrict it to searching only the direct children of the container (I don't know if a Panel's scrollbars are direct children, but it's likely) or use any other method of your choosing to find the appropriate object. In any case you can use the central idea of gaining access to the subcontrol by finding it in the control subtree - as long as you accept that this logic may not be valid forever and might have to change if the controls do.
QuestionHow do you bind with a textbox to a Nullable&lt;decimal&gt; datasource property? Pin
Last Attacker3-Nov-09 23:42
Last Attacker3-Nov-09 23:42 
Questionaccessing windows controls through application Pin
Innayat Ullah3-Nov-09 23:36
Innayat Ullah3-Nov-09 23:36 
AnswerRe: accessing windows controls through application Pin
Giorgi Dalakishvili4-Nov-09 0:09
mentorGiorgi Dalakishvili4-Nov-09 0:09 
GeneralRe: accessing windows controls through application Pin
Innayat Ullah8-Nov-09 18:47
Innayat Ullah8-Nov-09 18:47 
Questionssssssssssssssssssssss Pin
zhuangkai3-Nov-09 23:08
zhuangkai3-Nov-09 23:08 
AnswerRe: ssssssssssssssssssssss Pin
padmanabhan N3-Nov-09 23:11
padmanabhan N3-Nov-09 23:11 
GeneralRe: ssssssssssssssssssssss Pin
OriginalGriff4-Nov-09 1:19
mveOriginalGriff4-Nov-09 1:19 
GeneralRe: ssssssssssssssssssssss Pin
padmanabhan N4-Nov-09 1:32
padmanabhan N4-Nov-09 1:32 
GeneralRe: ssssssssssssssssssssss Pin
OriginalGriff4-Nov-09 1:37
mveOriginalGriff4-Nov-09 1:37 
QuestionSort Multi-Pges tiff file using C# Pin
sher_azam3-Nov-09 23:02
sher_azam3-Nov-09 23:02 
AnswerRe: Sort Multi-Pges tiff file using C# Pin
Richard MacCutchan3-Nov-09 23:07
mveRichard MacCutchan3-Nov-09 23:07 
GeneralRe: Sort Multi-Pges tiff file using C# Pin
sher_azam3-Nov-09 23:17
sher_azam3-Nov-09 23:17 
AnswerRe: Sort Multi-Pges tiff file using C# Pin
sher_azam5-Nov-09 0:45
sher_azam5-Nov-09 0:45 
QuestionRegular Expression Pin
Inderjeet Kaur3-Nov-09 22:28
Inderjeet Kaur3-Nov-09 22:28 
AnswerRe: Regular Expression Pin
padmanabhan N3-Nov-09 22:34
padmanabhan N3-Nov-09 22:34 
QuestionHow do you convert a number to a string in Crystal Reports? Pin
PravinYog3-Nov-09 22:25
PravinYog3-Nov-09 22:25 
AnswerRe: How do you convert a number to a string in Crystal Reports? Pin
Calla3-Nov-09 22:29
Calla3-Nov-09 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.