When creating a label a label inside a panel, the text is cut off VisualBasic - vb.net

I wanted to be able to dynamically create a panel with a label on it, but the label isn't acting as I would expect it too, its cutting most of it off.
When I create a panel then create a label inside the panel, the text isn't displayed correctly. Anyone know how to fix it?
What is was supposed to do was to create a panel with text on it with the newpanel() sub
Dim timetable(5, 5) As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Width = (74 * 5) - 3
Me.Height = My.Computer.Screen.Bounds.Size.Height
Me.Top = My.Computer.Screen.Bounds.Top
Me.Left = My.Computer.Screen.Bounds.Right - Me.Width
GetTimetable()
End Sub
Private Sub newpanel(colour As Color, textT As String)
Dim Npan As New Panel
Npan.Top = 0
Npan.Left = 0
Npan.Width = Me.Width
Npan.Height = 64
Npan.BackColor = colour
Dim NpanT As New Label
NpanT.Parent = Npan
NpanT.Text = textT
Npan.Controls.Add(NpanT)
Me.Controls.Add(Npan)
End Sub
Private Sub GetTimetable()
'Dim path As String = My.Computer.FileSystem.SpecialDirectories.Desktop + "\Timetable"
newpanel(Color.Aqua, "this is a test! test testtesttest test test test")
End Sub

Looking at the MSDN page for the Label.AutoSize Property, it states as was mentioned above that the Labels AutoSize Property defaults to true in the designer, but it is false when created in code.
From above link:
When added to a form using the designer, the default value is true. When instantiated from code, the default value is false.
So you need to change your newpanel method to this:
Private Sub newpanel(colour As Color, textT As String)
Dim Npan As New Panel
Npan.Top = 0
Npan.Left = 0
Npan.Width = Me.Width
Npan.Height = 64
Npan.BackColor = colour
Dim NpanT As New Label
NpanT.Parent = Npan
NpanT.Text = textT
NpanT.AutoSize = True 'Enables Auto sizing
Npan.Controls.Add(NpanT)
Me.Controls.Add(Npan)
End Sub

Related

Is it possible to group multiple PictureBoxes?

I can drag a PictureBox onto a Form Control, a Tab Control or a Panel Control, etc. And I can import an image into a PictureBox. However, I don't know how to group multiple PictureBoxes together in Visual Studio 2017. Looks like there is no such a function. I need this function because I want to generate a big picture based on the user's input. That big picture consists of multiple small pictures, the visibility of which is controlled by the user through multiple checkboxes.
In Excel, I could put multiple pictures in it, group them together, use the VBA to control the visibility of each picture, and finally copy that picture group into a Word file. I would do this in a VSTO Word Document project in Visual Studio 2017 using vb.net.
I added some pictures for demonstrate the expected function.
Picture 1 shows the small pictures to be used in a big picture. (Please ignore the .vslx file)
Picture 2 shows a possible result based on user's input.
You can make your own custom control. here is an example/suggestion how to do it with a User control that can be reused across your application. the user control is holding panels in a matrix, you can set a drag&drop Event to each Panel control and the user will be able to drop a picture box on each panel:
USER CONTROL:
Public Class UserControl1
Public NumberOfPanelsInRow As Integer
Sub New(ByVal height As Integer, width As Integer, Optional ByVal numberofPanelsInRow As Integer = 3)
' This call is required by the designer.'
InitializeComponent()
' Add any initialization after the InitializeComponent() call.'
Me.Height = height
Me.Width = width
Me.NumberOfPanelsInRow = numberofPanelsInRow
End Sub
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' grouped panels to hold picturebox you can drag & drop to them...'
Dim panelHeight As Integer = Me.Height / NumberOfPanelsInRow
Dim panelWidth As Integer = Me.Width / NumberOfPanelsInRow
Dim colors() As Color = {Color.Pink, Color.Black, Color.Red, Color.Cyan, Color.Green, Color.Orange,
Color.Red, Color.Pink, Color.Black, Color.Red, Color.Cyan, Color.Green, Color.Orange, Color.Red}
Dim total As Integer = NumberOfPanelsInRow * NumberOfPanelsInRow
Dim currentYlocation As Integer = 0
Dim currentXlocation As Integer = 0
Dim location As Point = New Point(0, currentYlocation)
Dim rowcounter As Integer = 0
Dim itemcounter As Integer = 0
For i = 1 To total
If rowcounter >= NumberOfPanelsInRow Then
rowcounter = 0
currentYlocation += panelHeight
currentXlocation = 0
End If
' to each one of this panel you can drag a picture box'
Dim p As New Panel
p.Size = New Size(panelWidth, panelHeight)
p.Location = New Point(currentXlocation, currentYlocation)
p.BackColor = colors(itemcounter)
Me.Controls.Add(p)
rowcounter += 1
itemcounter += 1
currentXlocation += panelWidth
Next
End Sub
End Class
CALLING THE USER CONTROL FROM FORM1:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim uc = New UserControl1(300, 300)
Me.Controls.Add(uc)
End Sub
End Class
GUI OUTPUT:

moving many controls together to represent a Seating Chart in VB.NET

Greetings,
I want To create a graphic representation of a seating chart (could be in a wedding venue or in cars...). I used a splitted panel, in the splittedPanel1 i put the buttons to create the cars, and inside the splitted panel2 i want to put the graphic representation. Inside SplittedPanel2, I've created a panel and a pictureBox (to represent some fixed areas in the real world).
I've also created a class called SimpleCar. SimpleCar is composed of 5 TextBox (es) and a PictureBox all in a panel to represent a car: the textBoses represent the passengers names and the car label, and the pictureBox to put an image representing a car (or a table). I've also made a sub to Add dynamically a SimpleCar.
2 problems occur when i want to move this new panel (dynamically created), using MouseDown and MouseUp events:
- first pb: while moving the existing panel, the screen flashes and the movement is not smooth
- second pb: i can't move a panel dynamically created
Note that moving a PictureBox by this code is very smooth but moving a panel is not user friendly.
I expect moving a dynamically created a panel smoothly, or should I reconsider displaying the cars in another way than in a panel?
Knowing that the final purpose of the code is to export a picture of all the created tables in the venue. I also tested the code with a groupBox and the results aren't good.
The simpleCar class is described in the code below:
Class SimpleCar
Public carNameBox, passengerNameBox1, passengerNameBox2,
passengerNameBox3, passengerNameBox4 As TextBox
Public carPictureBox As PictureBox
Public carGroup As Panel
Public Sub New()
carGroup = New Panel
carNameBox = New TextBox With {.Text = "carNmBx",
.BackColor = Color.Yellow,
.Name = "carNmBx"}
passengerNameBox1 = New TextBox With {.Text = "txtPassNmBx1",
.BackColor = Color.BlanchedAlmond,
.Name = "TextBox1"}
passengerNameBox2 = New TextBox With {.Text = "txtPassNmBx2",
.BackColor = Color.AliceBlue,
.Name = "TextBox2"}
passengerNameBox3 = New TextBox With {.Text = "txtPassNmBx3",
.BackColor = Color.Azure,
.Name = "TextBox3"}
passengerNameBox4 = New TextBox With {.Text = "txtPassNmBx4",
.BackColor = Color.Cyan,
.Name = "TextBox4"}
carPictureBox = New PictureBox With {.Text = "picBx1",
.BackColor = Color.BlanchedAlmond,
.Name = "picBox1"}
Dim fdialog As New OpenFileDialog()
fdialog.FileName = String.Empty
fdialog.Multiselect = True
If fdialog.ShowDialog = DialogResult.OK Then
If fdialog.FileNames.Length = 2 Then
carPictureBox.Image = Image.FromFile(fdialog.FileNames(0))
ElseIf fdialog.FileNames.Length = 1 Then
carPictureBox.Image = Image.FromFile(fdialog.FileName)
End If
End If
carGroup.Controls.Add(carPictureBox)
carGroup.Controls.Add(carNameBox)
carGroup.Controls.Add(passengerNameBox1)
carGroup.Controls.Add(passengerNameBox2)
carGroup.Controls.Add(passengerNameBox3)
carGroup.Controls.Add(passengerNameBox4)
End Sub
End Class
To Add dynamically a SimpleCar in the code below:
Public Sub Add_car()
Dim carType As SimpleCar
carType = New SimpleCar
Dim carPs1 = carType.passengerNameBox1
Dim carPs2 = carType.passengerNameBox2
Dim carPs3 = carType.passengerNameBox3
Dim carPs4 = carType.passengerNameBox4
Dim carNm = carType.carNameBox
Dim carPic = carType.carPictureBox
Dim carGroupBox = carType.carGroup
SplitContainer1.Panel2.Controls.Add(carGroupBox)
End Sub
So the problem occurs when i use this code to move a panel (if you replace PictureBox by Panel, even GroupBox) (it worked fine when I wanted to move one control: PictureBox1 in this sample):
'Drag To move PictureBox1 along with mouse-------------------------------------------
Dim oldX As Short
Dim oldY As Short
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
Dim X As Single = e.X
Dim Y As Single = e.Y
PictureBox1.Cursor = Cursors.SizeAll
oldX = CShort(X)
oldY = CShort(Y)
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
Dim X As Single = e.X
Dim Y As Single = e.Y
PictureBox1.Cursor = Cursors.Default
End Sub
' to limit the movement within the app----------------------------------
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = MouseButtons.Left Then
Dim ProposedLocation As New Point(PictureBox1.Left - (oldX - e.X), PictureBox1.Top - (oldY - e.Y))
PictureBox1.Left = CInt(IIf(ProposedLocation.X < 0, 0, IIf(ProposedLocation.X > SplitContainer1.Panel2.Width - PictureBox1.Width, SplitContainer1.Panel2.Width - PictureBox1.Width, ProposedLocation.X)))
PictureBox1.Top = CInt(IIf(ProposedLocation.Y < 0, 0, IIf(ProposedLocation.Y > SplitContainer1.Panel2.Height - PictureBox1.Height, SplitContainer1.Panel2.Height - PictureBox1.Height, ProposedLocation.Y)))
End If
End Sub

