Order of controls being added to panel, control not showing unless docked - vb.net

I imagine this is probably an easy to answer question but for some reason I can't get it to work
Sub New(ByVal Sess As AudioSessionControl2)
S_Session = Sess
'Create the panel and position it.
S_Panel.BackColor = Color.AliceBlue
S_Panel.Width = 200
S_Panel.Height = 40
Dim Position As New Point(6, 19)
If G_AppSessions.Count > 0 Then
Position = Point.Add(G_AppSessions.Item(G_AppSessions.Count - 1).SessionPanel.Location, New Point(0, 45))
End If
S_Panel.Location = Position
'Create a label which has the name of the process
Dim S_PName As New Label
S_PName.Text = "Test"
S_PName.Dock = DockStyle.Left
S_Panel.Controls.Add(S_PName)
'Create a button to change volume
Dim S_Save As New Button()
S_Save.Text = "Save"
AddHandler S_Save.Click, AddressOf Save_Click
S_Save.Parent = S_Panel
S_Panel.Controls.Add(S_Save)
S_Volume.Parent = S_Panel
S_PName.Parent = S_Panel
MainForm.Controls.Add(S_Panel)
S_Panel.Parent = MainForm.gb_Applications
End Sub
The problem is that, the label will show because its docked, but the button won't. It will only show if its docked as well, and thats just not what I want. This is part of a class for creating a dynamic UI, where I can create a number of this class to create a bunch of panels for various things.

I don't see anywhere where you are setting the label or button position. You probably have them both at 0,0 and the label is on top of the button, obscuring it. Did you try setting the position of both the controls, making sure they don't overlap?

Related

Rows Added to a Datagridview not being captured in Screen Capture

