Visual Studio 2019, Visual Basic.
I have a Form. On load i add a lot of textbox. I need to hide layout during this process. I try:
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SuspendLayout()
MakePanel() ' Sub adding textbox
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
But it showing all process. I Try
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Panel1.visible = false
MakePanel() ' Sub adding textbox
Me.Panel1.visible = true
End Sub
But nothing ...
I don't have the rep to comment but your second attempt is on the right track. The problem likely lies in your MakePanel() function not actually adding all the text boxes within Panel1's control but rather as part of your form itself. So when you go to hide the panel, it doesn't hide the text boxes within it.
They need to be added via Panel1.Controls.Add to actually 'hide' with Panel1
ie (starting with a new blank form):
Public Panel1 = New Panel()
Public TextBox1 = New TextBox()
Public TextBox2 = New TextBox()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Controls.Add(Panel1)
Panel1.Visible = False
MakePanel()
Panel1.Visible = True
End Sub
Private Sub MakePanel()
Panel1.Location = New Point(3, 3)
Panel1.Name = "Panel1"
Panel1.Size = New Size(100, 100)
Panel1.TabIndex = 0
TextBox1.Location = New Point(3, 3)
TextBox1.Name = "TextBox1"
TextBox1.Size = New Size(49, 20)
TextBox1.TabIndex = 0
Panel1.Controls.Add(TextBox1)
TextBox2.Location = New Point(3, 29)
TextBox2.Name = "TextBox2"
TextBox2.Size = New Size(49, 20)
TextBox2.TabIndex = 1
Panel1.Controls.Add(TextBox2)
End Sub
Hope this helps!
Related
I've been trying to create a code to simulate a queue for something(haven't got to that yet) for school and am trying to create multiple picture boxes and store them in a list. For some reason they are not appearing...anyone got any suggestions?
Public Class Form1
Dim peoples As New List(Of PictureBox)()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = randomnumber(100, 500)
End Sub
Sub loopover()
Timer1.Interval = randomnumber(100, 500)
End Sub
Function randomnumber(lower As Integer, upper As Integer)
Randomize()
Return Int((upper * Rnd()) + lower)
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
loopover()
newqueuemember()
End Sub
Private Sub newqueuemember()
Dim pictureBox As New PictureBox
pictureBox.Width = 50
pictureBox.Visible = True
pictureBox.Height = 50
Dim selectperson As Integer = randomnumber(1, 3)
If selectperson = 1 Then
pictureBox.Image = My.Resources.person1
ElseIf selectperson = 2 Then
pictureBox.Image = My.Resources.person2
Else
pictureBox.Image = My.Resources.person3
End If
pictureBox.Location = New Point(10, 20)
peoples.Add(pictureBox)
End Sub
End Class
Under:
peoples.Add(pictureBox)
add:
Me.Controls.Add(pictureBox)
Here's a C# reference (easily translated to VB):
https://support.microsoft.com/en-us/help/319266/how-to-programmatically-add-controls-to-windows-forms-at-run-time-by-u
Use a flow layout panel, Define the size (width, height) of the objects, then simply add them to the flow layout panel, you can even have a scrollbar if the length of the list of picture boxes is longer than the height of the panel.
FlowLayoutPanel1.controls.add(picturebox_object)
I'm making a custom login form for a program that is invoked similar to a messagebox, returning a DialogResult to show whether the login was successful. There are three buttons, each returning a different DialogResult. (The code for actually checking the login data is not implemented yet.) However, only the Cancel button actually fires the appropriate Event, the other buttons do nothing.
Public Class LoginBox
Inherits Form
Private WithEvents btnLogin As New Button With {.Text = "Login", .Size = New Size(80, 25), .Location = New Point(230, 90), .BackColor = Color.Silver}
Private WithEvents btnCancel As New Button With {.Text = "Cancel", .Size = New Size(80, 25), .Location = New Point(230, 120), .BackColor = Color.Silver}
Private WithEvents btnReadOnly As New Button With {.Text = "Open in" & vbCrLf & "readonly" & vbCrLf & "mode", .Size = New Size(80, 55), .Location = New Point(130, 90), .BackColor = Color.Silver}
Private WithEvents tbNaam As New TextBox With {.Size = New Size(180, 20), .BackColor = Color.Silver, .Location = New Point(130, 20)}
Private WithEvents tbWachtwoord As New TextBox With {.Size = New Size(180, 20), .BackColor = Color.Silver, .Location = New Point(130, 55)}
Private lblNaam As New Label With {.Text = "Inlognaam:", .ForeColor = Color.Silver, .Location = New Point(65, 23)}
Private lblWachtwoord As New Label With {.Text = "Wachtwoord:", .ForeColor = Color.Silver, .Location = New Point(53, 58)}
Private Shared WaitForButton As New System.Threading.EventWaitHandle(False, Threading.EventResetMode.ManualReset)
Private Shared antwoord As DialogResult
Private Sub New()
Me.FormBorderStyle = FormBorderStyle.FixedDialog
ControlBox = False
Text = "Inlog IC agenda"
AcceptButton = btnLogin
CancelButton = btnCancel
Size = New Size(350, 205)
BackColor = Color.DimGray
Controls.Add(btnLogin)
Controls.Add(btnCancel)
Controls.Add(btnReadOnly)
Controls.Add(tbNaam)
Controls.Add(tbWachtwoord)
Controls.Add(lblNaam)
Controls.Add(lblWachtwoord)
End Sub
Public Overloads Shared Function Show(ByVal ShowReadOnlyButton As Boolean) As DialogResult
Dim X As New LoginBox
If Not ShowReadOnlyButton Then
X.btnReadOnly.Visible = False
X.Size = New Size(350, 180)
X.btnCancel.Location = New Point(130, 90)
End If
X.CenterToParent()
X.ShowDialog()
WaitForButton.WaitOne()
Return antwoord
End Function
Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
'TODO: check login data, if incorrect, show error message
antwoord = DialogResult.OK
WaitForButton.Set()
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
antwoord = DialogResult.Cancel
WaitForButton.Set()
End Sub
Private Sub btnReadOnly_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReadOnly.Click
antwoord = DialogResult.Ignore
WaitForButton.Set()
End Sub
End Class
Testing shows the Cancel button working as expected. The other two buttons fail to raise the appropriate event, even though (in my eyes) they are coded in exactly the same way.
Button handlers are working, but the form is not closed. The reason is that you miss a line in event handlers.
DialogResult = antwoord
So, your event handlers should look similar like shown in the code below.
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
'TODO: check login data, if incorrect, show error message
antwoord = DialogResult.OK
DialogResult = antwoord
WaitForButton.Set()
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
antwoord = DialogResult.Cancel
DialogResult = antwoord
WaitForButton.Set()
End Sub
Private Sub btnReadOnly_Click(sender As Object, e As EventArgs) Handles btnReadOnly.Click
antwoord = DialogResult.Ignore
DialogResult = antwoord
WaitForButton.Set()
End Sub
"Cancel" is treated like DialogResult.None, so you had an impression that "Cancel" only works correctly.
With the help of the code below (by LarsTech - drag and detach tabpages
) I was able to detach a tabpage and place it into a new form. But when I close that form the dragged tabpage doesn't return to its original position.
If anyone out there can help me program this, it'll be great!
Private Sub TabControl1_MouseMove(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseMove
If (e.Button = MouseButtons.Left) Then
TabControl1.DoDragDrop(TabControl1.SelectedTab, DragDropEffects.Move)
End If
End Sub
Private Sub TabControl1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles TabControl1.GiveFeedback
e.UseDefaultCursors = False
End Sub
Private Sub TabControl1_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControl1.QueryContinueDrag
If Control.MouseButtons <> MouseButtons.Left Then
e.Action = DragAction.Cancel
Dim f As New Form
f.Size = New Size(400, 300)
f.StartPosition = FormStartPosition.Manual
f.Location = MousePosition
Dim tc As New TabControl
tc.Dock = DockStyle.Fill
tc.TabPages.Add(TabControl1.SelectedTab)
f.Controls.Add(tc)
f.Show()
Me.Cursor = Cursors.Default
Else
e.Action = DragAction.Continue
Me.Cursor = Cursors.Help
End If
End Sub
I've been able to produce the code which returns the tabpage to its original place when the form closes, however the page doesn't return to the original index position. Below is the updated code:
Public f As Form
Private Sub TabControl1_MouseMove(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseMove
If (e.Button = MouseButtons.Left) Then
TabControl1.DoDragDrop(TabControl1.SelectedTab, DragDropEffects.Move)
End If
End Sub
Private Sub TabControl1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles TabControl1.GiveFeedback
e.UseDefaultCursors = False
End Sub
Private Sub TabControl1_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControl1.QueryContinueDrag
f = New Form
If Control.MouseButtons <> MouseButtons.Left Then
e.Action = DragAction.Cancel
AddHandler f.FormClosing, AddressOf ClosingDraggableWindow_EventHandler
f.Size = New Size(400, 300)
f.Name = TabControl1.SelectedTab.Text
f.TabIndex = TabControl1.SelectedTab.TabIndex
f.StartPosition = FormStartPosition.Manual
f.Location = MousePosition
Dim tc As New TabControl()
tc.Dock = DockStyle.Fill
tc.TabPages.Add(TabControl1.SelectedTab)
f.Controls.Add(tc)
f.Show()
End If
End Sub
Sub ClosingDraggableWindow_EventHandler()
Dim tbp As New TabPage()
tbp.Text = f.Name
Dim tbc As TabControl = f.Controls(0)
Dim tbp2 As TabPage = tbc.TabPages(0)
TabControl1.TabPages.Insert(f.TabIndex + 1, tbp2)
End Sub
Changing this sentence
f.TabIndex = TabControl1.SelectedTab.TabIndex
to
f.TabIndex = TabControl1.SelectedIndex
it works fine.
TabIndex represents the order into the form but not the index into the TabControl1.
I am interested in vb and am confused how to save the contents of a Picturebox as a png file when button1 is clicked. When I try to do it now, it produces the error I show below. I can't figure out how to fix this because I'm new to using visual basic. The debugger seemed to catch something but I can't make sense of it. I would be extremely pleased if someone would help me to fix this. Here is the code:
'*** Acknowlegements ***
'Ideas for this code came from the MicroSoft "Scribble" sample code,
'Christian Graus's excellent arcticle on a C++ code called "Doodle"
'and the MicroSoft website.
'By John Buettner
'26 July 2003
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form
'Namespace myPaint
Inherits System.Windows.Forms.Form ' Of course ;)
Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement
'*** below I declare some values for an Alpha and other user selected variables
'these will be used as I expand this program for a higher level use.
Dim myAlpha As Integer = 100 ' declare a Alpha variable
Dim myUserColor As New Color() 'this is a color the user selects
Friend WithEvents Button1 As Button
Friend WithEvents SaveFileDialog1 As SaveFileDialog
Dim myPenWidth As Single = 5 'set pen width variable
'**************************************************************
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form))
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.GhostWhite
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Location = New System.Drawing.Point(0, 33)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(696, 423)
Me.PictureBox1.TabIndex = 2
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Image = Global.Ubernote.My.Resources.Resources.fl
Me.Button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.Button1.Location = New System.Drawing.Point(12, 4)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 3
Me.Button1.Text = "Save"
Me.Button1.UseVisualStyleBackColor = True
'
'SaveFileDialog1
'
'
'Form
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(696, 456)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "Form"
Me.Text = "Draw"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down
mousePath.StartFigure() ' The L mouse is down so we need to start a new line in mousePath
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down
Try
mousePath.AddLine(e.X, e.Y, e.X, e.Y) 'Add mouse coordiantes to mousePath
Catch
MsgBox("No way, Hose!")
End Try
End If
PictureBox1.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
' Here is where we do the actual painting
Try ' error trapping
myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
'change the value of myUserColor
myAlpha = 100 ' This will give the color a Alpha effect, you can set this to 255 if you want a full color
Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen
e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path! :)
Catch
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
SaveFileDialog1.ShowDialog()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Try
' Dim FileToSaveAs As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, SaveFileDialog1.FileName)
' PictureBox1.Image.Save(FileToSaveAs, System.Drawing.Imaging.ImageFormat.Jpeg)
'PictureBox1.Image.Save(FileToSaveAs)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim path As String
Dim pic As Image
pic = PictureBox1.Image
SaveFileDialog1.ShowDialog()
pic.Save(SaveFileDialog1.FileName)
End Sub
try this
Hi good day guys i would just like to ask if this is a good code for putting multiple labels in a second form with the text from the first form using one button? Thanks in advance and happy coding to all.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim lbl As Label = New Label 'Create your Label
'change the location..
lbl.Location = New Point(50, 50) 'Set Label Location
lbl.Text = TextBox1.Text 'Set Label Text
lbl.ForeColor = Color.Black 'Set Label ForeColor
frm2.Controls.Add(lbl) 'Add Label to it
Return
'change the location..
lbl.Location = New Point(10, 20) 'Set Label Location
lbl.Text = TextBox1.Text 'Set Label Text
lbl.ForeColor = Color.Black 'Set Label ForeColor
frm2.Controls.Add(lbl) 'Add Label to it
Return
End Sub
it only shows the first label but it does not show the next labels that I will input...
form2 codes
Public Class Form2
Public lbl As New Label
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lbl)
End Sub
End Class
Problem solved..Many thanks to Sir Plutonix and Steve for their help I really appreciate it :) I accepted the suggestion of Plutonix to use multiple textboxes rather than one textbox and I used the sample code of Steve in starting my project.. Many thanks again :)
You need to declare a global class level variable that keeps a reference to your Form2, then when you click the button and the global class level variable is nothing (first time creation) you create the Form2 instance, add the labels and most important add an event handler that will be called when the frm2 instance will be closed. Then show the form and bring it to the forefront.
If your user reclicks on the button, a new form will not be created (frm2 is not nothing at this point), instead, if your user closes the frm2 the event handler will be called and you reset the internal class level variable to nothing. Now if you user clicks again on the button the frm2 will be recreated
Private frm2 As Form2
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
if frm2 Is Nothing then
frm2 = new Form2
AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed
Dim lbl As Label = New Label
lbl.Location = New Point(50, 50)
lbl.Text = "FirstLabelText"
lbl.ForeColor = Color.Black
frm2.Controls.Add(lbl)
Dim lbl2 = New Label
lbl2.Location = New Point(10, 20)
lbl2.Text = "TextForSecondLabel"
lbl2.ForeColor = Color.Black
frm2.Controls.Add(lbl2)
End if
frm2.Show()
frm2.BringToFront
End Sub
Sub Form2HasBeenClosed(sender As Object, e As FormClosedEventArgs)
frm2 = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
if frm2 IsNot Nothing Then
frm2.Show(Me) 'Show Second Form
End If
End Sub
Private LabelCount As Integer = 0
Private Sub Button3_Click(yada yada yada
' I think this is actually fixing a nonexistant problem
' stemming from misunderstanding the issue
if frm2 Is Nothing then
frm2 = new Form2
AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed
Dim lbl As Label = New Label
lbl.Text = TextBox1.Text
lbl.ForeColor = Color.Black
Select Case LabelCount
Case 0
lbl.Location = New Point(50, 50)
Case 1
lbl.Location = New Point(10, 20)
Case Else
' not specified
Exit Sub
End Select
LabelCount +=1 ' dont forget this
frm2.Controls.Add(lbl)
End if
frm2.Show()
frm2.BringToFront
End Sub