passing values present in one user control to another using VB - controls

i have a user control with a text box and i need to access the value entered in this text box in a label present in another user control. How do i do that in vb.Thanks in advance.

create a shared event in the first user control (UserControl1):
Friend Shared Event GetTextBoxText(ByVal myString As String)
you can then raise this event with a button on (UserControl1)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'raise the event with the text from the text box
RaiseEvent GetTextBoxText(TextBox1.Text)
End Sub
on your second user control (UserControl2) add a handler to the event in your Constructor :
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'this will let us handle the event from (UserControl1)
AddHandler UserControl1.GetTextBoxText, AddressOf SetLabelText
End Sub
Private Sub SetLabelText(ByVal myString As String)
Label1.Text = myString
End Sub
now whenever you click the button on (UserControl1) the text will be displayed in the label on UserControl2
you could also add the event handler on any control and respond to GetTextBoxText event

Related

VSTO Outlook Visual Basic - Handle Mail Item Closure

I'm creating an Outlook Add-In, and am struggling to detect and handle the event when a mail item is closed. The inspectors_NewInspector function is triggered sucessfully (as I can obtain the subject and sender of the email), but I cannot get the mailItem_close() function to be called at all.
Public Class ThisAddIn
Public WithEvents inspectors As Outlook.Inspectors
Public WithEvents mailItem As Outlook.MailItem
Private Sub ThisAddIn_Startup() Handles Me.Startup
inspectors = Me.Application.Inspectors
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
If Inspector.CurrentItem.size > 0 And Inspector.CurrentItem.class = 43 Then
mailItem = Inspector.CurrentItem
Dim mSubject As String = mailItem.Subject
Dim mFrom As String = mailItem.SenderEmailAddress
Dim mTime As String = mailItem.ReceivedTime
'MsgBox(mSubject)
End If
End Sub
Private Sub mailItem_Close()
MsgBox("closing")
End Sub
It seems you have copied the code from VBA to a VB.NET add-in. In add-ins you need to use the AddHandler method to attach an event handler programmatically, see How do I create an event handler for a programmatically created object in VB.NET? for more information.
Note, each time the NewInspector event is fired the mailItem is overwritten, so the previous item instance is lost and can't fire events:
Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
If Inspector.CurrentItem.size > 0 And Inspector.CurrentItem.class = 43 Then
mailItem = Inspector.CurrentItem
Dim mSubject As String = mailItem.Subject
Dim mFrom As String = mailItem.SenderEmailAddress
Dim mTime As String = mailItem.ReceivedTime
'MsgBox(mSubject)
End If
End Sub
Private Sub mailItem_Close()
MsgBox("closing")
End Sub
Instead, you need to maintain a list or array of items, so each time the NewInspector event is fired you will add an item to the list to keep the reference alive, so the events will be fired.
You may find the Implement a wrapper for inspectors and track item-level events in each inspector article helpful.
With huge thanks to Eugene, the solution was indeed to use AddHandler.
Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
If Inspector.CurrentItem.size > 0 And Inspector.CurrentItem.class = 43 Then
inspectorEvents = TryCast(Inspector, Outlook.InspectorEvents_Event)
AddHandler inspectorEvents.Close, AddressOf inspectorEvents_Close
End If
End Sub
Public Sub inspectorEvents_Close()
Dim currentInspector As Outlook.Inspector
currentInspector = Application.ActiveInspector
mailItem = currentInspector.CurrentItem
Dim mSubject As String = mailItem.Subject
Dim mFrom As String = mailItem.SenderEmailAddress
Dim mTime As String = mailItem.ReceivedTime
MsgBox("closing" & mSubject)
End Sub
I'm occasionally getting multiple close handlers firing when a mail message is closed, but I should be able to get around that by maintaining an array / list of inspector ids and ensuring only one is created per mail item.

vb - edit specific cell on double click and set read only after

I am developing simple app in visual basic and what I want to do is:
On load whole DataGridView1 is ReadOnly
Next if user will double click on cell, then it turns edit mode for that cell
I try to do this by:
Private Sub CellDouble(ByVal sender As Object,
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellDoubleClick
DataGridView1(e.ColumnIndex, e.RowIndex).ReadOnly = False
DataGridView1.BeginEdit(e.RowIndex)
End Sub
But it doesnt even react. (function is triggered but code not working)
Final step is to Set Read only back to that cell after edit
I simply did:
Private Sub CellDouble(ByVal sender As Object,
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellDoubleClick
DataGridView1.BeginEdit()
End Sub
and it's working fine...

VB6 When closing the Parent form do unload queryunload or terminate events fire in Child Forms

I've asked this question before but I must not have been clear because the answer turned out not to be correct as far as I can tell .. so here goes again.
I have a VB6 application .. it has main menu that opens up Forms and dialogs (not MDI I don't think)
I want to save the position of any open forms or dialogs when the application closes so the next time I open the application the positions will be restored.
I hoped that when I close down the application that ANY of queryUnload, unload, terminate would fire in the 'child' windows or dialogs and I could save their position .. but nothing seems to fire.
I've put break points on all the above events, but when I close down the application nothing gets hit.
VB6 is not my usual gig .. so I'm probably thinking too much .net ..
Is there a way this can be done in VB6 ..
Edit: So it seems if I click the close cross in the top right corner I 'end' the application. Is there a way in VB6 to edit this behavior so I could instigate a graceful close down ?
All 3 events fire, but the Terminate event might not be fired when you expect it.
Create a test project consisting of 1 MDI form, 1 MDI child form, 1 normal form and add the following code:
MDI form:
'MDI form : name=MDIForm1
Option Explicit
Private Sub MDIForm_Click()
End
End Sub
Private Sub MDIForm_Load()
Form1.Show
Form2.Show vbModeless, Me
WindowState = vbMaximized
End Sub
Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "MDI form QueryUnload event"
End Sub
Private Sub MDIForm_Terminate()
MsgBox "MDI form Terminate event"
End Sub
Private Sub MDIForm_Unload(Cancel As Integer)
MsgBox "MDI form Unload event"
End Sub
MDI child:
'1 form: name=Form1 MDIChild=true
Option Explicit
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "MDI child QueryUnload event"
End Sub
Private Sub Form_Terminate()
MsgBox "MDI child Terminate event"
End Sub
Private Sub Form_Unload(Cancel As Integer)
MsgBox "MDI child Unload event"
End Sub
Normal form:
'1 form: name=Form2
Option Explicit
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "Form2 QueryUnload event"
End Sub
Private Sub Form_Terminate()
MsgBox "Form2 Terminate event"
End Sub
Private Sub Form_Unload(Cancel As Integer)
MsgBox "Form2 Unload event"
End Sub
You will see the QueryUnload and Unload events are fired (in this order), but the Terminate event is fired when you close down the MDI form.
When you click the background of the MDI form, then End will be called and no event will be fired
"Child" forms do get the usual events. Notice that on Form_QueryUnload the UnloadMode parameter is vbFormOwner.
"Child" forms are shown with explcit owner form like this:
'--- using global references
Form2.Show vbModal, Form1
Form3.Show , Form1 '--- Form3 is modeless
'--- using instances
With New Form2
.Show vbModal, oOwnerForm
End With
With New Form3
.Show , oOwnerForm
End With

how to put a number in a text box using a button in visual basic application forms

I am trying to make a calculator on visual basic application forms and I have most of the codes sorted. How do I make it so when I press the number button, it puts the number in the text box. It also needs to be able to work so if I pressed 1 then 2 then 3, it appears a 123.
Just set the SelectedText() property of the TextBox to the Text() property of your button.
For example:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox1.SelectedText = Button1.Text
TextBox1.Focus()
End Sub
If you make all the buttons fire the same handler, then it becomes:
Private Sub AllButtons_Click(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, _
Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button0.Click
Dim btn As Button = DirectCast(sender, Button)
TextBox1.SelectedText = btn.Text
TextBox1.Focus()
End Sub

Enabling CTRL+Z property in masked edit textbox using VB.Net

I am using a masked edit text box in my windows application that was developed by using vb.net.
In normal text boxes (CTRL+Z- to revert back to original value) is working fine. But In case of Masked Edit Textboxes its not working fine.
Can any one please help me about this.
This ctrl+Z should provide the functionality as same as normal textbox.
You can use a variable to store the current text by programming the Leave event and check during the KeyDown event for the combination of Control+Z:
Dim oldText As String = ""
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MaskedTextBox1.KeyDown
If e.Control AndAlso e.KeyCode = Keys.Z Then MaskedTextBox1.Text = oldText
End Sub
Private Sub MaskedTextBox1_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MaskedTextBox1.Leave
oldText = MaskedTextBox1.Text
End Sub

Resources