Textbox all properties not working when textbox added in other control - vb.net

I have added a windows form on control panel using following code
Dim frmopen As New WinForm1
panel1.Controls.Add(frmopen)
Form opens normally work also but my concern is,
I have one textbox on that WinForm1, when i am typing text in that textbox it takes input but when i am clicking in between that typed text the cursor goes to either last character or first character.
So if i have to enter characters in between, then first i have to erase the typed characters and then i have to type again.
Plz help me to work textbox normally??
Thank you

Try changing the child form's FormBorderStyle to None.
See: Windows Forms: Unable to Click to Focus a MaskedTextBox in a Non TopLevel Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim frm2 As New Form2
frm2.TopLevel = False
frm2.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.Panel1.Controls.Add(frm2)
frm2.Show()
End Sub
End Class

Related

How to add a form2 control on top of a form1.control

I have two forms. Form1 with richtextbox and form2 with listbox.
I'd like to put listbox on top of richtextbox, docking it.
I want to do exactly like this:
form2.Controls.Remove(ListBox1)
form2.hide
form1.richtextbox1.controls.add(form2.listbox1)
form1.richtextbox1.hide 'I can't hide, also listbox will hide...
form2.listbox1.location= richtextbox1 or
form2.listbox1.bounds=richtextbox1.bounds
Everything works until setting form2.listbox1.location as If richtextbox1 is hidden also listbox will be. Also, setting location and/or bounds of listbox the same as richtextbox is not completely covering the richtextbox. I also tried using the same size for both.
Gif Example
This works for me:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim LB As ListBox = Form2.ListBox1
If IsNothing(LB.Tab)
LB.Tag = LB.Size ' <-- store it for later use
End If
Me.Controls.Add(LB)
LB.Bounds = RichTextBox1.Bounds
RichTextBox1.Hide()
End Sub
Later, when you move the ListBox back, retrieve the size stored in the Tag:
If Not IsNothing(LB.Tab)
LB.Size = LB.Tag
End If
Output:
Form2.Listbox1.Parent = Form1
Form1.RichTextBox1.Visible = false
Form2.Listbox1.dock = DockStyle.Fill
The cool thing here is any code written in Form2 for Listbox1 will still function while it's sitting in Form1.
P.S. Rename your controls.

Opening a Form from UserControl and pass a Value to Form

I have a small problem, I have UserControl1 containing a Button and a TextBox. I've an instance of the user control on Form1 and I want when I click the button of user control, Form2 get opened and the text from the textbox of user control appear in TextBox1 if Form2.
NineBerry give me this example Link but I can't solve the problem yet using the link, any help please :)
This how i Don it, Thanks for my Friend Reza Aghaei for helping.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New SbillPrint
frm.smoney_txt.Text = moneys_txt.Text
frm.ShowDialog()
End Sub

Highlight textbox in Forms when mouse selected/focus on textbox VB.NET

I want to ask if how could I change a textbox color when I highlighted for each textbox in form controls.
For example if I have 50+ textbox, its really hard for me to code 50+ textbox each right?
So is there anyway I could set this in a single function?
Any help is appreciated.
Thanks,
Alvin.
Just make a new class and inherit from textbox.
Alter the events in your textbox class and use it instead of the normal textbox.
Public Class MTB
Inherits TextBox
Private Sub MTB_GotFocus(sender As Object, e As EventArgs) Handles Me.GotFocus
BackColor = Color.Aqua
End Sub
End Class
After you rebuild the project, the MTB object will be waiting for you in the toolbox.

Single code for Tab Key functionality using Enter Key in Vb.Net for a Windows Application