I am running into an issue where I am attempting to add some blank rows to a datagridview before printing the form for use.
I am having an issue trying to understand where I need to put the code to add the rows to the data gridview so they are added and captured before the user hits print. My concern is that I am not adding the rows in the right spot before the screen is captured however the code to add the rows is run before the code to hide the buttons is implemented however the buttons are removed and the rows are not added in the actual printed image.
If that is not clear hopefully these images will make more sense. At this point I am not sure why the buttons being hidden are being captured but the added rows are not when based on the code it looks like the rows are being added before the buttons are hidden.
Thank you.
Here is the Image before the user hits print
Here is the Image after the user hits Print
And here is what actually printed
And here is the code in question that captures the screen
Private Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
If dgvReceive.Rows.Count < 27 Then
Dim rowstoadd As Integer = 0
rowstoadd = 27 - dgvReceive.Rows.Count
dgvReceive.Rows.Add(rowstoadd)
End If
If MemoryImage IsNot Nothing Then
MemoryImage.Dispose()
End If
MemoryImage = New Bitmap(s.Width, s.Height, myGraphics)
For Each btn As Button In Me.Controls.OfType(Of Button)
btn.Visible = False
Next
dgvReceive.ClearSelection()
lblTitle.Select()
Me.FormBorderStyle = FormBorderStyle.None
Dim memoryGraphics As Graphics = Graphics.FromImage(MemoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
Me.FormBorderStyle = FormBorderStyle.Sizable
For Each btn As Button In Me.Controls.OfType(Of Button)
btn.Visible = True
Next
End Sub

Inserting a panel between 2 controls in Windows form

I want to insert a Panel (User Control) dynamically between two other control. Let say I have a PictureBox(Control) as the parent are there is an other control on the pictureBox.
I want to remove the other control from the picture box, add a panel(control) to the picture box and then set the older control to the new panel. For now I have this code :
If m_targetDisplay.MainDoubleBufferedPictureBox.HasChildren Then
For Each child As Control In m_targetDisplay.MainDoubleBufferedPictureBox.Controls
If (child.BackColor = Drawing.Color.Transparent) Then
Dim panel As SXFadeWrapperForGPU = New SXFadeWrapperForGPU()
panel.ClientSize = New Size(child.ClientSize.Width, child.ClientSize.Height)
panel.Location = New System.Drawing.Point(child.Location.X, child.Location.Y)
m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Add(panel)
panel.Controls.Add(child)
m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Remove(child)
AddHandler panel.Paint, AddressOf PanelPaintEvent
End If
Next
End If
My code is adding a background wrapper to the transparent color child in front of it. The thing is even if I remove the child before adding or after adding it back, I can never see it on the screen. Is there any particular thing that maybe the Remove does so the removed Child isn't usable again ?
You should change the child location to (0, 0), else it will still be relative to its position inside MainDoubleBufferedPictureBox, but now it's in the new panel. It could be there, just located outside the panel's boundary
If m_targetDisplay.MainDoubleBufferedPictureBox.HasChildren Then
For Each child As Control In m_targetDisplay.MainDoubleBufferedPictureBox.Controls
If (child.BackColor = Drawing.Color.Transparent) Then
Dim panel As SXFadeWrapperForGPU = New SXFadeWrapperForGPU()
panel.ClientSize = New Size(child.ClientSize.Width, child.ClientSize.Height)
panel.Location = New System.Drawing.Point(child.Location.X, child.Location.Y)
m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Add(panel)
panel.Controls.Add(child)
child.Location = New System.Drawing.Point(0, 0) ' <--- this
m_targetDisplay.MainDoubleBufferedPictureBox.Controls.Remove(child)
AddHandler panel.Paint, AddressOf PanelPaintEvent
End If
Next
End If

How to terminate the last action when a new label is clicked?

I have many labels. When a label is clicked, I change the BackColor to aqua. When I click on another label, both of them are aqua, but I want the color of the first label to go back to normal. It there a way to do that?
Here is my code:
Dim clickedLabel = TryCast(sender, Label)
If clickedLabel IsNot Nothing Then
clickedLabel.BackColor = Color.Aqua
TextBox1.Text = clickedLabel.Text
Else
End If
Put them all in a collection so that you can apply the default-color on all others or - if they are all in the same container-control like a Panel - use this code:
For Each lbl In LabelPanel.Controls.OfType(Of Label)()
clickedLabel.BackColor = If(lbl Is clickedLabel, Color.Aqua, DefaultColor)
Next
TextBox1.Text = clickedLabel.Text
Instead of LabelPanel.Controls you could also use Me.Controls, but then all labels on the form are used even if it's not related. Labels that are in other container controls won't be find in this way anyway, so no recursive search.
DefaultColor is a System.Drawing.Color that you store as class/member variable(shared or as instance-variable).

VB.Net 2008 Buttons acts as Tab Controls

I am looking for a way to design things differently in my project. Instead of using TabControls I wish to use Buttons (Instead of pressing the tabs on the top I would like to press the Buttons on the left-side). These buttons when pressed they have their own Panel where each has their own respective content.
Select Case tabAdmin.SelectedIndex
Case 0
If txtCode_Patient.Text = "" Then
txtCode_Patient.Focus()
Else
cmdAdminister.Focus()
End If
Case 1
If txtD_Patient.Text = "" Then
txtD_Patient.Focus()
Else
cmdRefresh.Focus()
End If
Case 2
If txtI_Patient.Text = "" Then
txtI_Patient.Focus()
Else
cmdI_CUser.Focus()
End If
Case 3
If txtStat_CS.Text = "" Then
txtStat_CS.Focus()
Else
cmdStat_Refresh.Focus()
End If
End Select
The code above is similar to what my project acts and it works with TabControls. I want to do a similar thing but this time, like I said before, pressing Buttons on the left-side. How can I do a similar thing ?
UPDATE:
I found a way for this one but now my concern is how do I make it look like one of its default button 3D look-alike?
Public Class Tab
Inherits TabControl
Private Property DoubledBuffered As Boolean
Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
DoubledBuffered = True
SizeMode = TabSizeMode.Fixed
ItemSize = New Size(30, 110)
End Sub
Protected Overrides Sub CreateHandle()
MyBase.CreateHandle()
Alignment = TabAlignment.Left
End Sub
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
Dim B As New Bitmap(Width, Height)
Dim G As Graphics = Graphics.FromImage(B)
G.Clear(Color.AliceBlue)
For i = 0 To TabCount - 1
Dim TabRectangle As Rectangle = GetTabRect(i)
If i = SelectedIndex Then
'//Selected
G.FillRectangle(Brushes.DarkSlateGray, TabRectangle)
Else
'//Not Selected
G.FillRectangle(Brushes.AntiqueWhite, TabRectangle)
End If
G.DrawString(TabPages(i).Text, Font, Brushes.Black, TabRectangle, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
Next
e.Graphics.DrawImage(B.Clone, 0, 0)
G.Dispose() : B.Dispose()
MyBase.OnPaint(e)
End Sub
End Class
Use the CheckBox control instead of Button, but set Appearance = Button, that way it looks exactly like a button can remains in the "pressed" state when clicked.
To shift between content, put each of your sub-forms into their own UserControl instances, then host them within a Panel control, then switch the .Visibility property of each sub-form according to which CheckBox was clicked.
There is an Outlook-style side bar available on Code Project. It has a VB version as well as C# and although it's knocking on a bit now, you could always adapt this to look a bit nicer. I have used it in the past and it worked pretty well as I recall.
You can set the selected tab via tabAdmin.SelectedIndex = 0 (or 1, 2, etc, but remember it is 0 based)
You may also set the tab by the tab's name via tabAdmin.SelectedTab = TabName
Use a common click event handler for the buttons. Store the relevant tabindex in the Tag property of the buttons. Then tabAdmin.SelectedIndex equals the tag of the clicked button cast as integer.

Add tooltip control dynamically

I have a child form that is completely created in code.
I would like to add tool tips to the textbox controls on the form. I know how to set up the tool tips on the fly but can't find a way to add the tooltip control to the form on the fly. All the hits I find on google refer to dragging the control from the designers toolbox.
I would need to do something like:
' Add tool tip control
Dim toolTip1 As New ToolTip()
toolTip1.ShowAlways = True
frm.Controls.Add(toolTip1)
This does not work
I tried adding a sub to handle the from.load event(with a handler that poitn to the sub) at design time but can not get passed the error "Tooltip1 is not declared" when adding the tooltip to the iundividual dynamic controls.
If I call this dynamic form from a parent form and add the tooltip control to the parent form I can use it for the child form. But how would I do this if I was creating the form from a routine that does not live on a parent form?
thanks
Dim frm As New Form
' Add tool tip control
''Dim toolTip1 As New ToolTip()
''toolTip1.ShowAlways = True
'Draw the Form object
'close the dynamic frm if existing already
If frm IsNot Nothing Then
frm.Close()
End If
frm = New Form()
frm.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
frm.Name = "frm_test"
'dimension is irrelevant at the moment
frm.ClientSize = New System.Drawing.Size(10, 10)
'the parent will be the current form
'frm.MdiParent = this;
'splash screen mode form, why not...
frm.ControlBox = True
frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
frm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
frm.BackColor = System.Drawing.Color.LightGray
For Each item As MYFILE.Class in the Collection
Dim aTextBox As New TextBox()
aTextBox.Font = New System.Drawing.Font(sFont, Single.Parse(sSizeFont), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CByte(0))
aTextBox.BackColor = System.Drawing.Color.Yellow
aTextBox.Location = New System.Drawing.Point(iNewColumnPosition + 5 + intMaxWidthLabel, intVertPos)
aTextBox.Size = New System.Drawing.Size(intWidthTextBox + 10, intGapHeight)
'store the biggest width, so that the textboxes can be vertically aligned
If intWidthTextBox > intMaxWidthText Then
intMaxWidthText = intWidthTextBox
End If
'giving a name to all your object will be the only way
'to retrieve them and use them
'for the purpose of this sample, the name can be the
'same for all textboxes.
aTextBox.Name = item.ParameterName
'giving the maximun size in caracters for the textbox.
aTextBox.MaxLength = Integer.Parse(item.ParameterLength)
toolTip1.SetToolTip(aTextBox, "TIP" & intIndex.ToString)
'tab have to be ordered
aTextBox.TabIndex = intIndex
intIndex += 1
'Vertical position is to be manage according the
'tallest object in the form, in this case the
'textbox it self
intVertPos += intGapHeight
'adding the textbox to the form
frm.SuspendLayout()
aTextBox.SuspendLayout()
frm.Controls.Add(aTextBox)
Next
I left a lot of code out but this should give you an idea as to what I am doing
In vb it is
Dim tooltip As New ToolTip(components)
tooltip.SetToolTip(textBox1, "This is a textbox tooltip")
Sorry this is not in VB.NET but I am pretty sure you can easily convert this. The first line is to set ToolTip control to your form components. The second is how you set a tooltip to a control and give it the related text.
ToolTip tooltip = new ToolTip(components);
tooltip.SetToolTip(textBox1, "This is a textbox tooltip");