Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
D

David Fleming

@David Fleming
About
Posts
87
Topics
31
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • DataGridView ComboBox: Allowing user to type in a value NOT in the Items collection.
    D David Fleming

    I found a little more info on this problem searching MS. Here's a comment:

    The DataGridViewComboBox must hold all possible values that it will display
    in it's items collection. For example, when binding to the Customers table
    and having the CompanyName field a combobox the combobox must contain all
    possible values. If the grid attempts to set the cell's value where the
    combobox cell doesn't have the value then we throw the dataError event.

    To workaround this, handle the DataError event of the DataGridView. For
    example:

    void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs
    e)
    {
    // ignore the data error occurring under the territoryComboBoxColumn
    if (e.ColumnIndex != territoryComboBoxColumn.Index)
    {
    MessageBox.Show("Data error occurs:" + e.Exception.Message);
    }
    }

    This tells us how to avoid the Exception. However, it doesn't address allowing the user's typed input to become the value for that cell. Any thoughts?

    C#

  • how read the prevoius row from the sqldatareader
    D David Fleming

    That's true if you only need to back up one record. Suppose you want to back up two.... The previous answer is, I think, more what the questioner needed. If you want to move forward AND backward, you need to use a DataTable, not a DataReader.

    C#

  • DataGridView ComboBox: Allowing user to type in a value NOT in the Items collection.
    D David Fleming

    This question has been asked, in one way or another, a few times and I've yet to see a solid answer. I've searched the articles and messages but don't see a solution. Here's my desire: A normal ComboBox, when the DropDownStyle is set to ComboBox rather than ComboBoxList, will allow the user to select an item from the Items list OR type in their own value. Near as I can tell, the DataGridView ComboBox performs validation to see if the Value matches an item in the list. If that Value is not found in the list, you get the error message: "System.ArgumentException: DataGridViewComboBoxCell value is not valid" Basically making the DataGridViewComboBoxCell function like a ComboBoxList -- in essence NOT allowing the user to type in a value that isn't in the list. Is there a way around this? Is there a way for a DataGridViewComboBoxCell to allow the user to EITHER select a Value from the list OR type in a new value that is NOT already in the list? I am definitely NOT the only one out here with this question / problem, so if you have any ideas on a way around it, PLEASE let us all know. Thanks a bunch.

    C#

  • How to add TimeSpan to a DatTime
    D David Fleming

    Two things: 1) Make sure calTimeStamp does not equal zero because the following works fine for me: int calTimeStamp = 30; //so that I know I have a value DateTime dtStart = DateTime.Now; //I need a start time DateTime da = dtStart + new TimeSpan(0, 0, calTimeStamp); //this adds 30 seconds to dtStart 2) Using TimeSpan(0, 0, variable) gives you seconds not milliseconds. For milliseconds you have to use TimeSpan(days, hours, seconds, milliseconds);

    C#

  • Disabling an unbound DataGridViewCell
    D David Fleming

    I've searched around CodeProject and the boards but haven't found a solution to my problem. I have a DataGridView that is unbound. Based on selections in the first column from a ComboBox, other cells in that row may be set to ReadOnly. What I would like to do is skip over any ReadOnly cell when the user presses Tab (or Shift-Tab). I can get the selection to go where I want it to go, but the input focus still goes to the next cell even if it is ReadOnly. Example: The current cell is [0, 0]. Based on what I selected, the next cell ([1, 0]) is ReadOnly. I press Tab. What I want is for the selection and the input focus to move to cell [2, 0]. If I set the .CurrentCell to [2, 0] and then set .CurrentCell.Selected = true, I get the selection to highlight in the right spot, but the input focus still goes to cell [1, 0]. How do I completely skip over a specific cell? As an added bonus, it would be nice if when the user clicks on a ReadOnly cell, that the selection and input focus goes to the next cell instead of the ReadOnly cell. Can it be done? If so, how? I hope what I'm describing makes sense. Thanks.

    C#

  • How do I create a ProperyGrid subcategory?
    D David Fleming

    I know that to display a custom control's attribute in a specific category in the PropertyGrid you use:

    [Browsable(true), Category("SomeCategory"), Description("Some info....")]

    What I would like to do is have one overriding category for my control's attributes (such as "Control Specific") and then have subcategories for more specific things (such as "Colors"). Sort of like under "Appearance" there is "Font". How do I do that? Thanks.

    C#

  • Custom Draw in ListView.
    D David Fleming

    The struct I had defined was pretty much the same except for the COLORREF parts. I had seen on PInvoke what you said about COLORREF being an int. The good news is that the memory exception went away when I fixed all the structs (using mine as well as your new one). The bad news is that it still isn't actually doing anything. I'll include my overridden WndProc for you to look at, and I'll make a couple of comments on it below:

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            //Always perform default functionality
            base.WndProc(ref m);
    
            //Check to see if it is a WM\_NOTIFY msg
            if (m.Msg == WM\_NOTIFY)
            {
                //NMHDR nmHdr = new NMHDR();
                //nmHdr = (NMHDR)m.GetLParam(typeof(NMHDR));
                nmlvCust = (NMLVCUSTOMDRAW)m.GetLParam(typeof(NMLVCUSTOMDRAW));
                if (nmlvCust.nmcd.hdr.code == NM\_CUSTOMDRAW)
                {
                    //Have windows do default drawing in case we don't make any changes
                    m.Result = (IntPtr)CDRF\_DODEFAULT;
                    Debug.WriteLine("In CustomDraw. dwDrawStage = " + nmlvCust.nmcd.dwDrawStage  ". Result = " + m.Result);
    
    
                    if (nmlvCust.nmcd.dwDrawStage == CDDS\_PREPAINT)
                    {
                        //We will notify windows that we want messages for each item
                        m.Result = (IntPtr)CDRF\_NOTIFYITEMDRAW;
                        Debug.WriteLine("In PrePaint. dwDrawStage = " + nmlvCust.nmcd.dwDrawStage + ". Result = " + m.Result);
                    }
                    else if (nmlvCust.nmcd.dwDrawStage == CDDS\_ITEMPREPAINT)
                    {
                        //We will notify windows that we want messages for each subitem
                        m.Result = (IntPtr)CDRF\_NOTIFYSUBITEMDRAW;
                        Debug.WriteLine("In ItemPrePaint. dwDrawStage = " + mlvCust.nmcd.dwDrawStage + ". Result = " + m.Result);
                    }
                    else if (nmlvCust.nmcd.dwDrawStage == (CDDS\_ITEMPREPAINT | CDDS\_SUBITEM))
                    {
                        //Here's where we do our stuff
                        if (nmlvCust.iSubItem == 0)
                        {
                            nmlvCust.clrText = ColorTranslator.ToWin32(SystemColors.Control);
                            nmlvCust.clrTextBk = ColorTranslator.ToWin32(SystemColors.Info);
                        }
    
    C#

  • Custom Draw in ListView.
    D David Fleming

    Boy, I hope you're not getting irritated with this thread yet because I like learning stuff. I'm just wondering if you've ever actually attempted what I'm talking about here. Your original response, and the associated links, made it seem as though this might be fairly easy. Unfortunately, the more I learn about it, the harder it seems to be getting. I was thinking about writing an article discussing OwnerDraw vs CustomDraw to customize the look of a ListView, but the CustomDraw thing seems to be monumental. Just from the standpoint of defining all the structs involved: NMHDR is easy, NMCUSTOMDRAW is fairly straightforward but even that also involves a RECT structure; don't even get me started on NMLVCUSTOMDRAW which involves RECT and COLORREF which itself is fairly complex. I am persistently running into unhandled exceptions involving read/write to protected memory which I am figuring must be due to not having completely figured all the structs out yet. I retrieve the LParam using:

                nmlvCust = (NMLVCUSTOMDRAW)m.GetLParam(typeof(NMLVCUSTOMDRAW));
    

    and put it back into the LParam using:

                Marshal.StructureToPtr(nmlvCust, m.LParam, true);
    

    But somewhere along the chain of messages, at the GetLParam point, I get the access violation. If you have any suggestions or ideas, please offer. If not, then I figure I'll abandon this for now and perhaps come back to it some other time. Regardless, thanks for your input -- it has certainly pointed me into areas I've not explored. It's been pretty cool thus far. Thanks.

    C#

  • Custom Draw in ListView.
    D David Fleming

    OK. I answered my own question. In case anyone else out there runs into this sort of problem, here's what I found (and it seems to be working):

            //Check to see if it is a WM\_NOTIFY msg
            if (m.Msg == WM\_NOTIFY)
            {
                NMHDR nmHdr = new NMHDR();
                nmHdr = (NMHDR)m.GetLParam(typeof(NMHDR));
                if (nmHdr.code == NM\_CUSTOMDRAW)
                {
                    Debug.WriteLine("Got here.");
                }
            }
    

    But now I have a different question (there's always something): Where can I find all the info on the struct.s I need (including, but not necessarily limited to: NMHDR, NMCUSTOMDRAW and NMLVCUSTOMDRAW)? I can find them easily enough by searching MS KB, but is there a file somewhere that I can cut and paste from or just include in my code that already has all of this stuff defined (in C#)? Manually coding all of the structs is kind of a pain. Thanks.

    C#

  • Custom Draw in ListView.
    D David Fleming

    Hmm. I'm sure this is a totally rookie problem. I've done my fair share of casting in C++, but I'm pretty new to C#. How do I cast the lParam to my NMHDR struct in C#? In my class (which per the link you supplied is derived from NativeWindow), I have NMHDR defined:

        private struct NMHDR
        {
            IntPtr hwndFrom;
            IntPtr idFrom;
            UInt32 code;
        }
    

    Then in the overridden WndProc, I have:

            //Check to see if it is a WM\_NOTIFY msg
            if (m.Msg == WM\_NOTIFY)
            {
                //Convert the lParam (within m) to NMHDR struct
            }
    

    And since I am pretty new to C#, I don't know the syntax for this casting.

    C#

  • Custom Draw in ListView.
    D David Fleming

    Wow! One of the best answers I've received here at CodeProject. Thanks a bunch for the links. I'll work through it and see what I can do. Thanks again.

    C#

  • Custom Draw in ListView.
    D David Fleming

    There's an excellent article here on CodeProject by Michael Dunn about intercepting NM_CUSTOMDRAW messages to modify the look of a ListView -- not as involved as OwnerDraw, though, which is nice. Here's the link: http://www.codeproject.com/listctrl/lvcustomdraw.asp[^] Unfortunately, the article is not written for C#. I was wondering how to hook into the message stream to intercept and act on NM_CUSTOMDRAW messages in C#. I asked at the article, but Michael does not know. I've seen the example code in the C# help on using OwnerDraw, but that of course requires handling almost all of the drawing chores. Can we, in C#, hook into the message stream and just do custom draw type stuff like described in Michael's excellent article? If so, how? What event handler do I need? Where do I declare it? Stuff like that. Or do I have to create a ListView derived class and override the WndProc? (which I'd rather not do if I can avoid it)

    C#

  • Putting image to right of text in ListView ColumnHeader.
    D David Fleming

    How do I go about putting an image (in my case, I want an arrow denoting direction of sort) in the header of a ListView TO THE RIGHT of the text? Is it possible without resorting to using APIs? If not, is it pretty much like doing it in VC++ (which I have seen code on here at CodeProject)? Can you direct me to an example? Secondary question... How do I change the background color of a column in a ListView? Again, can it be done without APIs? Is it done like in VC++? Where can I find an example? Thanks.

    C#

  • How do I change a control's content through the Controls collection?
    D David Fleming

    Thanks for the suggestion, but it turns out the problem was something totally different. The textbox in question is part of a CustomControl, and when I declared the public Attribute for Text I accidentally used the "new" keyword instead of "override". So, when I set .Text for the control, it was not actually setting the .Text for the TextBox part of the CustomControl like it was supposed to. Turns out the code snippet I put in my original message DOES work just fine. Thanks anyway for the response.

    C#

  • How do I change a control's content through the Controls collection?
    D David Fleming

    Here are two different code approaches I have used, but neither works.

    int i = thisForm.GetChildIndex(textBox);
    thisForm.Controls[index].Text = "Something";
    thisForm.Controls[index].Invalidate();

    and

    Control controlTest = thisForm.textBox;
    controlTest.Text = "Something";
    controlTest.Invalidate();

    The reason I'm trying to do this is that I would like to be able to "map" controls to fields in a database (I'd like to do it myself rather than do data-binding), so I need a way to be able to create a list (or other collection) of controls and the data field to put in that control. It is rather like UpdateData in VC++. But I can't seem to update the screen with code like what I've shown above. How do I go about this? I hope the question makes sense because I'm sure one of you guys knows how to do it if I can just figure the right way to ask the question. Thanks.

    C#

  • A "fly-out" toolbar like in VS2005
    D David Fleming

    I'm not really sure where best to ask this question (and these days if you ask a question in the wrong forum, you're asking for a good flaming). I'm wondering if there is a control already available to create a fly-out toolbar like in VS2005 -- you know, like they do for the toolbox, properties, solution explorer, etc. I like the way it is docked to the side, and the way it autohides, but can also be pinned to stay visible. Is there already a control for that? If not, then my next question becomes if I was to try to write one myself, what do you think I should inherit from -- should I derive from a dialog, a toolstrip, a groupbox, a splitter or completely write it from scratch? Anyone have any ideas or suggestions?

    Article Writing

  • Extremely basic ? about VS 2005.
    D David Fleming

    I am currently familiar w/ VC 6.0. I downloaded the trial of VS 2005 Express (just C++) to try out. I created a new "Windows Forms App" and added a couple of controls. When I add an event handler (such as _Click for a button), the IDE creates the entry in the .h file, but I don't see a .cpp file. Here's what's in the .h file:

    private: System::Void button1_Click(....

    I don't think I am now supposed to put my code in the .h file, so... 1) am I supposed to create the .cpp file myself? or 2) did I miss something such that the .cpp file did not get created when it should have? I know this is really simplistic, and I feel like I'm just overlooking something, but some advice to point me in the right direction would be nice. Thanks. PS: One other simple thing. I figured out the following to do a simple MessageBox:

    System::Windows::Forms::MessageBox::Show("Button pressed!");

    Surely there's a shorter way...! Thanks again.

    C / C++ / MFC

  • Creating a colored, transparent CStatic
    D David Fleming

    OK, it looks like I solved my own problem. Just in case anyone else is interested, here's what I found:

    CPaintDC dc(this); //get a DC for the control
    //The next several lines create a compatible bitmap in memory
    // and draw a solid light green "bitmap" on it,
    // then select that bitmap into the DC in memory
    pDCMem = new CDC;
    pDCMem->CreateCompatibleDC(&dc);
    bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
    pOldBitmap = pDCMem->SelectObject(&bmp);
    CBrush brush;
    brush.CreateSolidBrush(RGB(122,255,122));
    pDCMem->FillRect(rc, &brush);
    //After drawing on the DC in memory (text and whatever else)
    // use BitBlt to merge the bitmap in memory with what's on screen
    dc.BitBlt(0,0,rc.Width(),rc.Height(),pDCMem,0,0,SRCAND);
    //SRCAND combines the src and dest DCs using boolean AND
    //Then clean up
    pDCMem->SelectObject ( pOldBitmap ) ;
    delete pDCMem;

    And it works. Now the CStatic is light green, with text on it (which I did not include in the code snippet) and the bitmap "behind" it can be seen greenishly colored through it.

    C / C++ / MFC

  • Creating a colored, transparent CStatic
    D David Fleming

    I've been looking around in the articles on CStatics as well as in stuff on Bitmaps and the GDI, but I'm not quite finding what I need, so here goes.... What I would like to create is a CStatic-derived class that will make what looks like a partially transparent CStatic. Think of it as a piece of colored glass with text written on it, hovering over the background. I can handle making a CStatic transparent (there are a couple of nifty articles on that). In essence, taking an image of the part of the parent control behind the CStatic and painting that as the background of the CStatic. To be more explicit, here's a code snippet for doing basic transparent CStatic: This is in OnPaint for the CStatic-derived class (so dc is the supplied DC):

    CDC\* pdcT;
    pdcT = new CDC;
    pdcT->CreateCompatibleDC(&dc);
    CBitmap bitmap;
    CBitmap\* pOldBitmap;
    pOldBitmap = pdcT->SelectObject(&bitmap);
    dc.BitBlt(0, 0, rc.Width(), rc.Height(), pdcT, 0, 0, SRCCOPY);
    pdcT->SelectObject(pOldBitmap);
    delete pdcT;
    

    So what I need to be able to do is modify that "parent" image to add a wash of color, then paint that to the background of the CStatic. It seems that I need to do the modifying of the bitmap after it has been selected into the pdcT, but I don't know where to go on that. Or perhaps I'm missing something that I can do in the BitBlt function to accomplish this. Can it be done? And if so, how? Thanks.

    C / C++ / MFC

  • Getting ptr to IHTMLDocument2 from IHTMLElement?
    D David Fleming

    Excellent. Thanks. But now I have another question. What I'm trying to do is implement a HTMLEditDesigner and trap keyboard input in the TranslateAccelerator event. If the user is moving into an element that is "off limits", I want to cancel it. The elementFromPoint() method is using the mouse coordinates (which I did not realize upon reading it, but... duh!). My question is: let's say the user is on a line below a restricted line and presses the up arrow key (thus moving into a restricted area -- or at least attempting to); I trap the keydown event in the TranslateAccelerator callback, but it seems to me that the element I get (using pIEventObj->getSrcElement) always has BODY as the tag (using pElement->getTagName), even if the cursor is moving into a BOLD, DIV, etc. What's the snag or trick? Why can't I seem to get the actual tag? BTW, it works fine on mouse clicks trapped in the PretranslateEvent callback, but this callback does not seem to trap arrow key presses (my understanding is that that is what TranslateAccelerator is for, and supposedly it is called before PretranslateEvent). Thanks.

    C / C++ / MFC
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups