VB.NET Link Label to another Label - vb.net

I am new to VB.NET and also am self taught, so basically I know nothing. I have written some software (~2000 lines) it is fairly basic. However many of those lines are just changing different labels (on different tabs) when the user does different things.
I find modifying labels to be very annoying, mostly when I have 3-4 labels that always show the same thing but in different places. I have done some searching related to trying to link or bind these labels but I don't think I have found what I am looking for. I'm sure I am searching the wrong terminology or something very simple.
My question is: Is there a simple way to link labels together so I only need to change text/color on one label and the other "linked" labels follow suit?
Below is a snippet of what I am trying to simplify.
lblStatus.Text = "Connected to transmitter!"
lblStatus.BackColor = Color.Green
lblStatus.ForeColor = Color.White
lblRadioStatus.Text = "Connected to transmitter!"
lblRadioStatus.BackColor = Color.Green
lblRadioStatus.ForeColor = Color.White
lblStatusRec.Text = "Connected to transmitter!"
lblStatusRec.BackColor = Color.Green
lblStatusRec.ForeColor = Color.White
lblRadioSetup.Text = "Connected to transmitter!"
lblRadioSetup.BackColor = Color.Green
lblRadioSetup.ForeColor = Color.White
These labels always show the same but are displayed on different tabs. There has to be a better way.

You can create an array of labels declared at the form level and then fill this array after the call to InitializeComponent with the labels that you want to change.
Then write a method that loops over the array and change all the labels with your values.
For example:
Public Class Form1
Dim myLabels() As Label
Public Sub New()
InitializeComponent()
myLabels = New Label() {lblStatus, lblStatusRec, lblRadioStatus, lblRadioSetup};
End Sub
Private Sub UpdateLabel(labelText As String, fore As Color, back As Color)
For Each lbl In myLabels
l.Text = labelText
l.BackColor = back
l.ForeColor = fore
Next
End Sub
End Class
Now, everytime you need to change these labels you call the UpdateLabel method with the parameters required.

Related

Image won't change in picturebox, vb.net

I'm trying to code it so that i can create a picture box from a method in a class. However when my picture box is drawn it doesn't display any image, it only shows a white square of the specified dimensions in the specified location.
Here is the code which i am using to create said picture box:
Public Sub DrawEnemy(ByRef formInstance)
Dim enemypic As New PictureBox
enemypic.Image = Image.FromFile("C:\fboi1\Enemy.Png")
enemypic.Width = 64
enemypic.Height = 64
enemypic.Location = New Point(Me.EnemyPosX, EnemyPosY)
enemypic.Visible = True
formInstance.Controls.Add(enemypic)
End Sub
And here is where i am calling the method from:
Dim Enemy1 As New computerControlled(1, 1)
Enemy1.DrawEnemy(Me)
Please add the following code in your DrawEnemy() method:
enemypic.SizeMode = PictureBoxSizeMode.StretchImage
When i drag the console window suddenly the white box turns into the image i wanted.
Aha! This means the code is not causing the form to be repainted. We can trigger that by calling the Invalidate() function.
Public Sub DrawEnemy(formInstance As Form)
Dim enemypic As New PictureBox
enemypic.Image = Image.FromFile("C:\fboi1\Enemy.Png")
enemypic.Width = 64
enemypic.Height = 64
enemypic.Location = New Point(Me.EnemyPosX, EnemyPosY)
enemypic.Visible = True
formInstance.Controls.Add(enemypic)
formInstance.Invalidate()
End Sub
If you're calling this several times in a loop, you would instead handle this after the loop, where you also block repainting (to prevent flickering) until the loop is finished.
form.SuspendLayout()
For Each enemy In ...
'...
DrawEnemy(form)
Next
form.Invalidate()
form.ResumeLayout()
It's also possible you only need to Invalidate() the picturebox.

Is it possible to change the text of a label in another form without using .Show() or .ShowDialog

I have a problem on Vb.net
My question is if it is possible to change the text of a Label without using frm.Show () or .ShowDialog().
Example :
frmMain As frmMain = New frmMain
frmMain.lblText.Text = "Hello please help"
without using a .Show() since it is already shown in a panel :
Yes, it is possible.
What Plutonix said in the comment, I guess is use the label in another form which already exists instead of making a new one.
For example, add a label1 in NewForm1 while designing the form.
Then, in Form1, you can do this:
NewForm1.label1.text = "Your text here"
Otherwise, if you don't want to put a label already in the design time,
you can add it like this:
Dim NewLabel1 As New Label
With NewLabel1
'add properties here
.Text = "your text"
.Parent = Me 'set the parent of the label as new form
.Location = New Point(1, 1)
'other properties here
End With

VB.NET ClientSize

I have one form and I want it to be fullscreen, but taskbar should be still visible. And I want it to have one Panel on it, whose borders are 10px away from form borders
I tried hundreds of combinations, and I simply can't achieve this.
here's my code
Public Class Form1
Sub New()
InitializeComponent()
WindowState = FormWindowState.Maximized
Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
Dim p1 As New Panel()
p1.Location = New Point(10, 10)
p1.Size = New Size(ClientSize.Width - 20, ClientSize.Height - 20)
p1.BackColor = Color.Blue
Controls.Add(p1)
End Sub
End Class
what I want: http://i.imgur.com/4BxoBeh.png
what I get: http://i.imgur.com/QynIdaU.png
I would take an entirely different approach where there is no need to calculate anything:
WindowState = FormWindowState.Maximized
Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
Padding = New Padding(10)
Dim p1 As New Panel()
p1.Location = New Point(0, 0)
p1.Dock = DockStyle.Fill
p1.BackColor = Color.Blue
Controls.Add(p1)
Your calculation is correct for a form that takes entire screen but is not maximized, which you can see by unmaximizing it as soon as it appears. The reason is that you are observing the form sizes from the constructor which is a bit too early (namely, even though you are setting WindowState = FormWindowState.Maximized before everything else, ClientSize still has values corresponding to non-maximized window because the window has not yet been created and shown). If you move your original code to e.g. a Form.Load handler it will give the opposite result - looking correct when the form is maximized and incorrect if not.
The padding approach works as expected in all cases.

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.