Solved

Messagebox.Show with scrollbar?

Posted on 2006-06-28
12
3,557 Views
Last Modified: 2008-02-01
Hi,

I want to display a long text file. The user should not be able to alter the text file here but the file could be to long for the screen resolution. So I want a scrollbar to be displayed to let the user scroll up and down through the text file.

Here my actual code:

            Dim conffile As New System.IO.StreamReader(OpenFileDialog1.FileName)
            MessageBox.Show(conffile.ReadToEnd)

Thanks
0
Comment
Question by:moellert
  • 6
  • 5
12 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi moellert;

You can not do it that way because the MessageBox does not have a text box control and no scrollbar. If you want to do something like that create a form and place a multiline TextBox control on the form set its ReadOnly property to True and use it in place of the MessageBox control.

Fernando
0
 
LVL 2

Author Comment

by:moellert
Comment Utility
and how do I get the content of the already loaded text file in the textbox of another form then?
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not sure what you are asking. Can you give a little more detail please?
0
 
LVL 2

Author Comment

by:moellert
Comment Utility
I have already loaded the text file for reading it line by line in Form1. In Form1 I want to have a button like "Display loaded file". Now you told me I should create a new Form2 to display my file.

With
           Dim conffile As New System.IO.StreamReader(OpenFileDialog1.FileName)
I have already loaded it. So how do I get the contect of conffile in the textbox of Form2?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 50 total points
Comment Utility
The reason for a new Form2 is do to the fact that a MessageBox will not do what you want so that leaves you will creating what you need. Here is a sample projext with two Forms. Form2 is what I wanted you to create. Form1 is the main program.

Form1 Code
=============================================================================

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnDisplayLoadedFile As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnDisplayLoadedFile = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'btnDisplayLoadedFile
'
Me.btnDisplayLoadedFile.Location = New System.Drawing.Point(88, 96)
Me.btnDisplayLoadedFile.Name = "btnDisplayLoadedFile"
Me.btnDisplayLoadedFile.TabIndex = 0
Me.btnDisplayLoadedFile.Text = "Display loaded file"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 270)
Me.Controls.Add(Me.btnDisplayLoadedFile)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnDisplayLoadedFile_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnDisplayLoadedFile.Click

        Dim DisplayFile As String = "The file contents in this variable"
        Dim MyMessageBox As New Form2(DisplayFile)
        Dim results As DialogResult

        results = MyMessageBox.ShowDialog()
        If results = results.OK Then
            MessageBox.Show("The OK Button on Form2 was Pressed")
        Else
            MessageBox.Show("The Cancel Button on Form2 was Pressed")
        End If

    End Sub
End Class

=======================================================================

Form2 Code
======================================================================

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents btnOK As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.btnCancel = New System.Windows.Forms.Button
Me.btnOK = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(24, 16)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ReadOnly = True
Me.TextBox1.Size = New System.Drawing.Size(400, 88)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'btnCancel
'
Me.btnCancel.Location = New System.Drawing.Point(216, 120)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.TabIndex = 1
Me.btnCancel.Text = "Cancel"
'
'btnOK
'
Me.btnOK.Location = New System.Drawing.Point(112, 120)
Me.btnOK.Name = "btnOK"
Me.btnOK.TabIndex = 2
Me.btnOK.Text = "OK"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(456, 154)
Me.Controls.Add(Me.btnOK)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub New(ByVal msg As String)
        Me.New()
        TextBox1.Text = msg
    End Sub


    Private Sub btnOK_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnOK.Click

        Me.DialogResult = DialogResult.OK

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.DialogResult = DialogResult.Cancel

    End Sub
End Class

=====================================================================

Copy and past this code into a new project with a Form1 and Form2 and replace the code with the above code.

Fernando
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
Hi moellert,

See an article I wrote in January 2006 on how to create your own messagebox from http://emoreau.s2i.com/

Cheers!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Author Comment

by:moellert
Comment Utility
Fernando, Eric,

looks like your code could work but what is still not clear to me is how can I display my textfile?
Sure, I am able to put any text hardcoded in this line of Fernandos code: Dim DisplayFile As String = "The file contents in this variable"
but I have a complete text file which could contain several lines of text.
As written above I am reading this text file with this code:

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        End If
        Dim conffile As New System.IO.StreamReader(OpenFileDialog1.FileName)

How can I now "convert" the content of conffile in a format that could be shown in my custom message box in Form2?
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi moellert;

Just read the whole file into the variable DisplayFile as shown below.

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        End If
        Dim conffile As New System.IO.StreamReader(OpenFileDialog1.FileName)
        Dim DisplayFile As String = conffile.ReadToEnd( )
        conffile.Close( )
        Dim MyMessageBox As New Form2(DisplayFile)
        Dim results As DialogResult

        results = MyMessageBox.ShowDialog()
        If results = results.OK Then
            MessageBox.Show("The OK Button on Form2 was Pressed")
        Else
            MessageBox.Show("The Cancel Button on Form2 was Pressed")
        End If


Fernando
0
 
LVL 2

Author Comment

by:moellert
Comment Utility
cool, this one is working.
One last question.
The text that is now show in Form2.Textbox1 is completly marked. When I click in the Textbox the text is not longer selected.
Is it possible to show the text directly "un-selected"?
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Modify the TextBox1 New subroutine in Form2 as shown below. This will display the text un-highlighted.

    Public Sub New(ByVal msg As String)
        Me.New()
        TextBox1.Text = msg
        TextBox1.SelectionLength = 0
    End Sub
0
 
LVL 2

Author Comment

by:moellert
Comment Utility
Thanks a lot.

You are a genius
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
I am not a genius, at least not yet. Glad I was able to help. ;=)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

12 Experts available now in Live!

Get 1:1 Help Now