Solved

What event fires when form completes initialization in .NET

Posted on 2006-06-10
8
373 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Following an example - Why do I need to click Save button twice? 3 29
Sort GridView by ID Descending 1 17
SQL Query Help Top 1 and Distinct? 6 36
MailAddress in vb 4 29
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 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