Appointment Calendar for a month on one form

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
Hi,

I am trying to design a monthly calendar to be shown on a form at once.

I can do a week using a datagridview, showing each appointment by time and day of the week, but with 31 days it seems difficult to decide which way I should do it.

Do I add 31 datagridviews to a single form, that may be slow to draw so many all at once and show in one form?

Or compress all appointments into 1 string and show a rectangle for each day of the month, which could be easier to do and run a lot faster.

I'd be interested in what people think and a pointer in the best direction? Or even screenshot of how one might look?

kind regards
G
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
I don't really want to go down a blind alley, so hoping someone might have tried it and decided the best way to go!

I'm thinking now that I will use a datagridview with large cells there is a cell for each day of the month and I need a way to show the appointments for that day as a single line in each cell.
Can anyone help with the code?
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
If you want something that the DataGridView does not already do then you can look into creating your own custom column, cell and editing control classes. Another option for tabular layout, if you're not editing in-place, is a TableLayoutPanel. Each cell can only contain one direct child control so, if you want something complex, you can add a Panel and then add an arbitrary number of children to that. Most likely though, you'd be better off creating a user control that already contains the children you want, then adding instances of that to the cells. I can imagine a situation where you have a Panel with AutoScroll set to True on the form and a TableLayoutPanel in that, then a FlowLayoutPanel in each cell and then an arbitrary number of your own user control in that. If you familiarise yourself with what controls are available and what they can do first, then you can determine what controls would be best in which situation. This is what I mean about doing research first. If you don't know what controls are available then you won't know to use them in situations that suit them. If you do the research first, you already know the possibilities to choose from.
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
1584806132980.png


So here is my draft monthly calendar using a datagridivew , I still have to add more appointment data to the appt time which is easy enough, but I'd like to color code the appointment lines?
Any idea how it might be done?
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
Having trouble adding CR/LF to text in cells I have added

DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True

But that allows to display CR/LF but also wraps the text , Id prefer not to warp the text if possible, any ideas?
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
Yes, that what I want to do, may need a bit of help tho.

My first step now is to put together say 4 strings with CR/LF at the end, with each line with a different cokour background into some type of object?
Can this be done in a textbox or listboax perhaps?

and then add that object to the cell in the datagridview using the cellpaint event!

Any help appreciated!
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
There's no need for a control unless the user is entering the text or you want them to format it. Otherwise, you just do it in code. String manipulation is simple stuff and something you can research independently.

Strings don't contain formatting though. A String is just text. If you want to include formatting metadata then you have to use a format that supports that, e.g. HTML or RTF. You'd have to parse out that metadata when actually displaying the text though, unless you were using a control understood the format, e.g. a WebBrowser or RichTextBox. If you're just drawing the text yourself on the cell in the CellPainting event handler, you need to use GDI+ to draw the different parts of the text in the appropriate colours, e.g. if you have three lines of text in three different colours then you would need to call DrawString three times. Do some research on the CellPainting event - starting with reading the documentation and including searching for examples online - and also about GDI+ in general, so you know the sorts of things you can do in the event handler.
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
Your right no control required, just a string of text nad using the graphic pen to color thetext.

I have looked at a few examples but not that clear yet. Will do some testing tomorrow.

Does every cell call cellpaint?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
Does every cell call cellpaint?
No cells call cellpaint. The CellPainting event is raised by the grid every time any cell needs to be painted. Like all cell-centric events of the DataGridView, the CellPainting event provides a means to determine which cell it currently relates to, so you can decide whether to act based on that.
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
So I first need to disable any cell content I was adding!

Then I somehow need to set the cell paintintg e.Value to the string content I wish to display.

So I need to pick up the string from with cellpainting method !

