Is it possible to Show a MessageBox or Something simular to a MessageBox that by it self will show for about 2 seconds so you doesn´t have to press OK.

So what happens is that a "messageBox" will appear for about 2 seconds and the by itself dissapear ?
(A messageBox without an "OK" button)

MessageBox::Show("Text here");

Recommended Answers

All 5 Replies

Yes, create your own message box using a dialog box, then set up a timer event to kick off in 2 seconds. In the timer event handler destroy the dialog box.

Thanks. I am not sure how to do any of these I think.
A dialog box is not the same as a MessageBox ?

A timer Event, is this the TimerClass that I can find in the Toolbox ?

So I beleive the first Step is to put code inside a buttoncontrol that will show a
Dialog Box ? I am not sure how to do this ?

Yes, create your own message box using a dialog box, then set up a timer event to kick off in 2 seconds. In the timer event handler destroy the dialog box.

they are all win32 api functions. Here is a tutorial that will get you started with writing windows programs. After doing all the exercises in that tutorial you will know how to create a dialog box. Then it will be pretty easy to add SetTimer() to setup a timer event and the event handler function, and KillTimer() to stop it.

Thank you. I will check this out.

/J

From what Edward has seen, you're using Windows Forms and not Win32. Without any of the bells and whistles of the standard message box, it's pretty simple to write your own message box dialog that closes after a set time. Here's a bare bones example:

#pragma once

using namespace System;
using namespace System::Timers;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;

namespace FormsScratch {
  public ref class EdMessageBox: public Form {
    System::ComponentModel::Container^ components;
    System::Windows::Forms::Label^ labelMessage;
    System::Windows::Forms::Button^ buttonCancel;
    System::Windows::Forms::Button^ buttonOK;

    // Ed added this to close the form
    // after a set number of milliseconds
    System::Timers::Timer^ _timer;
  public:
    EdMessageBox(String^ message, int milliseconds)
    {
      InitializeComponent();

      _timer = gcnew System::Timers::Timer();
      _timer->Interval = milliseconds;
      _timer->Elapsed += gcnew ElapsedEventHandler(
        this, &EdMessageBox::_timer_Tick);
    }
  protected:
    ~EdMessageBox()
    {
      if (components)
        delete components;
    }
  private:
    //
    // Visual Studio added generated code here, but Edward will paste it
    // in another code block so it doesn't distract from the meat of the form
    //

    System::Void EdMessageBox_Load(Object^ sender, EventArgs^ e)
    {
      _timer->Enabled = true;
    }

    System::Void _timer_Tick(Object^ sender, ElapsedEventArgs^ e)
    {
      // The timer ticks only once, and then
      // the dialog closes with a status that
      // can't be reached through the buttons
      _timer->Enabled = false;
      DialogResult = ::DialogResult::Abort;
    }

    System::Void buttonOK_Click(Object^ sender, EventArgs^ e)
    {
      DialogResult = ::DialogResult::OK;
    }

    System::Void buttonCancel_Click(Object^ sender, EventArgs^ e) 
    {
      DialogResult = ::DialogResult::Cancel;
    }
  };
}

Here is the designer generated code for the form:

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
      this->labelMessage = (gcnew System::Windows::Forms::Label());
      this->buttonCancel = (gcnew System::Windows::Forms::Button());
      this->buttonOK = (gcnew System::Windows::Forms::Button());
      this->SuspendLayout();
      // 
      // labelMessage
      // 
      this->labelMessage->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
        | System::Windows::Forms::AnchorStyles::Left) 
        | System::Windows::Forms::AnchorStyles::Right));
      this->labelMessage->Location = System::Drawing::Point(-1, -1);
      this->labelMessage->Name = L"labelMessage";
      this->labelMessage->Size = System::Drawing::Size(512, 150);
      this->labelMessage->TabIndex = 0;
      // 
      // buttonCancel
      // 
      this->buttonCancel->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
      this->buttonCancel->Location = System::Drawing::Point(424, 162);
      this->buttonCancel->Name = L"buttonCancel";
      this->buttonCancel->Size = System::Drawing::Size(75, 23);
      this->buttonCancel->TabIndex = 1;
      this->buttonCancel->Text = L"Cancel";
      this->buttonCancel->UseVisualStyleBackColor = true;
      this->buttonCancel->Click += gcnew System::EventHandler(this, &EdMessageBox::buttonCancel_Click);
      // 
      // buttonOK
      // 
      this->buttonOK->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
      this->buttonOK->Location = System::Drawing::Point(343, 162);
      this->buttonOK->Name = L"buttonOK";
      this->buttonOK->Size = System::Drawing::Size(75, 23);
      this->buttonOK->TabIndex = 2;
      this->buttonOK->Text = L"OK";
      this->buttonOK->UseVisualStyleBackColor = true;
      this->buttonOK->Click += gcnew System::EventHandler(this, &EdMessageBox::buttonOK_Click);
      // 
      // EdMessageBox
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
      this->ClientSize = System::Drawing::Size(511, 197);
      this->Controls->Add(this->buttonOK);
      this->Controls->Add(this->buttonCancel);
      this->Controls->Add(this->labelMessage);
      this->Name = L"EdMessageBox";
      this->Text = L"EdMessageBox";
      this->Load += gcnew System::EventHandler(this, &EdMessageBox::EdMessageBox_Load);
      this->ResumeLayout(false);

    }
#pragma endregion
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.