Skulaurun Mrusal
Skulaurun Mrusal

Reputation: 2847

VB.NET Resize programmatically created form

I have form which is programmatically created.

Public declaration:

Public Form As Form

This code is inside sub

    Dim Form As New Form With
    {
        .BackColor = Color.FromArgb(45, 45, 58),
        .StartPosition = FormStartPosition.CenterScreen,
        .FormBorderStyle = FormBorderStyle.None,
        .Size = New Size(546, 244)
    }

When i try to resize this form at runtime by clicking at button:

Form.Size = New Size(546, 455)

Form.Refresh()

It does not work.

EDIT: I tried to rename 'Form' to 'MyForm' but nothing happened.

EDIT2: I'm creating my custom messages box. I have created module and inside this module is sub CreateMsgBox(text As String, type As MsgType, Optional ex As String = "") and in this sub is code which generating my form. After this code is few AddHandler statements with events like Button_Click() btw button is also created programmatically. I tried to change Form.Size to Form.Location to verify if button event works but location works only size not.

PS: Ahmed Abdelhameed If I replace MyForm As New Form With with MyForm = New Form With

  1. It brokes my code for moving my form.

  2. Form still not resizing.

I can not use Me because I do not have class. Any ideas how to do it? Thanks in advance.

Upvotes: 0

Views: 2098

Answers (1)

Fabulous
Fabulous

Reputation: 2423

The issue here is that you have a class level reference to the form but when you instantiate it, you are creating a local variable with the same name which shadows the class level one. I have written your code using the variable name _window for the form to avoid confusing it with anything else as follows

Private _window As Form

When creating it, I have this code...

_window = New Form With
{
    .BackColor = Color.FromArgb(45, 45, 58),
    .StartPosition = FormStartPosition.CenterScreen,
    .FormBorderStyle = FormBorderStyle.None,
    .Size = New Size(546, 244)
}
_window.Show() 

Then when resizing it, in a button click in the form that had the code to run it, I do this...

_window.Size = New Size(546, 455)

I'm not using refresh and the created form is resizing as expected.

Upvotes: 3

Related Questions