I write this code for tab-like behavior when user presses ENTER on textboxes for every form, which works fine.
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
However, I need to write the code once, perhaps as a sub or function in a module so that I do not have to write the same for every form. I have checked various forums including
Detecting Enter keypress on VB.NET and
Tab Key Functionality Using Enter Key in VB.Net
BUT all I get is either to write code for each textbox or for every individual form.
Has anyone tried out a single code for ALL or may be several selected forms of the application? If yes, please share with me. Writing for every form still works fine for me but i need to take advantage of OOP. Thanks
There are many ways to implement this, one of those is to create a custom textbox User Control
Adding a control to your project is very easy just right click in your project in the solution explorer ->Add->User Control
Give a name to your control ex. "tabedtextbox"
Add a textbox control to your User Control
Place the code in the keypress event of textbox1 of the UserControl
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
Compile once in order to update the whole project with the new user control.
Goto to your form and add your new User Control, you can locate it at the Toolbox panel.
Run your program and you will see the behavior of TAB when you press enter on each textbox.
If You wish to allow the user to press enter in a TextBox, instead of pressing a specific button,
you can use the acceptbutton property.
One good way to use it, is to change the property, on the Enter and Leave events of the TextBox. That you call Click event of the Button, when User press Enter inside the TextBox.
Try this code:
Private Sub tbProject_Enter(sender As Object, e As EventArgs) Handles tbProject.Enter
Me.AcceptButton = bSearch
End Sub
Private Sub tbProject_Leave(sender As Object, e As EventArgs) Handles tbProject.Leave
Me.AcceptButton = Nothing
End Sub
Private Sub bSearch_Click(sender As Object, e As EventArgs) Handles bSearch.Click
'.... actions to perfom
End Sub
Screen

Select the content of textbox when it receives focus

I have found a similar question to mine in Making a WinForms TextBox behave like your browser's address bar
Now i am trying to modify or make it some more different by making it general. I want to apply same action to all the textboxes in form without write code for each one... how many i dun know. As soon as i add a textbox in my form it should behave with similar action of selecting.
So wondering how to do it?
The following code inherits from TextBox and implements the code you mentioned in Making a WinForms TextBox behave like your browser's address bar.
Once you've added the MyTextBox class to your project you can do a global search for System.Windows.Forms.Text and replace with MyTextBox.
The advantage of using this class is you can't forget to wire all the events for every textbox. Also if you decide on another tweak for all textboxes you have one place to add the feature.
Imports System
Imports System.Windows.Forms
Public Class MyTextBox
Inherits TextBox
Private alreadyFocused As Boolean
Protected Overrides Sub OnLeave(ByVal e As EventArgs)
MyBase.OnLeave(e)
Me.alreadyFocused = False
End Sub
Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
MyBase.OnGotFocus(e)
' Select all text only if the mouse isn't down.
' This makes tabbing to the textbox give focus.
If MouseButtons = MouseButtons.None Then
Me.SelectAll()
Me.alreadyFocused = True
End If
End Sub
Protected Overrides Sub OnMouseUp(ByVal mevent As MouseEventArgs)
MyBase.OnMouseUp(mevent)
' Web browsers like Google Chrome select the text on mouse up.
' They only do it if the textbox isn't already focused,
' and if the user hasn't selected all text.
If Not Me.alreadyFocused AndAlso Me.SelectionLength = 0 Then
Me.alreadyFocused = True
Me.SelectAll()
End If
End Sub
End Class
Assuming you're going to use the accepted solution from the question you link to, all you'd need to do would be that whenever you create a new textbox, you use AddHandler to add the same 3 eventhandlers to each new textbox.
Then you need to change the event handlers to instead of referencing the textbox as this.textBox1 they'll reference it as CType(sender, TextBox) which means that they'll use the textbox that generated the event.
Edit: I'll add that line of code here as well since it's easier to read then
Private Sub TextBox_GotFocus (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
We use this custom textbox control:
Public Class TextBoxX
Inherits System.Windows.Forms.TextBox
Private Sub TextBoxX_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
SelectAll()
End Sub
end class
You can see the full project of our TextBox (on steroids) on GitHub https://github.com/logico-dev/TextBoxX