blah blah blah is here! blah blah » Close

up0down
link

I was trying to create a custom messageBox, I'm able to get the code working to display the icon, but I'm not able to get the Form displayed near the Right side of the Clock in the taskbar.
How should I show the form (form name: <span style="color=blue;">messageBoxForm</span>)???
Here's the code:<span style="color=blue;">
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class mesasgBoxForm : Form
{
class ControlContainer : IContainer
{
ComponentCollection _components;
public ControlContainer()
{
_components = new ComponentCollection(new IComponent[] { });
}
public void Add(IComponent component)
{ }
public void Add(IComponent component, string Name)
{ }
public void Remove(IComponent component)
{ }
public ComponentCollection Components
{
get { return _components; }
}
public void Dispose()
{
_components = null;
}
}
class SomeClass
{
ControlContainer container = new ControlContainer();
private System.Windows.Forms.NotifyIcon notifyIcon1;
public SomeClass()
{
this.notifyIcon1 = new NotifyIcon(container);
}
}
public mesasgBoxForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}</span>
I'm having Label1 & Label2 in <span style="color=blue;">messageBoxForm</span>, but the <b>main</b> form is <span style="color=maroon;">Form1</span>.
So.... I was also curious about the thought if I can send the Contents (error msgs) of Label1 & Label2 from <b>Form1</b> to <span style="color=blue;">messageBoxForm</span>.
I'm missing some link here..... :(

last answered 8 months ago

1 answers

up0down
link

hey, if you mean a custom messagebox that acts somehow like the normal messagebox, why don't you add a new form removing the ControlBox from the form, and to activate this messagebox make a static method in the custom messagebox form that have the title and the text of the messagebox (stored in a label ofcourse) and disabling the enabled forms by looping through the enabled forms and storing them in a temporary list and disabling them. Also when the user close or clicking the ok button, the forms in the list are enabled again.Beside that in the static function make a loop to check the response in the form if its ok or canceled by looping around and Application.DoEvent(); wiht a lil Sleep of the thread.
In MessageBoxForm:
bool Response=false;
string result;
List<Form> forms=new List<Form>();
public void DisableForms()
{
foreach(Form f in Application.OpenForms)
{
if(f.Enabled&&f!=this)
{
forms.Add(f);
f.Enabled=false;
}
}
}
public string Result()
{
return result;
}
protected void btnOK_Click(object sender, EventArgs e)
{
result="OK";
Response=true;
}
protected void MessageBoxForm_FormClosing(object sender, EventArgs e)
{
result="Cancel";
Response=true;
}
public void EnableForms()
{
foreach(Form f in forms)
{
f.Enabled=true;
}
}
public static string Show(string title,string text)
{
MessageBoxForm msg=new MessageBoxForm();
msg.Show();
msg.DisableForms();
while(!msg.Response)
{
Application.DoEvent();
System.Threading.Thread.Sleep(30);
}
msg.EnableForms();
return msg.Result();
}

up0down
link

there is also a missing function:
public void SetMessage(string title,string text)
{
this.Text=title;
this.label1.Text=text;
}
in the Show static method:
msg.Show();
msg.SetMessage(title,text);
msg.DisableForms();

up0down
link

Thx muster for the help, But I'm not able to understand the use/need of that "Ok" button there.
I've added the Form Click function to close this popup (Form).
private void messageBoxForm_Click(object sender, EventArgs e)
{
this.Close();
}
I'm planning to use this messageBox as an alternative to defalt messageBox function. Which means....
I'm having a form... <span style="color=blue;">Form1</span>, and now I'm having this messageBoxForm. I was thinking of passing on the strings (title, msgtext) from Form1; into two labels.. label1 & label2 in messageBoxForm.
So I think I need a way to send strings (title, msgtext) out from Form1, or call that static string show from messageBoxForm.
??????????

up0down
link

This means that I've to use <b>SetMessage</b> method in <span style="color=red;">Form1</span>, and call it's string values in <b>static string show</b> method in <span style="color=red;">messageBoxForm</span>.
.

up0down
link

hello, the setmessage and the Show method are methods that can be changed to several things according to the programmer. If you want to make an OK messagebox without a yes/no or yes/no/cancel then all you have to do in the button_OK eventhandler is set the result to "OK" which is not important anyway and set the response to true (Response=true; ) so the user gets the result. Also i forgot to close the form after msg.EnableForms(); so you have to add
try
{
msg.Close();
}
catch(Exception)
{
}

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback