Changing fonts at runtime and resizing controls - vb.net

I have an application where I am dynamically changing form font types and sizes at runtime. The forms and controls resize proportionally when the controls are added to the form itself but not when they are added to containers such as panel or group controls where they tend to overlap or not grow to accommodate larger text. Any pointers as to why this might be?

For changing the font and size you use this:
Me.Font = New Font("Verdana", 12)
Me.Size = New Size(20, 20)

I have discovered that this was an issue specific to the DevExpress XtraForm. I resolved it by adding this to the form's constructor:
Font = (New Form()).Font
So my form's constructor looks like this:
Public Sub New()
Font = (New Form()).Font
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub

Related

VB.NET - Put a form in a panel

I wanna to put all controls of a determinated form in a panel. No problem so far, however the elements added to the panel are resized. Suppose that the panel is 800x600 in size and the module containing the controls that I want to add to the panel is the same size, i.e. 800x600. The controls are automatically resized despite the fact that autosize is disabled both on the panel and in the module itself. I wrote a function that switch controls of a form to another.
Sub SwitchPanel(ByVal FormToShow As Form)
FormToShow.FormBorderStyle = Windows.Forms.FormBorderStyle.None
FormToShow.Location = New System.Drawing.Point(0, 0)
FormToShow.WindowState = FormWindowState.Normal
Home.GunaPanel.Controls.Clear()
FormToShow.TopLevel = False
Home.GunaPanel.Controls.Add(FormToShow)
FormToShow.Show()
Home.GunaPanel.Show()
FormToShow.Dock = DockStyle.Fill
FormToShow.AutoSize = False
End Sub
Thanks! Sorry for my bad english.

Changing usercontrol background colour doesn't show on form

I'm just porting a load of FoxPro code to VB.NET. I have a csslbuttonpanel user control with a background colour of skyblue. This control is dropped onto various winforms. If I change the colour in csslbuttonpanel to red the forms still show skyblue.
Am I missing something?
Regards
Graham
Public Class csslButtonPanel
Inherits Panel
Public Sub New()
' This call is required by the designer.
InitializeComponent()
BackColor = Color.Red
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
This happened because you made a mistake in the first version of the UserControl. You forgot to tell the designer what the default value is. You can easily see this back in the Properties window. Select one of the existing ones on the form and note how the BackColor property is displayed. It is shown in bold type, indicating that its value is not the default.
And since it is wasn't the default, it recorded Color.SkyBlue in the form's InitializeComponent() method. Changing the default in your UserControl now has no effect, it keeps using the value selected in the form's InitializeComponent() method.
What you should have done is tell the designer about the default with an attribute. Like this:
<DefaultValue(GetType(Color), "255, 135, 206, 235")> _
Public Overrides Property BackColor As Color
Get
Return MyBase.BackColor
End Get
Set(value As Color)
MyBase.BackColor = value
End Set
End Property
The <DefaultValue> attribute informs the designer about the default, SkyBlue in this case. Now the value doesn't get recorded in the form and changing your default in the user control can be effective.
Easy mistake, everybody makes it at least once. The attribute format is pretty awkward for Color, that doesn't help. Easy to fix however, simply copy/paste the snippet and change the string to "255, 255, 0, 0" to match Color.Red. Rebuild. Go back to the form and right-click the BackColor property on the existing controls and select Reset. They'll now turn Red.

Control disappearing in designer

