Solved

What event fires when form completes initialization in .NET

Posted on 2006-06-10
8
369 Views
Last Modified: 2012-05-05
Writing a custom component, based on TextBox, in WinForms, not ASP.NET.
I wish to fire an event when the parent form is ready, but not sooner.
In the Event Handler, I check a parameter passed in from the component and use it to set the
Form's Text (Caption bar).  It works fine when I change the text with the keyboard, but the very first use
from the custom component sets the Form's Text, which then gets over-written by the form.

So what I need is for the custom control to tap into an event, which fires when the parent finishes its initial startup.
Then, it would notify each control, mine included, that it may fire initial events.

Any ideas?
Brother Bill
0
Comment
Question by:brotherbill999
  • 3
8 Comments
 
LVL 5

Expert Comment

by:aaronfeng
ID: 16879831
Have you tried to override the OnShown in the Form?  That would be a good place for you to fire that event.  Not sure if that is what you want, but you can give it a try.

Cheers,

Aaron
http://geekswithblogs.net/afeng
0
 

Author Comment

by:brotherbill999
ID: 16880986
Aaron, this is easy to do within the form, but I am building a winforms component.
What I need is an event that the form would raise (send) to all controls indicating that the window is fully initialized, and that the control can raise (fire) its initial events.

In my case, I want the component, a custom TextBox to raise an event indicating whether the Text string will fit in the box, or will be partially obscured.  I know how to RaiseEvent on the TextChanged Event, but it seems to fire before the Form is ready.  

In the Form it has something like...
Public Sub OnFullyVisibleChanged (fullyVisible as Boolean)
     If fullyVisible Then
          Me.Text = "Fully Visible Text"
     Else
          Me.Text = "Partially Obscured Text"
     End

The problem is that what the Form Displays the first time is its Caption, such as Form1

Brother Bill
0
 
LVL 2

Accepted Solution

by:
shy_talk earned 500 total points
ID: 16882317
Think I know the problem. Your routine is not returning the result of your test.


Imports System.Windows.Forms
Imports System.Drawing

Public Class MyTextBox
    Inherits System.Windows.Forms.TextBox

    ' Inherited from TextBox, so Me is a TextBox
    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        MyBase.OnTextChanged(e)
        ' If it all fits, then background color is Window, else Control
        If FullyVisible(Me) Then
            BackColor = Drawing.SystemColors.Window
        Else
            BackColor = Drawing.SystemColors.Control
        End If
    End Sub

    Public Function FullyVisible() As Boolean
        Return FullyVisible(Me)
    End Function

    ' This is the work horse...
    Public Function FullyVisible(ByVal txtBox As TextBox) As Boolean
        If txtBox Is Nothing Then Throw New ArgumentException("FullyVisible: txtBox may not be Nothing")
        Dim Result As Boolean
        If txtBox.Text.Length = 0 Then
            ' No matter how small the textbox is, you see all that there is, that is Nothing
            Result = True
        ElseIf txtBox.Multiline = False Then
            ' Create a copy of original text box, and copy key attributes
            Dim txtBox2 As New TextBox
            Dim pt2_2 As New Point(2, 2)
            txtBox2.Font = txtBox.Font
            txtBox2.Size = txtBox.Size
            txtBox2.Text = txtBox.Text
            ' c1 is char index 2 pixels from left, typically 0
            txtBox2.SelectionStart = 0
            Dim c1 As Integer = txtBox2.GetCharIndexFromPosition(pt2_2)
            ' c2 is char index 2 pixels from left, after scrolling to make last character visible
            txtBox2.SelectionStart = txtBox2.Text.Length
            Dim c2 As Integer = txtBox2.GetCharIndexFromPosition(pt2_2)
            ' If character indexes are equal, then you see all there is to see
            Result = (c1 = c2)
        Else
            ' MultiLine = True
            ' Client Height is the maximum vertical area inside the box where text may be written, excluding margins
            Dim clientHeight As Integer = txtBox.ClientSize.Height
            ' Line Height is number of pixels for each row, including white space
            Dim lineHeight As Integer = Math.Ceiling(txtBox.Font.GetHeight)
            ' Max Lines is number of visible lines of text
            Dim maxLines = Math.Floor(clientHeight / lineHeight)  ' Maximum rows that can be shown

            Dim len = txtBox.Text.Length
            ' Find which line (origin 1) that the last character is on
            Dim line1 As Integer = 1 + IIf(len = 0, 0, Me.GetLineFromCharIndex(len))

            ' If its within the max lines, its fully visible, else there is some hidden text
            Result = (line1 <= maxLines)

        End If
'************************
'ADD THIS LINE TO YOUR CODE!!!
        Return Result
'************************
    End Function
End Class

Public Class Form1


'Code I put in the parent form...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        Application.DoEvents()

        If MyTextBox1.FullyVisible = True Then
            Me.Text = "Visible"
        Else
            Me.Text = "Not Visible"
        End If

        Application.DoEvents()
    End Sub
End Class

Works for me. Hope this is OK.

All the best, and good luck - shy
0
 
LVL 2

Expert Comment

by:shy_talk
ID: 17074650
An acknowledgement of some kind would have been nice! Having worked the example, fixed the bug and posted the working code, I would appreciate a bit of something. All the best - shy.
0
 
LVL 2

Expert Comment

by:shy_talk
ID: 17129259
Many thanks, TheLearnedOne and hope it all worked out for brotherbill - shy.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now