Click here to Skip to main content
15,170,558 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? Pin
dojohansen4-Nov-09 11:08
Memberdojohansen4-Nov-09 11:08 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? Pin
Pete O'Hanlon4-Nov-09 11:21
mvaPete O'Hanlon4-Nov-09 11:21 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? Pin
PIEBALDconsult4-Nov-09 11:45
professionalPIEBALDconsult4-Nov-09 11:45 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? Pin
dojohansen4-Nov-09 14:22
Memberdojohansen4-Nov-09 14:22 
GeneralRe: Is it possible to add an interface implementation to precompiled types at runtime? [modified] Pin
PIEBALDconsult4-Nov-09 16:07
professionalPIEBALDconsult4-Nov-09 16:07 
Questionhow to use towords() function in crystal reports...... Pin
PravinYog4-Nov-09 1:14
MemberPravinYog4-Nov-09 1:14 
QuestionChanging the colour of scroll bar Pin
deeptishete4-Nov-09 1:10
Memberdeeptishete4-Nov-09 1:10 
AnswerRe: Changing the colour of scroll bar Pin
dojohansen4-Nov-09 1:57
Memberdojohansen4-Nov-09 1: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 Attacker4-Nov-09 0:42
MemberLast Attacker4-Nov-09 0:42 
Questionaccessing windows controls through application Pin
Innayat Ullah4-Nov-09 0:36
MemberInnayat Ullah4-Nov-09 0:36 
AnswerRe: accessing windows controls through application Pin
Giorgi Dalakishvili4-Nov-09 1:09
mentorGiorgi Dalakishvili4-Nov-09 1:09 
GeneralRe: accessing windows controls through application Pin
Innayat Ullah8-Nov-09 19:47
MemberInnayat Ullah8-Nov-09 19:47 
Questionssssssssssssssssssssss Pin
zhuangkai4-Nov-09 0:08
Memberzhuangkai4-Nov-09 0:08 
AnswerRe: ssssssssssssssssssssss Pin
padmanabhan N4-Nov-09 0:11
Memberpadmanabhan N4-Nov-09 0:11 
GeneralRe: ssssssssssssssssssssss Pin
OriginalGriff4-Nov-09 2:19
mveOriginalGriff4-Nov-09 2:19 
GeneralRe: ssssssssssssssssssssss Pin
padmanabhan N4-Nov-09 2:32
Memberpadmanabhan N4-Nov-09 2:32 
GeneralRe: ssssssssssssssssssssss Pin
OriginalGriff4-Nov-09 2:37
mveOriginalGriff4-Nov-09 2:37 
QuestionSort Multi-Pges tiff file using C# Pin
sher_azam4-Nov-09 0:02
Membersher_azam4-Nov-09 0:02 
AnswerRe: Sort Multi-Pges tiff file using C# Pin
Richard MacCutchan4-Nov-09 0:07
mveRichard MacCutchan4-Nov-09 0:07 
GeneralRe: Sort Multi-Pges tiff file using C# Pin
sher_azam4-Nov-09 0:17
Membersher_azam4-Nov-09 0:17 
AnswerRe: Sort Multi-Pges tiff file using C# Pin
sher_azam5-Nov-09 1:45
Membersher_azam5-Nov-09 1:45 
QuestionRegular Expression Pin
Inderjeet Kaur3-Nov-09 23:28
MemberInderjeet Kaur3-Nov-09 23:28 
AnswerRe: Regular Expression Pin
padmanabhan N3-Nov-09 23:34
Memberpadmanabhan N3-Nov-09 23:34 
QuestionHow do you convert a number to a string in Crystal Reports? Pin
PravinYog3-Nov-09 23:25
MemberPravinYog3-Nov-09 23:25 
AnswerRe: How do you convert a number to a string in Crystal Reports? Pin
Calla3-Nov-09 23:29
MemberCalla3-Nov-09 23: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.