Consider an empty WinForms application created using VS2010.
It has a custom TextBox class with the following code:
Public Class DummyTextBox : Inherits TextBox
Private Const FONT_SIZE As Single = 14.25!
Private Const FONT_FAMILY As String = "Microsoft Sans Serif"
Private Sub Me_ParentChanged(sender As Object,
e As System.EventArgs) Handles Me.ParentChanged
'this one does not work, it causes designer
'to lose its controls once in a while
Me.Font = New Font(Me.Parent.Font.FontFamily, FONT_SIZE)
'if I use a constant value instead, like below, it works fine
'Me.Font = New Font(FONT_FAMILY, FONT_SIZE)
End Sub
End Class
So basically a TextBox with increased font size, same family as the parent form.
What happens is that after being put on the form, and then built, the control sometimes disappears from designer view. If you run the project, it's usually showing fine. Close/reopen a form and it's there again.
Sometimes, however, the control will disappear completely (I was not able to reproduce this 100% of the time), so you'd have to add it again and set the properties. If multiple controls are placed in one shot, usually only one of them disappears like that. Controls are more likely to disappear after being moved around on the form.
What's going on?
According to my research, Me.Parent can sometimes be Nothing inside ParentChanged, so that line throws an exception, which is never displayed to the user (it only happens at design time). Putting a Try/Catch around it helps verify this fact. It looks like Windows Form Designer likes detaching controls and attaching them back at its own discretion.
To solve the problem, need to verify there exists a parent, and only then set the Font.
So changing this:
Me.Font = New Font(Me.Parent.Font.FontFamily, FONT_SIZE)
To this:
Dim parent As Control = Me.Parent
If parent Is Nothing Then Return
Me.Font = New Font(parent.Font.FontFamily, FONT_SIZE)
Makes the issue go away and does not affect runtime in any way.

Background Image on Panel flicker when scrolling

I've got a panel in windows forms (Visual Studio 2008) which has a background image (A book shelf).
When scrolling the image flickers and does not redraw so looks awfully - I've tried creating a new object to use double buffering but this has no effect, any suggestions?
Public Class DoubleBufferPanel
Inherits Panel
Public Sub New()
Me.DoubleBuffered = True
SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.DoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
UpdateStyles()
End Sub
End Class
That seems to happen when the BackgroundImageLayout is set to Zoom. If you set it to Stretch, that might fix the problem. If necessary, you can resize the image to fit the panel at load time and whenever the panel size changes.
I have used a docked picturebox, rather than labels I added the text directly to the image.

How can I set a form to have a transparent background

I am struggling to get my form to have a transparent background in vb.net
Currently in the form New I set
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true)
But still the form shows up as having the default grey background
Can anyone help??
EDIT: I need the controls on the form to be visible so I don't think setting the opacity to 0 will work
EDIT: I tried the transparency key solution but it doesn't work. I have a circular image with a black background. OnPaint I set the transparency key to the img pixel at 0,0, this then leaves me with circular image (which I want ) It hides the black background but I am still left with the default grey rectangle of the form.
below is the code I have -
Public Sub New()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Transparent
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Timer1.Start()
End Sub
Private Sub frmWoll_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim img As Bitmap = CType(Me.BackgroundImage, Bitmap)
img.MakeTransparent(img.GetPixel(2, 2))
Me.TransparencyKey = img.GetPixel(2, 2)
End Sub
Use TransparencyKey for transparent form.
eg.
TransparencyKey = Color.Red
Button1.BackColor = Color.Red
Now run the form you will find that the button1 has a hole in it.
So using this method you can create a mask image in paint for which part has to be transparent and apply that image to form and voila the form is now transparent.
Edit:
Sorry for late reply.
Following is your code modified to suit your requirement
Public Sub New()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Transparent
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim img As Bitmap = CType(Me.BackgroundImage, Bitmap)
'img.MakeTransparent(img.GetPixel(2, 2))
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.TransparencyKey = img.GetPixel(2, 2)
End Sub
Set Form's TransparencyKey color property same as form's Background color property
There are a few methods you could use.
Use the forms TransparencyKey
Override OnPaintBackground (WM_ERASEBKGND)
Override WndProc and handle the paint messages (WM_NCPAINT, WM_PAINT, etc)
I recommend overriding the window procedure to get optimal results.
Me.Opacity = 0
Be warned that:
This is for the entire form, rather than just the background. There are work-arounds to make certain parts more opague.
It's only psuedo-transparency where it takes a snapshot of what's behind it. It's smart enough to know when you move the form, but not when you move other transparent objects on top of the form.