How to add (via code) one or more Buttons in a WinForm TextBox or RichTextBox? Specifically, I want to add buttons at the end of each text line, before the VbCrLf character.
It will be something like this:
Friend WithEvents Button1 As Button
Private Sub RichTextBox1_Click(sender As Object, e As EventArgs) Handles RichTextBox1.Click
Button1 = New Button ' Create new instance
Me.Button1.Size = New System.Drawing.Size(75, 23) ' give the button a size
Button1.Text = "My button" ' set the button text
Me.Button1.UseVisualStyleBackColor = True ' make it look windows like
Dim pos As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart) 'determine the button position
RichTextBox1.Controls.Add(Button1) ' get it inside the rich text box
Button1.Location = New Point(RichTextBox1.Right - Button1.Width, pos.Y + RichTextBox1.Top) ' set the button position
End Sub
Of course it does not need to be in the Click event, you perhaps may use the KeyPress event to determine if the user pressed enter and then insert it at that point. Also play with the position (POS variable) to change the button position.
Nandostyle
Related
I am creating a textbox and button dynamically like this inside a FlowLayoutPanel:
Dim txtRating As New TextBox
txtRating.Name = "buildingRating_" + intCount.ToString
Dim btnAddRating As New Button
btnAddRating.Name = "addRating_" + intCount.ToString
And then I create an event handler for the button:
AddHandler btnAddRating.Click, AddressOf HandleAddRatingButtonClick
Each textbox and button will be associated with each other with the intCount value, so it would look like this:
buildingRating_1 addRating_1
buildingRating_2 addRating_2
buildingRating_3 addRating_3
etc....
I need to get the value of the textbox that is in associated with the button.
For example, if the user clicks the button named addRating_1, the text value of buildingRating_1 will be saved a database...
Is there a way to get that association or pass intCount into the btnAddRating.Click event that I am creating?
Thanks!
Extract the index from the name of the button, and use it to recreate the name of the textbox. Then find the textbox on the form.
Private Sub HandleAddRatingButtonClick(sender As Object, e As EventArgs)
Dim button = DirectCast(sender, Button)
Dim index = button.Name.Split("_"c).Last()
Dim textboxname = "buildingRating_" & index
Dim textbox = Me.Controls.OfType(Of TextBox).Single(Function(tb) tb.Name = textboxname)
Dim text = textbox.Text ' this is the text
End Sub
If the textbox is in a container (panel, groupbox, etc.) then instead of Me.Controls... do Container.Controls...
I created a simple TableLayoutPanel (6 by 6) with buttons in each cell. I have a click handler that executes whenever any of the button is pressed (in my case, change color of the button from red to green)
see eg.
But now I need to change the color of the button next to the button i clicked (eg. When I click on button 2 3, the button 3 3 will also change color).
Any ideas?
You can cast the Sender argument on the click handler to a Control and then use that to determine the clicked control's position. Then you can use that position to compute you adject controls and retrieve a reference to them.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ctrl As Control = TryCast(sender, Control)
If ctrl IsNot Nothing Then
Dim pos As TableLayoutPanelCellPosition = TableLayoutPanel1.GetCellPosition(ctrl)
If pos.Row < (TableLayoutPanel1.RowCount - 1) Then
Dim nextCtrl As Control = TableLayoutPanel1.GetControlFromPosition(pos.Column, pos.Row + 1)
If nextCtrl IsNot Nothing Then
' do something with nextCtrl
nextCtrl.BackColor = Color.Red
End If
End If
End If
End Sub
I'm using MDI container to run my bussiness application I created for my client.
Since using MDI means that when I open several forms they will still run in background all the time untill I close them manualy.
What I need is to make User Control or anything else that could preview all opened forms in Tab Form so my client can easily close all or some of opened forms without closing a form he is curently viewing.
For now I have used this code, so for now only first clicked item from menu appears as button, but not others clicked menu items.
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
Dim Button As New Button
Me.Panel5.Controls.Add(Button)
Button.Text = e.ClickedItem.Name
Button.Width = 50
Button.Height = 25
End Sub
Now I need to write a code to add more buttons bellow, also should add a code for adding buttons only when I click on SubMenu item (The one when is clicked new Form appear).
And also, I should now add a little Close button into previewed User-Button-Control.
From your comments, I understand that your ideas regarding adding buttons at runtime are not too clear and thus I am including a small code which hopefully will help you on this front. Start a new project and put a Panel (Panel5) and a Button (AddButtons) on it, and write this code:
Dim lastButtonIndex, lastLeft, lastTop As Integer
Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
Dim curButton As Button = DirectCast(sender, Button)
If (curButton.Name = "Button1") Then
'do Button1 stuff
End If
'etc.
End Sub
Private Sub addNewButton()
lastButtonIndex = lastButtonIndex + 1
lastLeft = lastLeft + 5
lastTop = lastTop + 5
Dim Button As New Button
With Button
.Name = "Button" + lastButtonIndex.ToString()
.Text = "Button" + lastButtonIndex.ToString()
.Width = 50
.Height = 25
.Left = lastLeft
.Top = lastTop
AddHandler .Click, AddressOf Button_Click
End With
Me.Panel5.Controls.Add(Button)
End Sub
Private Sub ButtonAddButtons_Click(sender As System.Object, e As System.EventArgs) Handles AddButtons.Click
addNewButton()
End Sub
This code will add a new button to the panel every time you click on AddButtons. All the buttons will have an associated Click Event (the same one for all of them): Button_Click. The way to know which button is the current one inside this method is via sender, as shown in the code (you can put as many conditions as buttons. The names are given sequentially starting from 1; but you can take any other property as reference, curButton is the given Button Control).
Bear in mind that one of the problems you have to take care of is the location of the buttons. The code above has a very simplistic X/Y values (Left/Top properties) auto-increase which, logically, will not deliver what you want.
I have a DataList, and each data list has a label and button, I want to get the text of the label when the button is clicked for each data list row ?
I am using vb.net.
I assume that you're handling the DataList's ItemCommand event by setting the Button's CommandName property.
Sub Item_Command(sender As Object, e As DataListCommandEventArgs)Handles DataList1.ItemCommand
' What command was triggered?
If e.CommandName = "YourCommandName" Then
' Get the Label in the DataListItem of the clicked button
Dim lbl = DirectCast(e.Item.FindControl("Label1"), Label)
Dim labelText = lbl.Text
End If
End Sub
Here's a scernerio that will get what I am trying to accomplish....
Say I have a form with a textbox to enter a number and a button called "Create"
When a user enters a number and clicks create the form gets populated with the number of buttons entered in the textbox and the title of the buttons are labeled by consecutive numbers.
For example if you enter 5 the form will populate with 5 buttons labeled button1, button2, ...button5
When you click on these newly created buttons a messagebox will popup stating the buttons name.
Basically I need to know how to create events and populate them with code I guess
dynamically.
Please respond in a solution for a windows app not web. No JavaScript.
Please can someone respond with a code example this idea is a little foggy to me
'Make sure that the textbox contains a number.
If IsNumeric(TextBox1.Text) Then
'Make sure that it's a positive number.
If CInt(TextBox1.Text) > 0 Then
For i = 1 To CInt(TextBox1.Text)
Dim x As New Button
x.Name = "Button" & i.ToString
x.Top = 100 + (i * 30) 'To avoid stacking.
AddHandler x.Click, AddressOf y 'Add the event here.
'Add it to the form.
Controls.Add(x)
Next
End If
End If
Private Sub y(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(CType(sender, Button).Name)
End Sub