Using vb.net to display a form inside a panel. error transparency - vb.net

I have a panel and button.
A form with some text-box.
Trying to display form inside my panel. code that i have written is....
Private Sub Btn_Click(sender As Object, e As EventArgs) Handles Btn.Click
Dim f As New form()
f.TopLevel = False
f.WindowState = FormWindowState.Normal
f.FormBorderStyle = FormBorderStyle.None
f.Visible = True
Pages.Controls.Add(f)
End Sub
in another page, i have written.....
Dim f As New add_customer()
f.TopLevel = False
f.WindowState = FormWindowState.Normal
f.FormBorderStyle = FormBorderStyle.None
f.Visible = True
Form1.Pages.Controls.Add(f)
Form displays inside panel. Button inside that form works well, Combo-box works well, Check-box works well, all most everything works well, but text-box isn't working.
Tried that form to open outside panel. All is fine. But inside panel it doesn't work.
All the letters and text-box become transparent.
Main form
After Customer button clicked
After add customer button clicked
When pages run solely....
Help needed.

I have found the solution.
When the TransparencyKey property is assigned a Color, the areas of the form that have the same BackColor will be displayed transparently. Any mouse actions, such as the click of the mouse, that are performed on the transparent areas of the form will be transferred to the windows below the transparent area. For example, if the client region of a form is made transparent, clicking the mouse on that area would send the event notification of the click to any window that is below it. If the color assigned to the TransparencyKey property is the same as any controls on the form, they also will be displayed transparently.
I was unaware of this property of Transparency.
I Have changed background colour and all is good now.
Thanks Visual Vincent for your fast reply.....
This link is useful.
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.transparencykey(v=vs.110).aspx

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.

Need To Close A Panel When Clicked Outside (Lost Focus)

I have an issue that I cannot seem to overcome.
In my application, I have a custom class that loads a form into a panel upon startup. Then when I click a button on my main form I show the panel as visible revealing the form to the user.
My problem is that I want to be able to hide the panel when a user clicks outside of it (back onto the main form).
so far I have tried Form_Deactivate, Form_Leave, Form_LostFocus, Panel_Leave and Panel_LostFocus events but nothing will seem to trigger an event consistently to hide the panel. The only thing that works is if the user clicks inside the form (on a listview control) once the form is visible and then clicks outside of the form.
Is there anyway I can ensure this event gets called everytime whether the user clicks the form or not?
So far my code looks something like:
Public Class cls_UserObjects
Private frm As frmUsers
Public pnl As Panel
Public Sub ShowUserPanel()
Try
frm = New frmUsers
frm.TopLevel = False
pnl.Controls.Add(frm)
frm.Show()
frm.Focus()
....
End Class
Then in my main form I call the code below to build the form into the panel:
class_Users.pnl = pnlUsers
class_Users.ShowUserPanel()
And pnlUsers.Visible = True to show it to the user
I just can't seem to close it. I understand that Panels don't support the LostFocus properly, however, I can't find away around this. Maybe it has something to do with how I am opening my form/panel but I was advised to use classes to open forms so I can have more control over the controls within my forms from outside calls.
Any help appreciated. Thanks
MouseLeave event works, the panel hides immediately once it leaves the panel.
Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
Panel1.Visible = False
End Sub

Stop items in a list view from being selected on right click

I have a ListView on a Windows Form in VB 2010.
I have set the MultiSelect property of the ListView to False so that only one item can be selected at any time.
I have configured a context menu for the ListView and it shows up correctly when the ListView is right clicked.
[Added a ContextMenuStrip control in the Designer and set the ContextMenuStrip property of the ListView to this.]
Consider these 2 scenarios:
A user right clicks on an item that is already selected in the ListView. Then the context menu is displayed and there are no issues.
A user right clicks on an item other than the item that is already selected in the ListView, Then before the context menu is displayed, the item that the user right clicked is selected.
In scenario 2, I need to stop the item that the user right clicks on from being selected automatically. Need to context menu to be displayed but the item that was previously selected should remain selected.
How can I achieve this?
I noticed that on the ListView's MouseDown event, the SelectedItems.Item(0).Index property is still at the old index. However, on the MouseUp event, this property changes to the new index.
In the MouseDown event handler, or anywhere else, how can I stop the SelectedItems from changing? Or how can I change it back to the previous selected item (without the user noticing it is being changed and then changed back)?
I can catch a right click on the MouseDown or MouseUp using the code below. However, I am not sure what I need to put inside this condition to stop the SelectedItems from changing.
If e.Button = Windows.Forms.MouseButtons.Right Then
...
End If
Note: I am able to use the following code for this. However, when I use this with scenario (2), it selects the item that the user right clicked on and then changes it back to the previous item and this change back is seen by the user. Therefore this solution cannot be used.
Dim intPrevSelectedIndex As Integer = -1
Dim boolCancel As Boolean = False
Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
If ListView1.SelectedItems.Count > 0 AndAlso e.Button = Windows.Forms.MouseButtons.Right Then
boolCancel = True
intPrevSelectedIndex = ListView1.SelectedItems(0).Index
End If
End Sub
Private Sub ListView1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
If boolCancel Then
lstWalkResults.Items(intPrevSelectedIndex).Selected = True
boolCancel = False
End If
End Sub
Please let me know any solutions you might have. Thanks for your time!
In the code behind you should be able to handle the right click event. In that method you would display the context menu manually and then ignore the click event preventing the item from being selected.
If e.Button = Windows.Forms.MouseButtons.Right Then
//display context menu because you're handling the click event manually.
...context menu code...
Dim ee As New System.Windows.Forms.MouseEventArgs(Forms.MouseButtons.None, e.Clicks, e.X, e.Y, e.Delta)
e = ee
End If

