Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5051
  • Last Modified:

Messagebox.Show with scrollbar?

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
moellert
Asked:
moellert
  • 6
  • 5
1 Solution
 
Fernando SotoCommented:
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
 
moellertAuthor Commented:
and how do I get the content of the already loaded text file in the textbox of another form then?
0
 
Fernando SotoCommented:
Not sure what you are asking. Can you give a little more detail please?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
moellertAuthor Commented:
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
 
Fernando SotoCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
Hi moellert,

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

Cheers!
0
 
moellertAuthor Commented:
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
 
Fernando SotoCommented:
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
 
moellertAuthor Commented:
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
 
Fernando SotoCommented:
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
 
moellertAuthor Commented:
Thanks a lot.

You are a genius
0
 
Fernando SotoCommented:
I am not a genius, at least not yet. Glad I was able to help. ;=)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now