Solved

What event fires when form completes initialization in .NET

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 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

19 Experts available now in Live!

Get 1:1 Help Now