vb.net change location of 2 different texts in button - vb.net

I have a problem with another line of text in button. This code works fine but not how i want.
Another line of text displays on top of the button and i want to it display on bottom of the button.
Code:
Private Sub btnCheckPass_Paint(sender As Object, e As PaintEventArgs) Handles btnCheckPass.Paint
Dim btn = DirectCast(sender, Button)
Dim text = ("Skontrolujte silu vašich hesiel a ich prelomovosť")
Dim smallFont As New Font(btn.Font.FontFamily, btn.Font.Size - 10)
e.Graphics.DrawString(text.ToString(),
btn.Font,
Brushes.Gray,
New Rectangle(New Point(), btn.Size),
New StringFormat With {.Alignment = StringAlignment.Center})
End Sub
Does anyone know how to solve this?
Thanks

Related

How to create usercontrol dynamically?

i have a flowlayoutpanel in my winform,
i want to add some button into the flowlayoutpanel when the form is loaded,
the number of button will depend on how many item(row) exist in my dataset
dataset
Private Sub temp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ItemInfoTableAdapter1.Fill(DataSet11.ItemInfo)
End Sub
Private Sub FlowLayoutPanel1_Paint(sender As Object, e As PaintEventArgs) Handles FlowLayoutPanel1.Paint
Dim btn As New Button
Dim table as datatable = Dataset11.Tables("ItemInfo")
For 'i think this will need a looping, but i have no idea how to write'
With btn
.Text = ?? 'text will be the itemName'
.Tag = ?? 'tag will be the itemPrice'
End With
Me.FlowLayoutPanel1.Controls.Add(btn)
Next
End Sub
i have no idea what to do next, please help.
The problem here aside from adding a control is iterating from a datatable. Datatables contain DataRows. So that is what you want to get to create the the buttons based on the content of your datatable.
Try this:
Dim btn As New Button
For Each drow As DataRow In table.Rows
btn = New Button
btn.Text = drow.Item("itemName").ToString
btn.Tag = drow.Item("itemPrice").ToString
FlowLayoutPanel1.Controls.Add(btn)
Next
Also, you might take into consideration changing the event in which you are adding the controls in the FlowLayoutPanel. You can use a Form_Load event for example.

Button Array - how to pass a parameter to shared handler

I have a bit of code where i have a dynamically created array or buttons with staff pictures on them, as well as the staff's name. I've added one handler to handle any button click from any of the buttons. where i am stuck is, if you look at the code below, it all works fine, and if you click any of the buttons you get the "aha" test message. but i want the name of the staff clicked on (so btnArray(i).Text) to be passed to the handler for further processing. I tried adding a ByVal parameter to the handler but that caused an error. what's the correct way to do this? As i said, the code below works for me, i just am at a loss as to how to add the extra functionality.
Dim btnArray(staffcount) As System.Windows.Forms.Button
For i As Integer = 1 To staffcount - 1
btnArray(i) = New System.Windows.Forms.Button
btnArray(i).Visible = True
btnArray(i).Width = 80
btnArray(i).Height = 101
btnArray(i).BackgroundImage = Image.FromFile(picloc(i))
btnArray(i).BackgroundImageLayout = ImageLayout.Stretch
btnArray(i).Text = staffname(i)
Dim who As String
who = btnArray(i).Text
AddHandler btnArray(i).Click, AddressOf Me.theButton_Click
btnArray(i).ForeColor = Color.White
btnArray(i).TextAlign = ContentAlignment.BottomCenter
Dim fnt As Font
fnt = btnArray(i).Font
btnArray(i).Font = New Font(fnt.Name, 10, FontStyle.Bold)
FlowLayoutPanel1.Controls.Add(btnArray(i))
Next i
End Sub
Private Sub theButton_Click()
MsgBox("aha")
End Sub
First, correct the signature of your shared handler.
Private Sub theButton_Click(sender As Object, e As EventArgs)
End Sub
Once that is done getting the text of the button clicked is a simple matter.
Private Sub theButton_Click(sender As Object, e As EventArgs)
Dim textOfButtonClicked As String = DirectCast(sender, Button).Text
MessageBox.Show(textOfButtonClicked)
End Sub
The sender is the button that was clicked. Since signatures use objects for the sender the DirectCast 'changes' it to button and you then can access the .Text property of the button.
If there are more manipulations you want to perform on the clicked button you could do it this way
Private Sub theButton_Click(sender As Object, e As EventArgs)
Dim whBtn As Button = DirectCast(sender, Button) ' get reference to button clicked
Dim textOfButtonClicked As String = whBtn.Text
MessageBox.Show(textOfButtonClicked)
'e.g. change the color
whBtn.BackColor = Color.LightYellow
End Sub

My code only works when I insert a random MsgBox

