Inherited panel is making controls disappear randomly - vb.net

I have a class written that inherits a panel, and enables double buffering to it:
Public Class dblBufferPanel
Inherits Panel
Public Sub New()
'inherit a panel and add double buff to reduce flicker
'this will be used wherever a panel with gradient or bg img is needed
Me.DoubleBuffered = True
Me.ResizeRedraw = True
End Sub
End Class
The problem that I have been facing is that randomly while coding, the controls set within the various panels seem to "vanish" off the panel. Basically, the panel looks "empty" and there is no way to select the control from form design. I can however select them in the properties panel, but altering properties of these controls does nothing. The only solution I have found is that I close VS2010 and restart. Then, if I did not compile after noticing the controls "vanishing", they reappear upon opening the solution.
This seems to happen with any control, comboboxes, labels, listview, etc. What would be causing this?

Related

Set backgroundcolor of every form in vb.net

How can I set the BackColor of every form that exists in my vb.net (fw 4.7.2) project?
And yes, I could do
Form1.BackColor = ...
Form2.BackColor = ...
......
but that would be too messy then.
Open one of your forms in the designer, any form, doesn't matter
Click the form background
In the property grid, at the top, go to (Application Settings) then click the dots button in (Property Binding), find BackColor in the list of proeprties, click the dropdown next to it and click New at the bottom
Call your setting ThemeColor, pick the color you want for the light theme, such as White
OK everything
Open the designer for another form, go Application Settings/Property Binding again. This time don't click New, just bind BackColor to the existing ThemeColor setting
Repeat for all forms in your app
Add a button to the "Settings" form (or wherever you want to offer a toggle for the theme), and in its click handler write:
My.Settings.ThemeColor = Color.White
Add another button to the form, make its click handler set Color.Black
Run the application. Open one or more forms, including the settings form. Click one of the buttons to change the setting. All forms change color
You could:
For Each f as Form in Application.OpenForms
f.BackColor = Color.Red
Next f
at any point after you've opened a boatload of forms
.. or you'd just set the back color in the designer if you were designing a form and you wanted every form instance of that form that you created be red as soon as it was shown
I would have said you could create a class:
Public Class ColoredForm
Inherits Form
Public Sub New()
Me.BackColor = Color.Red
End Sub
End Class
And then in every form you add, open the designer and change the code so that it Inherits this form instead; but it's messing with the designer files (not everyone wants to do it, and changes could be lost if they were regenerated) and isn't actually any different to just.. setting the back color in the designer property grid. If you have some more involved behavior you want all your forms to adopt then consider an inheritance, but if it's as trivial as setting the BackColor, just do it in each form as a design time thing (If you have a lot of forms, can use find/replace for it)

Adding a form to TabPage inside a TabControl [duplicate]

I have a customer which as a Visual Basic Project in single instance mode with a wired presentation logic.
The main form contains a TabControl with mutliple TabPages.
If I click on TabPageA another form is shown in front of the Form and resized to have the same size as the TabPage.
If I click on TabPageB the first form is hidden and another form is displayed.
So basically for the user it looks like you have a TabControl with different TabPages which is not the case.
I tried converting the Forms to UserControls and put them inside the TabPage, but, thanks to the SingleInstance app, this would take a whole lot of refactoring. I tried it but eventually gave up because of many many runtime errors and I don't want to put any more effort in this.
My Ideam was that, at runtime, I could add the forms to the TabPages and let them act like UserControls, is this even possible?
You can turn a Form class back to a child control by setting its TopLevel property to False. It becomes essentially a UserControl with some unused overhead. Make it look similar to this:
Public Class Form1
Public Sub New()
InitializeComponent()
Dim frm As New Form2
frm.TopLevel = False
frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
frm.Visible = True
frm.Dock = DockStyle.Fill
TabPage1.Controls.Add(frm)
End Sub
End Class
Any window can be hosted in any other window (a Control is a window, technically) using SetParent.
<System.Runtime.InteropServices.DllImport("user32.dll")>
Public Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
End Function
to declare and
SetParent(FormToHost.Handle, ControlToHostForm.Handle)
to use. It may not be ideal but it's okay if you don't want to put any more effort into this, like you say. The forms will maximize and minimize properly, and will not show up in the taskbar, and will close with their containers.
you can use panels. in each tab different panel or panels must be show an the other or others must be hide.

how to apply same style to windows from in vb.net at design time?

