?
Solved

Messagebox.Show with scrollbar?

Posted on 2006-06-28
12
Medium Priority
?
4,747 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17001003
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
ID: 17001386
and how do I get the content of the already loaded text file in the textbox of another form then?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17001524
Not sure what you are asking. Can you give a little more detail please?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:moellert
ID: 17001932
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 64

Accepted Solution

by:
Fernando Soto earned 200 total points
ID: 17002660
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 70

Expert Comment

by:Éric Moreau
ID: 17002800
Hi moellert,

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

Cheers!
0
 
LVL 2

Author Comment

by:moellert
ID: 17004040
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 64

Expert Comment

by:Fernando Soto
ID: 17004147
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
ID: 17004289
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 64

Expert Comment

by:Fernando Soto
ID: 17004374
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
ID: 17004418
Thanks a lot.

You are a genius
0
 
LVL 64

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

718 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