I've encountered a very bizarre problem when trying to get a screenshot of a TableLayoutPanel in my form.
I have this code (taken from another question (How to get a screenshot, only for a picturebox); code courtesy of user "Chase Rocker"):
Dim s As Size = TableLayoutPanel1.Size
Dim memoryImage = New Bitmap(s.Width, s.Height)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
Dim ScreenPos As Point = Me.TableLayoutPanel1.PointToScreen(New Point(0, 0))
memoryGraphics.CopyFromScreen(ScreenPos.X, ScreenPos.Y, 0, 0, s)
Form3.PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
Form3.PictureBox1.BringToFront()
Form3.PictureBox1.Image = memoryImage
Now, here comes my problem. This code gives me a blank picture. It takes the screenshot apparently, but all I can see is white. Now, I was trying to see if the size was correct, so I was messing with MsgBox. I add this line to the code:
MsgBox("Random Message")
Getting
Dim s As Size = TableLayoutPanel1.Size
MsgBox("Random Message")
Dim memoryImage = New Bitmap(s.Width, s.Height)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
Dim ScreenPos As Point = Me.TableLayoutPanel1.PointToScreen(New Point(0, 0))
memoryGraphics.CopyFromScreen(ScreenPos.X, ScreenPos.Y, 0, 0, s)
Form3.PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
Form3.PictureBox1.BringToFront()
Form3.PictureBox1.Image = memoryImage
By some reason I don't know, the screenshot now works. I don't see white anymore, but the actual screenshot of the TableLayoutPanel. For me is very weird that the code only works with a MsgBox. Maybe I'm missing something. Does anyone know why this happens? Thank you!
How about if you try to make the TableLayoutPanel draw itself to a bitmap instead? This can be done using the Control.DrawToBitmap() method.
Dim s As Size = TableLayoutPanel1.Size
Dim memoryImage As New Bitmap(s.Width, s.Height)
TableLayoutPanel1.DrawToBitmap(memoryImage, New Rectangle(New Point(0, 0), s))
Form3.PictureBox1.Image = memoryImage
If the TableLayoutPanel fill happens in the same event handler where you grab the image then Windows has not draw the UI for the elements added to the TableLayoutPanel. Only when you exit from the event handler, the winforms engine has the opportunity to draw everything.
Adding a MessageBox changes everything because calling Show (a modal call that interrupts your code and pass control back to window) allows the Winform engine to draw the pending updates and your code works.
You can add a Timer control and put the code that execute the ScreenShoot in the Timer event.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
......
' code that fills the TableLayoutPanel
......
Dim tim1 = new System.Windows.Forms.Timer()
tim1.Interval = 1
AddHandler tim1.Tick, AddressOf tim1Ticked
tim1.Start()
End Sub
Private Sub tim1Ticked(sender As Object, e As EventArgs)
......
' Code that execute the screenshoot.
......
Dim t = DirectCast(sender, System.Windows.Forms.Timer)
t.Stop()
End Sub

How to manage dynamically created controls in VB.NET?

I just understand how to make controls dynamically in VB.NET (I mean, only part of adding a new one)
But, unlike VB6, it seems hard to handle those dynamic things.
When I click the DONE button, I want to make an array filled with the text of textboxes.
At the same time, I want to make a Delete button that removes the button itself and the textbox in the same line.
Is there any simple method or an sample code for this?
Thank you!
Drop a TableLayoutPanel on your form, called pnlLayout, and also the Add button called btnAdd. Configure TableLayoutPanel to have two columns, adjust column width as needed.
Paste below code into your form:
Public Class Form1
Dim deleteButtons As List(Of Button)
Dim textBoxes As List(Of TextBox)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
deleteButtons = New List(Of Button)
textBoxes = New List(Of TextBox)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim elementCount As Integer = deleteButtons.Count
Dim txt As New TextBox
txt.Width = 100
txt.Height = 20
textBoxes.Add(txt)
Dim btn As New Button
btn.Width = 100
btn.Height = 20
btn.Text = "Delete " & elementCount.ToString
AddHandler btn.Click, AddressOf btnDelete
deleteButtons.Add(btn)
pnlLayout.SetCellPosition(txt, New TableLayoutPanelCellPosition(0, elementCount))
pnlLayout.SetCellPosition(btn, New TableLayoutPanelCellPosition(1, elementCount))
pnlLayout.Controls.Add(btn)
pnlLayout.Controls.Add(txt)
End Sub
Private Sub btnDelete(sender As Object, e As EventArgs)
Dim senderButton As Button = DirectCast(sender, Button)
Dim txt As TextBox = textBoxes(deleteButtons.IndexOf(senderButton))
pnlLayout.Controls.Remove(senderButton)
pnlLayout.Controls.Remove(txt)
End Sub
End Class
By default, it will have no textboxes and no Delete buttons, you can add as many rows of "Textbox + Delete button" as you want. When you press Delete, the row will be removed (and everything shifted to accommodate the empty space).
For the textbox'ex part:
Dim strcol() As String = {TextBox2.Text, TextBox3.Text}
For Each strtxt In strcol
MessageBox.Show(strtxt)
Next
It really depends on your code, but, if you have their name use this to delete the buttons &/ textbox'es:
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
If TypeOf Me.Controls(i) Is TextBox Then
If Me.Controls(i).Name = "TextBox2" Then
Me.Controls.RemoveAt(i)
End If
End If
If TypeOf Me.Controls(i) Is Button Then
If Me.Controls(i).Name = "Button3" Then
Me.Controls.RemoveAt(i)
End If
End If
Next
But it depends on your code...

