How to remove textboxes on button click (vb.net)? - dynamic

I am not knowing about how to delete textboxes on the click of a button in my Windows form.
Here is my story:
Initially, I wanted to add textboxes on the click of a button, and upon searching the net, I was able to find out, on this forum, how to do this. https://stackoverflow.com/questions/15461978/adding-new-textbox-with-button-click
I used the code that user "Rajaprabhu Aravindasam" (2nd answer) gave. Here is only part of my code that I used (in order not to confuse you):
Private Sub Button_AddTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_AddTask.Click
count += 1
TabPage_TaskStructure.Controls.Add(New Label() With _
{.Name = "Label_Task" & count})
TabPage_TaskStructure.Controls.Add(New RichTextBox() With _
{.Name = "RichTextBox" & count})
End Sub
Now, as you can see, the purpose of the button 'Button_AddTask" ('+' button on my form) is to create rich textboxes and their respective label. Assume that the rich textboxes and the labels are being created below one another.
Beside the '+' button, there is a '-' button. What I want is to use this '-' button to delete all created textboxes sequentially up. That is, if I have created 4 textboxes with the button '+', textbox no.4 will be deleted first when I click the '-' button, then no. 3 after a second click, then no.2 after a third click and so on.
The sequential part is not a problem, I know perfectly how to do it. Here is part of the code I tried:
Private Sub Button_DeleteTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DeleteTask.Click
TabPage_TaskStructure.Controls.Remove(New Label() With _
{.Name = "Label_Task" & count})
TabPage_TaskStructure.Controls.Remove(New RichTextBox() With _
{.Name = "RichTextBox" & count})
count -= 1
If count = 1 Then
Button_DeleteTask.Visible = False
End If
End Sub
What I did, logically, was simply replace 'Add' with 'Remove', but I am not knowing with what to replace 'New'. And so my question is: What needs to be used instead of 'New'? If I need to use an entirely different code, please do tell me.
Any help is greatly appreciated.
Update:
Ok, I have done some research since I've asked this question and I've been able to deduce that the removal of the controls (Label & Rich Text Box) can be done by using a For Each loop. In my code for the '-' button, I have replaced the first 4 lines of code with this:
Dim Ctrl As Control
For Each Ctrl In TabPage_TaskStructure.Controls
If TypeOf Ctrl Is Label And Ctrl.Name = "Label_Task" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
ElseIf TypeOf Ctrl Is RichTextBox And Ctrl.Name = "RichTextBox" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
Next
And so, when I click the '-' button, the program checks whether each control on the tab page (TabPage_TaskStructure) is a label with the name ("Label_Task" & count') or a rich text box with the name ("RichTextBox" & count), and if they are, they will be removed. The rest of the code is the same.
It's working, however not completely. It's working for the labels as they are successfully being removed, but not for the rich text boxes. I cannot understand why. I have tried the code for other controls such as date time pickers and text boxes, and it's not working for any of these either. It seems to be working for labels only.
Also, I have tried using the code on a default rich text box, and it worked! But for rich text boxes being created at run time, it's not working.
Can anyone clarify me on this?

I'm not really good at vb but from simple logic if and when an "IF" argument is accepted the code will move to "End If" and skip "Else" altoghter.
Try this instead:
Dim Ctrl As Control
For Each Ctrl In TabPage_TaskStructure.Controls
If TypeOf Ctrl Is Label And Ctrl.Name = "Label_Task" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
If TypeOf Ctrl Is RichTextBox And Ctrl.Name = "RichTextBox" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
Next

Related

Remove previous selection highlighting in RichTextBox without scrolling

I have a form with a RichTextBox (RTB) and a listBox.
When the end user selects an item in the listbox, any matched text in the RTB is highlighted (full code removed for brevity).
re = New Regex(txtToFind)
For Each m In re.Matches(rtbMain.Text)
rtbMain.[Select](m.Index, m.Length)
rtbMain.SelectionColor = Color.White
rtbMain.SelectionBackColor = System.Drawing.SystemColors.Highlight
Next
When the user left mouse clicks in the RTB I want the previously highlighted text to be cleared. This is the standard windows behaviour - If you manually select some text in an RTB with the mouse, it is highlighted, click anywhere back in the RTB and the highlighting disappears. My programatically selected text remains selected.
I have some partially working code (below). I can clear all the highlighted text, but it is by process of selecting everything, changing the colour back and then deselecting it again. I know it is not efficient, the RTB flickers and I am sure it is not the correct way to do it. Can I emulate the standard windows behaviour?
Also using my code, it scrolls to the first line when entering the RTB a second time.
I get around this the first time by returning the top visible line index before clearing the text and then selecting that line again afterwards and using ScrollToCaret(). This only works on the first pass. Subsequent MouseDown events select the top row regardless of where the user has clicked so nothing can be manually highlighted in the RTB.
Private Sub rtbMain_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles rtbMain.MouseDown
Dim topIndex As Integer = rtbMain.GetCharIndexFromPosition(New System.Drawing.Point(1, 1))
Dim topLine As Integer = rtbMain.GetLineFromCharIndex(topIndex)
If e.Button = Windows.Forms.MouseButtons.Right Then
'Do nothing (Context Menu)
Else
rtbMain.SelectAll()
rtbMain.SelectionColor = Color.Black
rtbMain.SelectionBackColor = Color.White
rtbMain.DeselectAll()
rtbMain.Select(topIndex, 0)
rtbMain.ScrollToCaret()
End If
End Sub
I need my code to emulate the standard windows behaviour - clear selected text highlighting on MouseDown and leave the mouse cursor where the user has clicked.
Any help anyone can offer is gratefully appreciated.
I think you may be overthinking this one.
In the right click event, try RtbMain.SelectionLength = 0

How to make paragraphed text appear in a multi-line textbox when a treenode is selected

I reckon this is probably a simple thing to do but I can't seem to know how to pull it off as I am new to VB.Net.
On my form, I have a treeview control I have populated with nodes using the Nodes Collection TreeNode Editor. I also have a multi-line textbox. When a node is selected, some pre-defined text assigned to that particular node appears in my textbox.
What I want to achieve is for paragraphs of text with formatting, to be inputted into the textbox and not just single lines of text as shown in my code below.
For instance, when the user clicks a node titled 'Soccer', I would like a formatted body of text like:
“This is a sport.
It is a sport played between two teams of eleven players with a spherical ball.
It is played by 250 million players in over 200 countries.”
to appear in my textbox. Please how can I get the code to do this? I am using Visual Basic 2010 Express. The code I am using at the moment is shown below. Thank you in advance.
Private Sub TreeView1_AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim SelectedNode As TreeNode
SelectedNode = TreeView1.SelectedNode
If SelectedNode.Text = "Soccer" Then
TextBox1.Text = "This is a sport." 'I would like to have paragraphed text appear in 'textbox1 instead of a single line of text
Else
If SelectedNode.Text = "Moon" Then
TextBox1.Text = "This is the name of a car." 'I would like to have paragraphed text 'appear in textbox1 instead of a single 'line of text
End If
End If
End Sub
You should add '\n' in end of your line.

Reversi VB.net logic behind it

I am trying to make the reversi game in VB.Net. I have some difficulties translating the game`s logic into vb.net
If a button is black and the button next to it is white,than the button next to the white one will be black wen pressed.
newButton.tag = colum of button + (row of button * amount of columns)
-> I made 64 buttons via a function loop and added a tag
Dim knop As Button = sender
Dim value As String = knop.Tag
If value = "...(?)" Then
knop.BackColor = Color.Black
If ....(?)
End If
End If
I already made a scheme with the label of the buttons, but I find it hard to implement the logic. Can someone help me out with thid one?
EDIT: http://i.stack.imgur.com/3gdrJ.png
If you use Dim ButtonList As List(Of List(Of Button)) and add the buttons to the form in runtime you can add each the button for each row to a list then add that list to ButtonList. Now you can access each button by the indexes in the 2 dimensional list.
Since you're changing the backcolor just use that instead of using the tag.

Opening new form after right click on selected record in continuous form

In my access database before I was using a list box in the form for having list of items and I had below code for opening new form after right click on each selected item in list box.
Private Sub ItemList_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const RIGHTBUTTON = 2
If Button = RIGHTBUTTON Then
DoCmd.OpenForm "frmShortcut_GenerateTask"
DoCmd.MoveSize udtPos.X * mp.TwipsPerPixelX, udtPos.Y * mp.TwipsPerPixelY
End If
End Sub
Now I am using a continuous form instead of list box and I have defined a [isselected) field for selecting each record in continuous form after clicking on that. Now my problem is how I have to write code for right clicking and opening new form.
I used the same code I had used for list box, but it does not work and nothing happened.
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const RIGHTBUTTON = 2
If Button = RIGHTBUTTON Then
DoCmd.OpenForm "frmShortcut_GenerateTask"
DoCmd.MoveSize udtPos.X * mp.TwipsPerPixelX, udtPos.Y * mp.TwipsPerPixelY
End If
End Sub
Private Sub P_Click()
On Error Resume Next
Me.IsSelected = Not Me.IsSelected
' Save the status
Me.Dirty = False
' Force conditional highlighting
P_ForceHighLight
' Update display in SF_Selected
Me.Parent("SF_Selected").Requery
ActiveControl.SelLength = 0
On Error GoTo 0
End Sub
I recommend either using a DoubleClick event in all of your textboxes and combos, or else putting a small button at one edge of your continuous form that allows the user to open records. Right-click is going to give you problems (so will labels and image controls) because this event doesn't cause (or ensure) that the Form's internal Recordset Bookmark property is actually moved to the record they right-clicked on. In my experience, only buttons, textboxes, and comboboxes will move the Bookmark to the record the user is trying to select.
To Test this, try putting code at the top of your different routines that shows you what record is selected:
MsgBox "RecordID = " & Me!RecordIDField
If you are consistently getting the correct ID, then your problem has to do with how your opening your new form, passing parameters or arguments, etc.

Accessing controls located in a dynamically created user control vb.net

I am a casual programmer with not a lot of experience. I am happy I have made it this far on my own (with help of course from this site and others like it). But now I need some help.
I have created a user control with several text boxes, masked text boxes, combo boxes, a check box and 3 buttons.
I have created a form (Form1) with a tab control (TabControl1) that has 1 tab page on it (TabPage1). I have added my user control to TabPage1 and the control assumes the name ContactTab1. This was done through the VB.net form design, not by code.
When I run my form I have code so that when I click on my add button, it adds another tab with my user control added to it (no matter which tab I may be on). It works great, I can add as many tabs as I want. When I click on my edit or delete button, they work great in the sense that I know which tab the button is on when it gets clicked. My problem is when I click the edit button I need to set ckbDeleteContact.Checked = False and ckbDeleteContact.Visible = False on the tab that the button was clicked. When I click the delete button I need to set ckbDeleteContact.Checked = True and ckbDeleteContact.Visible = True on the tab that the button was clicked. I can access the check box on the first tab without a problem with the statement ContactTab1.ckbDeleteContact.Checked = False.
So my question is, how do I access all these text boxes, masked text boxes, combo boxes, and my check box on these dynamically added controls? Below is my code for Form1 and I have commented out what I need working:
Public Class Form1
Private intTabPage As Integer = 1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TabPage1.Text = "Contact #" & intTabPage
ContactTab1.ckbDeleteContact.Checked = False
ContactTab1.ckbDeleteContact.Visible = False
TabPage1.Name = "TabPage" & intTabPage
intTabPage = intTabPage + 1
End Sub
Private Sub UC_btnAddContact_Click() Handles ContactTab1.UC_btnAddContact_Click
AddNewTab()
End Sub
Private Sub UC_btnEditContact_Click() Handles ContactTab1.UC_btnEditContact_Click
'**DEBUG: See which tab the button is on when clicked
MessageBox.Show("The edit button from the following tab was clicked: " & TabControl1.SelectedTab.Name() & vbCrLf & "The edit button on the following contact tab was clicked: " & TabControl1.SelectedTab.Controls.Item(0).Name(), "Check", MessageBoxButtons.OK, MessageBoxIcon.Information)
'This code is what needs to work. ContactTabObject would have naming convention "ContactTabX" where X = the tab # 1 through the highest tab #
'ContactTabObject.ckbDeleteContact.Checked = False
'ContactTabObject.ckbDeleteContact.Visible = False
End Sub
Private Sub UC_btnDeleteContact_Click() Handles ContactTab1.UC_btnDeleteContact_Click
'**DEBUG: See which tab the button is on when clicked
MessageBox.Show("The delete button from the following tab was clicked: " & TabControl1.SelectedTab.Name() & vbCrLf & "The delete button on the following contact tab was clicked: " & TabControl1.SelectedTab.Controls.Item(0).Name(), "Check", MessageBoxButtons.OK, MessageBoxIcon.Information)
'This code is what needs to work. ContactTabObject would have naming convention "ContactTabX" where X = the tab # 1 through the highest tab #
'ContactTabObject.ckbDeleteContact.Visible = True
'ContactTabObject.ckbDeleteContact.Checked = True
End Sub
Function AddNewTab()
Dim NewTab As New TabPage
Dim NewContactTab As New ContactTab
TabControl1.Controls.Add(NewTab)
TabControl1.SelectTab(NewTab)
NewTab.Text = "Contact #" & intTabPage
NewTab.BackColor = System.Drawing.Color.Transparent
NewTab.Controls.Add(NewContactTab)
NewTab.Name = "TabPage" & intTabPage
NewContactTab.Location = New System.Drawing.Point(6, 6)
NewContactTab.BackColor = System.Drawing.Color.Transparent
NewContactTab.ckbDeleteContact.Checked = False
NewContactTab.ckbDeleteContact.Visible = False
AddHandler (NewContactTab.btnAddContact.Click), AddressOf UC_btnAddContact_Click
AddHandler (NewContactTab.btnEditContact.Click), AddressOf UC_btnEditContact_Click
AddHandler (NewContactTab.btnDeleteContact.Click), AddressOf UC_btnDeleteContact_Click
NewContactTab.Name = "ContactTab" & intTabPage
intTabPage = intTabPage + 1
End Function
End Class
Once I get this figured out, I should be good to go and I should be able to get the rest on my own. In case you are wondering, I will also be filling in the options for my combo boxes with data from a database. I will then be using the form to take all the data in it and either adding, editing, or deleting the information from a database.
Thanks in advance.
As #HansPassant said you just need to add properties to your user control to get access to your controls in it. I'm not a vb.net guy, but I think this is going to help you:
Public Function MyTextbox() As System.Windows.Forms.TextBox
Return Textbox1
End Function
You can write this in your user control code.
Ok, maybe I was not the clearest in my post or I just don't understand the encapsulation thing. I can access all my controls since they are standard controls. I just needed to know how I could get the name of the parent control, which in this case is the user defined control named ContactTabX where X = 1 through n controls that were added when I pressed my add button n times. I could always access them by saying something likeContactTab5.ckbDeleteContact.Visible = True or whatever. I did not want to hardcode since I would not be sure how many tabs were added so I wanted a way to know which tab I was on when the button was pressed that way I could change that check box property on that particular tab (since every tab is identical).
I spent hours trying to figure it out and well here is what I was able to figure out about 10 mins after posting the question (go figure). I hope this helps anyone else. And for you experts out there, any feedback is appreciated on my solution. I always like to learn :)
So replacing the subs I originally posted with these worked perfectly.
Private Sub UC_btnEditContact_Click() Handles ContactTab1.UC_btnEditContact_Click
'**DEBUG: See which tab the button is on when clicked
'MessageBox.Show("The edit button from the following tab was clicked: " & TabControl1.SelectedTab.Name() & vbCrLf & "The edit button on the following contact tab was clicked: " & TabControl1.SelectedTab.Controls.Item(0).Name(), "Check", MessageBoxButtons.OK, MessageBoxIcon.Information)
Dim Contact As ContactTab = TabControl1.SelectedTab.Controls.Item(0)
Contact.Name = TabControl1.SelectedTab.Controls.Item(0).Name()
Contact.ckbDeleteContact.Visible = False
Contact.ckbDeleteContact.Checked = False
Contact = Nothing
End Sub
Private Sub UC_btnDeleteContact_Click() Handles ContactTab1.UC_btnDeleteContact_Click
'**DEBUG: See which tab the button is on when clicked
' MessageBox.Show("The delete button from the following tab was clicked: " & TabControl1.SelectedTab.Name() & vbCrLf & "The delete button on the following contact tab was clicked: " & TabControl1.SelectedTab.Controls.Item(0).Name(), "Check", MessageBoxButtons.OK, MessageBoxIcon.Information)
Dim Contact As ContactTab = TabControl1.SelectedTab.Controls.Item(0)
Contact.Name = TabControl1.SelectedTab.Controls.Item(0).Name()
Contact.ckbDeleteContact.Visible = True
Contact.ckbDeleteContact.Checked = True
Contact = Nothing
End Sub
Thanks again for the input.