Following on from my post yesterday (how to anchor buttons/controls to various sides of the current form when the form is resized) I have run into another problem. I am setting the width of my datagrid to be 80% of the form width on a 'form resize' event with 10% of the width at either side so it remains centred in whatever size window is available. This works fine. (mydatagrid.width = me.width/5*4)

I then decided to dynamically set the width of the 2 columns in the datagrid each to 50% of the width of the datagrid. Again, this sub is called in the 'form resize' event. This fails because the datagrid hasn't loaded the data from my sql database when the first 'resize' event is triggered. I did a bit of testing (by placing a simple msgbox statement into the resize event and the load event) and it seems the 'resize' event is triggered three times before the form's load event.

So - my question is - how do I stop the resize event triggering when the form loads (or at least make the load event trigger first).

Thanks
Toomutch

Recommended Answers

All 6 Replies

Can u show ur code???

HI poojavb,

        Private Sub frmProcessWorksheets_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MsgBox("Load Event")
            ResizeScreen() 'redraws the screen
            RefreshWork() 'loads data into datagridview
            ResizeDGV() 'redraws datagridview columns
        End Sub
        Private Sub ResizeScreen()
            dgvJobList.Size = New System.Drawing.Size((Me.Width / 5) * 4, Me.Height - 130)
        End Sub
        Private Sub ResizeDGV()
            dgvJobList.Columns(0).Width = dgvJobList.width / 2
            dgvJobList.Columns(1).Width = dgvJobList.width / 2
        End Sub
            Private Sub frmProcessWorksheets_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
            ResizeScreen()
            MsgBox("Resize event")
        End Sub

Think that's all the relevant routines.

everytime the form loads it first triggers the resize event and then the load event

try to debug the code u will understand....using step over and step into

That's what I found - the resize is triggered first (see OP). The question was how do I stop it (or at least stop trying to resize the dgv columns).

Hi Toomutch,

Is there a specific reason for wanting the distance from the edge as a percentage of the form size?
If you can accept having the sides at a constant distance from the edge, you can use the Control.Anchor property to achieve this. For the case of your datagridview, you could anchor to to the left,top, and right of the form. This will cause the dgv to resize with the form.
anchor2

Edit: I seen that you also have a set distance from the bottom, so anchor to the bottom as well.

To answer your question of controling the ReSize event logic, how about something like this:

   Private DoneLoading As Boolean = False

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      'your code
      DoneLoading = True
   End Sub

   Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
      If DoneLoading Then ScreenResize()
   End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.