开发者

How do I read a text file to display in richedit and show a message when this succeeds?

开发者 https://www.devze.com 2023-02-04 07:10 出处:网络
Below is my Perl code. I need help with: how to display the output into richedit when loading a text file containing a 100 line by line name list.

Below is my Perl code. I need help with:

  1. how to display the output into richedit when loading a text file containing a 100 line by line name list.

  2. after displaying the output in richedit then a small message window (popup window) will come out and tell me "your name list was successfully loaded".


use Win32::GUI;

$Win = new Win32::GUI::Window(
    -left   => 341,
    -top    => 218,
    -width  => 300,
    -height => 300,
    -name   => "Win",
    -text   => "Open File Read and Display Popup"
);

$Win->Show();

$Win->AddTextfield(
    -text   => "Load Your File TXT Here",
    -name   => "Textfield_1",
    -left   => 4,
    -top    => 24,
    -width  => 278,
    -height => 20,
);

$Win->AddButton(
    -text       => "Click Here To Load",
    -name       => "Button_1",
    -left       => 6,
    -top        => 48,
    -width      开发者_开发百科=> 275,
    -height     => 21,
    -foreground => 0,
    -onClick    => \&Load1,

);

$Win->AddRichEdit(
    -text   => "",
    -name   => "RichEdit_1",
    -left   => 5,
    -top    => 72,
    -width  => 275,
    -height => 169,
);

$Win->AddStatusBar(
    -text   => "",
    -name   => "StatusBar_1",
    -left   => 0,
    -top    => 248,
    -width  => 290,
    -height => 17,
);

Win32::GUI::Dialog();

sub Win_Terminate {
    return -1;
}

sub Load1 {

    my $file1 = Win32::GUI::GetOpenFileName(
        -owner => $Win,                   # Main window for modal dialog
        -title => "Load Name List ...",

        # Dialog title
        -filter => [                      # Filter file
            'Name List(*.txt)' => '*.txt',
            'All files'        => '*.*',
        ],
        -directory => ".",                # Use current directory
    );

    # Have select a file ?
    if ($file1) {

        # Load file to animation control
        $Win->Textfield_1->Text(
            $file1);
    }

    # Or an error messagebox with error.
    elsif (Win32::GUI::CommDlgExtendedError()) {
        Win32::GUI::MessageBox(0, "ERROR : " . Win32::GUI::CommDlgExtendedError(), "GetOpenFileName Error");
    }
    $Win->{Button_1}->Text('Edit');
    $file1 = 1;
}


You can set richedit's text with its Text method. In Load1 handler add following to display list of names:

my $data = do { local $/; open my $in,"<",$file1 or die "$file1: $!"; <$in> };
$Win->RichEdit_1->Text($data);

Message box can be displayed with Win32::GUI::MessageBox function:

Win32::GUI::MessageBox($Win,"A message","App Title",MB_ICONINFORMATION);


Full Version Answer

use Win32::GUI;

$Win = new Win32::GUI::Window(
    -left   => 341,
    -top    => 218,
    -width  => 300,
    -height => 300,
    -name   => "Win",
    -text   => "Open File Read and Display Popup"
);

$Win->Show();

$Win->AddTextfield(
    -text   => "Load Your File TXT Here",
    -name   => "Textfield_1",
    -left   => 4,
    -top    => 24,
    -width  => 278,
    -height => 20,
    -prompt => "Input birthday:",
);
$Win->Textfield_1->SetFocus();

$Win->AddButton(
    -text       => "Click Here To Load",
    -name       => "Button_1",
    -left       => 6,
    -top        => 48,
    -width      => 275,
    -height     => 21,
    -foreground => 0,
    -onClick    => \&Load1,

);

$Win->AddRichEdit(
    -text   => "",
    -name   => "RichEdit_1",
    -left   => 5,
    -top    => 72,
    -width  => 275,
    -height => 169,
);

$Win->AddStatusBar(
    -text   => "",
    -name   => "StatusBar_1",
    -left   => 0,
    -top    => 248,
    -width  => 290,
    -height => 17,
);

Win32::GUI::Dialog();

sub Win_Terminate {
    return -1;
}

sub Load1 {

    my $file1 = Win32::GUI::GetOpenFileName(
        -owner => $Win,                   # Main window for modal dialog
        -title => "Load Name List ...",

        # Dialog title
        -filter => [                      # Filter file
            'Name List(*.txt)' => '*.txt',
            'All files'        => '*.*',
        ],
        -directory => ".",                # Use current directory
    );

    # Have select a file ?
    if ($file1) {

        # Load file to animation control
        $Win->Textfield_1->Text($file1);
        my $data = do {
            local $/;
            open my $in, "<", $file1 or die "$file1: $!";
            <$in>
        };
        $Win->RichEdit_1->Text($data);
        Win32::GUI::MessageBox($Win, "A message", "App Title", MB_ICONINFORMATION);
    }

    # Or an error messagebox with error.
    elsif (Win32::GUI::CommDlgExtendedError()) {
        Win32::GUI::MessageBox(0, "ERROR : " . Win32::GUI::CommDlgExtendedError(), "GetOpenFileName Error");
    }
    $Win->{Button_1}->Text('Edit');
    $file1 = 1;

}

How to change Win32::GUI::MessageBox($Win,"A message","App Title",MB_ICONINFORMATION); with progress bar

0

精彩评论

暂无评论...
验证码 换一张
取 消