And then do it through cell paint using the Graphic pen!
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
The Value of each cell should be the data you want in that cell. How you then display that data is another matter. The data is already in the Value property. You get it from there and then draw it using GDI+.
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
So here is my test code, but text disappears instantly for some reason?
VB.NET:
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

        If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
            e.Paint(e.ClipBounds, DataGridViewPaintParts.All)  'Draw all parts of the cell normally
            Dim newRect As New Rectangle(e.CellBounds.Left, e.CellBounds.Y + 6, 12, 12)
            e.Graphics.FillRectangle(Brushes.Crimson, newRect) 'draws a red filled rectangle in each cell

            Dim RowHeight1 As Integer = 80 'DataGridView1.Rows(e.RowIndex).Height
            Dim newCell As Rectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)

            '  newCell.X += DataGridView1.Left
            ' newCell.Y += DataGridView1.Top + RowHeight1

            Dim px As Point = PointToScreen(New Point(newCell.X, newCell.Y))
            
            Using font = New Font("Century Gothic", 8.0F, FontStyle.Regular, GraphicsUnit.Pixel)
                Using Brush = New SolidBrush(Color.Black) 'Pen Type
                    ' Int(px = newCell.X + newCell.Width)
                    e.Graphics.DrawString("SUBJECT:", font, Brush, px)
                    px.Y = px.Y + 12
                    e.Graphics.DrawString("ENGLISH", font, Brush, px)

                End Using
            End Using
            e.Handled = True
        End If
        'e.Handled = True  'complete cell painting!

    End Sub
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
So what your saying is I first populate the datagridview cells with my text.

CellPainting will then pick up this text and assign it to e.Value ?

And then I can manipulate the graphics using GDI. What the difference between GDI and e.Graphics.Drawstring ?

kind regards
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
So what your saying is I first populate the datagridview cells with my text.
You populate your cells with your data, whatever that may be. It might be just the text or it might also contain some metadata regarding how the text will be displayed.
CellPainting will then pick up this text and assign it to e.Value ?
What happened when you tried it for yourself?
And then I can manipulate the graphics using GDI. What the difference between GDI and e.Graphics.Drawstring ?
GDI and GDI+ are two similar but different things. You're using GDI+ to draw the graphics. The difference between GDI+ and Graphics.DrawString is a nonsense. GDI+ is a technology for displaying graphics onscreen and Graphics.DrawString is a method for drawing text using GDI+.

Asking questions like that suggest that you haven't actually done any real research. If you want to know what GDI+ is, look it up on Bing or Google or whatever search engine you prefer. You shouldn't be asking anything here unless you've already searched for it and not been able to find anything or enough.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
You have comment out e.Handled.
The one that is commented out is outside the If block, so that shouldn't make a difference. There is one inside the If block and that should be executed if painting is actually done. I don;t have time to look into it further right now but, while the behaviour sounds like that's the issue, the code doesn't look like it is.
 

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
Unfortunately I don't have a lot of time either. Thought vb.netforums would give me the info quickly!

On form load its paints the text but then the text disappears for some reason, it looks likes it's overwritten it with the FormLoad event ?

It appears to use the CellPainting right at the beginning, thn s when I add new text to the datagrid it cellpaint again.

All very messy. Cant seem to find a guide to show how to add text to the cell with event CellPainting its very confusing! Even trying to get the text to add appears confusing this is where I am at now...


VB.NET:
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
          'e.Paint(e.ClipBounds, DataGridViewPaintParts.All)  'Draw all parts of the cell normally

        Dim cr = e.CellBounds
        e.PaintBackground(cr, True)  '//  (2)
        e.PaintContent(cr)


        If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
          
            Dim newRect As New Rectangle(e.CellBounds.Left, e.CellBounds.Y + 6, 12, 12)
            e.Graphics.FillRectangle(Brushes.Crimson, newRect) 'draws a red filled rectangle in each cell

            Dim RowHeight1 As Integer = 80 'DataGridView1.Rows(e.RowIndex).Height
            Dim newCell As Rectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)

            '  newCell.X += DataGridView1.Left
            ' newCell.Y += DataGridView1.Top + RowHeight1

            Dim px As Point = PointToScreen(New Point(newCell.X, newCell.Y))

            Using font = New Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point)
                Using Brush = New SolidBrush(Color.Blue) 'Pen Type
                   e.Graphics.DrawString(e.Value.ToString(), font, Brush, px)
         

                End Using
            End Using
        End If
        e.Handled = True  'complete cell painting!
      
    End Sub
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+
The one that is commented out is outside the If block, so that shouldn't make a difference. There is one inside the If block and that should be executed if painting is actually done. I don;t have time to look into it further right now but, while the behaviour sounds like that's the issue, the code doesn't look like it is.
Ohh, I'm reading too fast again 🤨
 
Top Bottom