Solved

Messagebox.Show with scrollbar?

Posted on 2006-06-28
12
3,842 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
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 62

Expert Comment

by:Fernando Soto
ID: 17001524
Not sure what you are asking. Can you give a little more detail please?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 62

Accepted Solution

by:
Fernando Soto earned 50 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 62

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 62

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 62

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

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