Darken a .Net Form

I have a 1080p touchscreen application. When a modal pops up, i want to emphasize that by darkening the main form.
Right now i use a second form, the size of the main form, that is black and has 50% opacity. Whenever a modal needs to appear, i open the opaque form, and then open the desired modal.
I feel this is a bit devious for my purpose. Its also not asshole-proof that when the user alt tabs, the forms will glitch out of sequence.
Is there a better way to achieve the darkening effect. Perhaps by darkening the main form from within itself?
Solved it myself by doing the following:
Place a hidden picturebox with dock:fill on the main form,
Take a screenshot of the current screen and darken it
assign the image to the picturebox and make it visible
open the modal in a new win
when the modal is dismissed
hide the picturebox
It really stupid that VB.net doesn't have this function built into it. Here's what you do to get around it:
Make a new form and call it Shade. I'm going to assume your main form is called frmMain. For the sake of clarity, lets assume the form you're launching is called dlgX.
Add the following lines in the Load event of dlgX (that's the sub with dlgX.Load or Me.Load or MyBase.Load):
Shade.Opacity = 0.001
Shade.Show()
Shade.Location = frmMain.Location ' Form location will only update if the form is visible.
Shade.Hide()
Shade.FormBorderStyle = Windows.Forms.FormBorderStyle.None 'This gets rid of the windows Titlebar and window border.
Shade.Size = frmMain.Size
Shade.BackColor = Color.Black
Shade.Opacity = 0.5
Shade.Show() ' Form size will only update the next time you show it.
Shade.TopMost = True ' Puts Shade over main form
Me.TopMost = True ' Puts current form over shade
Under all events that dismiss the form dlgX (OK.click, Cancel.click, etc), add the following lines:
Shade.Close
Or you can even make your own sub that handles all events where the form is closed:
Private Sub DispelShades(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
Shade.Close()
End Sub
This is way simpler than the PictureBox scenario and you don't have to mess with layering issues and having to ensure that the PictureBox renders on top of everything (for example, tabs really do not like having things rendered above them and they will not let you render a picture box above them). Rendering a black semi transparent form above your main form gets around all these headaches.
If you have multiple forms to shade, just make a Shad1, Shade2, Shade3 etc.
This is pretty obvious but it's worth stating: if you're shading the main form, you'll also want to make it unclickable by opening dlgX via dlgX.ShowDialog and not dlgX.Show
Here is some code, very similar to the method in Thomas's answer. Note to use the Darkness property in a Try...Finally block, to make sure you never leave the form in the dark state.
Public Class Form1
Private _PB As PictureBox
Public WriteOnly Property Darkness
Set(value)
If value Then
Dim Bmp = New Bitmap(Bounds.Size.Width, Bounds.Size.Height)
Me.DrawToBitmap(Bmp, New Rectangle(Point.Empty, Bounds.Size))
Using g = Graphics.FromImage(Bmp)
Dim Brush As New SolidBrush(Color.FromArgb(125, Color.Black))
g.FillRectangle(Brush, New Rectangle(Point.Empty, Bmp.Size))
End Using
_PB = New PictureBox
Me.Controls.Add(_PB)
_PB.Size = Bounds.Size
_PB.Location = Bounds.Location - PointToScreen(Point.Empty)
_PB.Image = Bmp
_PB.BringToFront()
Else
If _PB IsNot Nothing Then
Me.Controls.Remove(_PB)
_PB.Dispose()
End If
End If
End Set
End Property
Private Sub btnDialog_Click(sender As Object, e As EventArgs) Handles btnDialog.Click
Try
Darkness = True
MsgBox("Modal dialog")
Finally
Darkness = False
End Try
End Sub
End Class

vb.net How to get working scrollbars when moving nested form controls inside a parent panel

I have a container (a panel) which can contain multiple Form controls.
(Form.TopLevel = False)
When the user moves the Forms around I would like to display scrollbars when a form is out of the panel bounds.
When I register the Form.Move event, I can set the AutoScrollPosition. This works unless the user uses the scrollbars.
The problem is that the form.move event is also fired when the scrollbars are used. The result is that the scrollbars don't work. (And I have currently no idea how to find out whether the form has been moved by the mouse or by the scrollbar)
So the question is: How can I make the scrollbars of the panel appear/work when a form (or multiple) forms of the panel exceed the boundaries? I think there must be a simpler way than to handle the move event..
Note:
The panel is placed inside a Infragistics DockableControlPane. (Managed by an UltraDockManager)
(So there are multiple panels which contain at least one form per panel)
The reason is that the "panels" should appear as tabs, can be moved around using the DockManager and display their "sub" forms (Which also can be moved around on their panel).
Any idea would be great
It looks like the LocationChanged event could be used. Example with only one form:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
Dim f As New Form
f.TopLevel = False
AddHandler f.LocationChanged, AddressOf Form_LocationChanged
Panel1.Controls.Add(f)
f.Show()
Call Form_LocationChanged(f, EventArgs.Empty)
End Sub
Private Sub Form_LocationChanged(sender As Object, e As EventArgs)
With DirectCast(sender, Form)
Panel1.AutoScrollMinSize = New Size(.Bounds.Right, .Bounds.Bottom)
End With
End Sub
Using an MDI form seems to be more appropriate though for something like this.