I have a created windows application in VB.net. now I want apply style to it. Like I want apply background color, button style, font type etc from signal setting. And didn't want go and style style to Individual controls.
There are several solutions:
You can store all form settings (the settings that should change for each theme) in My.Settings and then apply these settings to each form. Here's a tutorial that might help you.
Example (in the Load event handler):
' ...
Me.BackColor = My.Settings.ThemeBackColor
' ...
If you have multiple themes that the user can choose from, then store them in separate settings files and read them into the program.
In order to apply settings to multiple controls, loop through all of them and apply the settings. See https://stackoverflow.com/a/4674181/2671135 for more information on how to get all controls of a certain type.
You could also create a Module with a Public Sub similar to this:
Public Module Theme
Public Sub ApplyTheme(ByRef form As System.Windows.Forms.Form)
With form
.BackColor = Color.Black
.Color = Color.Green
' ...
End With
End Sub
End Module
Inside each form's Load event handler, simply call this method:
ApplyTheme(Me)
Again, see https://stackoverflow.com/a/4674181/2671135 for more information on how to loop through form controls.
Another option would be to create a class that Inherits System.Windows.Forms.Form. In the constructor method, set all settings as appropriate. Then, for each form, inherit from this class.
I faced the same problem a while ago and I created a DLL that automates the second solution. Check it out on GitHub, especially the ConfigureWindow method in this file. It is written in C#.NET though, but I hope it helps anyway...

not able to set readonly property of textboxes in a panel in windows form to true in vb.net

i want to enable the read only property of all the text boxes in a panel in windows forms to true during form load but its not working. i am using the below code.when i am debugging the code it skips that part.not sure why??
The below code does not work,it skips that part as if there are no text box controls in the particular panel.
Private Sub lockgroupcontrols()
For Each TextBox As TextBox In Pnltransaction.Controls.OfType(Of TextBox)()
TextBox.ReadOnly = True
Next
End Sub
Your code seems correct, so, I can only imagine that, if it skips the For Each, then you don't have any textboxes inside the PnlTransaction panel.
Sometimes this happens when you draw the Panel over preexisting textboxes. You think the controls are inside the panel, but in reality they are under the panel and you see them because the panel background is transparent.
Try to move the panel on a different place, drag&drop the textboxes over the panel and then reposition the panel.
You shouldn't use TextBox as name for your vars
For Each **TextBox** As **TextBox** In
try it with for ex.
...For Each **tBox** As **TextBox** In
tBox.ReadOnly = True....

VB.NET: How can you activate the childform when only the control inside is clicked?

*edit: OK, so this is my real problem, below scenario happens only when the form is MDIChild.. thanks for anyone that could provide me with the code
I have a form with labels, panels, buttons etc. Where I'm having problem is, while form2 is my active window/form and I clicked on a control inside form1, the form1 does not activate itself. What I would like to happen is for form1 to activate even when it's not the form I clicked, only the control inside it (any control)..
I'm thinking that if I clicked a control on the form, there's an event fired on the form. If I could only know of that certain event, that would help - maybe (coz I could just add Me.activate on that event if it exists). I've tried searching for series of events when a control (ex. label) is clicked but to no avail. I hope that someone could help me with this one.
Thanks in advance.
*edit
i will just try to make my question more understandable..
How can I activate the form when only the control is clicked (say, label or textbox)? My forms does not activate or focused when I click inside it except the form itself..
I can do this on one control..
Private Sub Label1_Click - Handles Label1.Click
Me.Activate()
End Sub
But what if I have 20 controls (labels, buttons, textbox, combobox, etc)? See? =)
EDIT: this answer does not apply to MDI applications.
I think what you really want to know is which one of your forms is currently the foreground window (if any). The first thing you need to understand is that a form instance lives inside a window, but the window's behavior is controlled somewhere higher up. Similar to how a form instance is identified by a variable pointing to the instance, a window can be identified by what's known as a window handle.
Knowing this, the proper way to find out whether a form is the "active" form is to:
find out the window handles of the windows containing our instances of Form1 and Form2
find out the window handle of the foreground window (which can be any window)
compare the value found in step 2 to all of the values found in step 1
Perhaps you'd then like to fire an event if the foreground window changes, but I'll leave the actual implementation up to you. There are probably several ways to perform step 1 and 2, but I can't give any solutions off the top of my head. Hopefuly I've put you back on the right track.
EDIT
Alternatively, you can use the form's Containsfocus property. If its value is True, you can safely assume that your form is the foreground window. I didn't find out about this property until after I wrote my own implementation, which I'll show you anyway:
One module containing only a windows API call
Friend Module NativeMethods
Friend Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
End Module
Calling this method will return the window handle of the foreground window (if any).
One module containing the extension method for the Form class
Imports System.Runtime.CompilerServices
Public Module FormExtensions
<Extension>
Public Function IsForeground(f As Form) As Boolean
Return (f.Handle = NativeMethods.GetForegroundWindow)
End Function
End Module
Calling this method returns whether the specified form f has the same window handle as the foreground window.
Usage example
You could use a Timer that periodically checks whether a form is the foreground window.
Public Class Form1
Private WithEvents timer As New Timer With {.Enabled = True}
Private Sub timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
If Me.IsForeground() Then
Console.WriteLine("this instance of Form1 is the foreground window")
End If
End Sub
End Class
Like I said before, you can use Me.ContainsFocus instead of my extension method and it will work just fine.
In non-MDI forms, the form is automatically activated when you click any control inside it.