How would I use a for loop to edit multiple labels in Visual basic for visual studios

Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim randVal As Integer
Dim label As String
Dim val As Integer
Dim stringVal As String
For i As Integer = 1 To 256 Step 1
val = i
stringVal = CStr(val)
label = "Label" + stringVal
randVal = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
label.BackColor = Color.Green
Next
End Sub
I get an error that string has no property BackColor.
How would I be able to edit all the strings without calling them individually?
The error message is correct: there isn't a property called BackColor on a string.
There is a BackColor property on Button, however, and it looks as if you're perhaps trying to set the background color of the Button object when it's clicked. If so, then you need to get hold of the Button object before you can set the color. The event handler has made this (moderately) easy, by passing the object into your handler as the parameter "sender". The only problem is that it's sent it as an object, not as a Button, so you first have to cast it to the type you want, like this:
Dim button As Button
button = DirectCast(sender, Button)
Then later on you can set the color:
button.BackColor = Color.Green
Also, if you want to set the text of the button, you have to set it using the button.Text property:
button.Text = "What I want to see on the button"
However, you're program is hard to follow. I can't see clearly from the code why you're executing a loop, or why you're setting values like randVal that aren't being used, and so it's hard to give concrete advice.
You are trying to access a property that does not exist.
The String type in Visual Basic has two properties. See here (https://msdn.microsoft.com/en-us/library/system.string_properties(v=vs.110).aspx)
If you are trying to change the background color of your label, then you need to reference your label name, which has that property.
For example: (I am assuming you want to change the text and color on your label)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim randVal As Integer
Dim labelText As String
Dim val As Integer
Dim stringVal As String
For i As Integer = 1 To 256 Step 1
val = i
stringVal = CStr(val)
labelText = "Label" + stringVal
randVal = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
labelName.BackColor = Color.Green
Next
End Sub
Having said that, you can also use your label properties to change the text of your label.
labelName.Text = "Label" + stringVal
Here are some references
(How to change the text color. Simple?)
Also, here is a link to Label Properties (https://msdn.microsoft.com/en-us/library/system.windows.forms.label_properties(v=vs.110).aspx)
and your Buttons properties (https://msdn.microsoft.com/en-us/library/system.windows.forms.button(v=vs.110).aspx)
You managed to set the variable label to the name of your Label control, but not to the object itself. Now you need to get that Control object from its name.
VB6:
Me.Controls(label).BackColor = vbGreen
VB.Net:
Me.Controls(label).BackColor = Color.Green
However, you had an easier way to span you label controls and update their backgrounds:
Dim c as Control
For Each c In Me.Controls
If (TypeOf c Is Label) Then c.BackColor = Color.Green
Next c
If you want to reference Label1 thru Label256 in a loop, no matter which container they are in, then use Controls.Find():
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 1 To 256
Dim lbl As Label = Me.Controls.Find("Label" & i, True).FirstOrDefault
If Not IsNothing(lbl) Then
lbl.BackColor = Color.Green
End If
Next
End Sub

Why do the WordWrap and AutoSize properties on a Text Box behave differently if the text boxes are added dynamically?

I am creating some textboxes dynamically in a custom control (actually is just a panel with a binding source that adds other sub controls at runtime.
In some of those I am setting WordWrap=True because I am expecting large text. But the textbox has only one line. Also I notice that I have to set the property AutoSize to False. When I add dynamically textboxes in a form everything is working fine (even without setting the AutoSize and the WordWrap.
Dim txt as new textbox
txt.Multiline = True
txt.AutoSize = False
txt.Size = New Size(100, 50)
txt.WordWrap = True
txt.Location = New Point(10, 10)
Me.Controls.Add(txt) 'Me is my custom Control
I checked the property WordWrap for the text box after I inserted them (with double click) and it is True.
Any ideas why I face this strange behaviour in my custom control?
Edit:
The problem is that I cannot make my text to wordwrap. So although I set MultiLine to true, AutoSize to false, WordWrap to True, the text is not changing lines. I don't want to add scrollbars.
You could adjust the .Height of the TextBox when its .Text changes:
Public Class Form1
Dim txt As TextBox
Sub maketb()
txt = New TextBox
txt.Multiline = True
txt.Size = New Size(100, 50)
txt.WordWrap = True
txt.Location = New Point(10, 10)
Me.Controls.Add(txt)
End Sub
Sub SetHeight(sender As Object, e As EventArgs)
Dim target = DirectCast(sender, TextBox)
Dim fn = target.Font
Dim gr = target.CreateGraphics()
Dim lrMarginSize = target.Margin.Left + target.Margin.Right
Dim tbMarginSize = target.Margin.Top + target.Margin.Bottom
Dim h = gr.MeasureString(target.Text, fn, target.Width - lrMarginSize).Height + tbMarginSize
target.Height = CInt(Math.Ceiling(h))
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
maketb()
AddHandler txt.TextChanged, AddressOf SetHeight
txt.Text = "dfgd dfd gg hgljhhkjlh jhkkj hkjh hghjg hgjhgz hfjsdhfytu hgjahg ht gretyt jgagury agha gty ajhg ajgx"
End Sub
End Class
Guys I have to apologize.
The error was in my xml (I had MultiLine instead of Multiline), so my textbox was never Multiline.
Thanks for your time, Sorry for spending it without meaning.

Padding/ Size / Margin, when using ToolstripControlHost for a popup control

I'm using VB2008 Express. And I've been working on a "popup" to select a date range. The DateTimePicker isn't ideal because the purpose is to pick a date range, which will always be one full week, from Sunday through Saturday. The control works just fine and I'm pretty proud of it. My problem has to do with the border added when using ToolstripControlHost for this. I've included a screenshot and my code.
In the code below, assume there exists a button named "btnTimePeriod", below which I desire to show a panel, which contains a few custom items, and the panel's name is "pnlDateRangePicker".
IT WORKS... but it doesn't look right. The panel itself is 147 x 326 pixels, but notice in the attached graphic that it's adding a border around the panel which I don't want. There's a border on the top, bottom, and left... but for some reason the border on the right one is especially large. Although my code doesn't expressly set it, AutoSize = true so I would have expected it to shrink around the panel.
As required, my code already does set ShowCheckMargin and ShowImageMargin false. I haven't included the code for the DrawDateCalander Sub because it's not relevant. I believe even a blank panel would yield the same result. I have no idea where this margin is coming from. Any guidance?
Private Sub btnTimePeriod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimePeriod.Click
Call DrawDateCalendar(DatePart(DateInterval.Month, FirstDisplayedSunday), DatePart(DateInterval.Year, FirstDisplayedSunday))
Call ShowControlBelow(btnTimePeriod, pnlDateRangePicker)
End Sub
Sub ShowControlBelow(ByVal Showbutton As Control, ByVal ShownControl As Control)
Dim PopupContainer As New ToolStripControlHost(ShownControl)
PopupContainer.Margin = New Padding(0)
Dim mnuDropDown As New ContextMenuStrip
mnuDropDown.Padding = New Padding(0)
mnuDropDown.ShowCheckMargin = False
mnuDropDown.ShowImageMargin = False
mnuDropDown.Items.Add(PopupContainer)
ShowMenuBelow(Showbutton, mnuDropDown)
End Sub
Sub ShowMenuBelow(ByVal Showbutton As Control, ByVal WhichMenu As ContextMenuStrip, Optional ByVal AlignRight As Boolean = False)
Dim x As Integer = 0
Dim y As Integer = 0
Dim itscontainer As Control = Showbutton.Parent
x = Showbutton.Location.X
y = Showbutton.Location.Y
If Not itscontainer Is Nothing Then
Do Until TypeOf itscontainer Is Form
x = x + itscontainer.Location.X
y = y + itscontainer.Location.Y
itscontainer = itscontainer.Parent
If itscontainer Is Nothing Then Exit Do
Loop
End If
y = y + Showbutton.Height
If AlignRight = True Then
x = x - WhichMenu.Width + Showbutton.Width
End If
Dim xy As New Point(x, y)
WhichMenu.Show(Showbutton.FindForm, xy)
End Sub
I've never used a ContextMenuStrip for that, and maybe that's the problem.
You can try using a ToolStripDropDown instead:
Private Sub ShowControl(ByVal fromControl As Control, ByVal whichControl As Control)
'\\ whichControl needs MinimumSize set:
whichControl.MinimumSize = whichControl.Size
Dim toolDrop As New ToolStripDropDown()
Dim toolHost As New ToolStripControlHost(whichControl)
toolHost.Margin = New Padding(0)
toolDrop.Padding = New Padding(0)
toolDrop.Items.Add(toolHost)
toolDrop.Show(Me, New Point(fromControl.Left, fromControl.Bottom))
End Sub
Private Sub btnTimePeriod_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTimePeriod.Click
Call DrawDateCalendar(DatePart(DateInterval.Month, FirstDisplayedSunday), DatePart(DateInterval.Year, FirstDisplayedSunday))
'\\Call ShowControlBelow(btnTimePeriod, pnlDateRangePicker)
Call ShowControl(btnTimePeriod, pnlDateRangePicker)
End Sub