How to add new tabs to a TabControl and be able to update controls in them?

I need to be able to programmatically create new tabs on a TabControl, add controls to them, and be able to update the controls in each tab from another function. I already have a function to add tabs to the control, and to add controls to those tabs when they are created, but I'm stuck as to update the controls after they have been created.
EDIT: This is what I have to make the tabs and add the controls:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "(empty)"
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Parent = tabpage
textbox2.Parent = tabpage
textbox1.Location = New Point(10, 10)
textbox2.Location = New Point(10, 30)
TabControl1.TabPages.Add(tabpage)
End Sub
Urgh. I can't seem to get back into the account I used to post this question, so I have to post my follow-up to Tim's question in the comments for the previous answer as a new answer.
Debug.WriteLine(TabControl1.TabPages.Item(2).Controls.Find("textbox1", True).Count) returns 0. The tab and the controls have been created prior.
Ok - I'll give it a shot, but I'm real rusty with WinForms, slightly less rusty with VB.NET. You'll need to locate the control you want to update, and you should be able to do that through the Controls collection of the appropriate container - in this case, most likely a TabPage:
TextBox tb1 = CType(tabpage.Controls.Find("textBox1", false), TextBox)
tb1.Text = "I set the text!"
Syntax might be slightly off, but hopefully this will at least point you in the right direction.
See Control.ControlCollection.Find Method
UPDATED
Hans Passant suggested that this isn't working because you didn't set the Name property (I'm assuming he means the name of the controls, not the tab page). I did a little more reading on the ControlsCollection.Find method, and MSDN says "Searches for controls by their Name property and builds an array of all the controls that match." You (and I) were trying to find the control by the instance name (textbox1, textbox2) - which were the instance names for the two controls, not the control names.
So try this instead:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "(empty)"
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Parent = tabpage
textbox2.Parent = tabpage
textbox1.Location = New Point(10, 10)
textbox2.Location = New Point(10, 30)
textbox1.Name = "textbox1"
textbox2.Name = "textbox2"
TabControl1.TabPages.Add(tabpage)
End Sub
Then you can find the control using:
TextBox tb1 = CType(TabControl1.TabPages.Item(TabControl1.TabPages.Count - 1).Controls.Find("textbox1", True)(0), TextBox)
tb1.text = "Test"
Give that a try and see if it works for you. The key (and what I missed looking at your code last night as it was past my bedtime for me) was there was now way to identify the control in the Find method.
I know this is old, but just an opinion, write a function that creates AND returns the control you want to add to the tabpage. In that case, you will have a reference to the control ready at hand.
For example:
Public Function CreateNewListBoxInsideNewPageTab() As ListBox
Dim newTab As New TabPage()
newTab.Text = "Tab " & TabControl1.TabPages.Count + 1
Dim newLst As New ListBox
newLst.Dock = DockStyle.Fill
newTab.Controls.Add(newLst)
TabControl1.TabPages.Add(newTab)
TabControl1.SelectedTab = newTab
Return newLst
End Function
Now when I call this function, I'll have the new listbox as an object:
Dim newListBox as ListBox = CreateNewListBoxInsideNewPageTab()
newListBox.Items.Add("This is a new listbox item!")
can i add tabcontroll elements after programm started?
For i = 0 To frmMain.cmbZielSpache.Items.Count - 1
Dim cBox = New CheckBox()
cBox.Name = "GEN_" & i
cBox.Location = New Point(offsetX, offsetY)
cBox.Text = frmMain.cmbZielSpache.Items(i)
If frmMain.cmbZielSpache.Items(i) = frmMain.cmbZielSpache.Text Or My.Settings.chkTranslate_normal_alleSprachen = True Then
cBox.Checked = True
End If
offsetX = offsetX + 120
Me.Controls.Add(cBox)
AddHandler cBox.CheckedChanged, AddressOf checkChangedHandler
Next i
Instead of using Me.Controls i wanna add the checkboxes after programm started dynamically.