Hello. To Whome it may concern.
My following codes in c#, dose not work. Indeed method ShowBox's overloads in MyMessageBox Class do not return any value. can you help me to correct and why the reason is?
Best Regards
M. nouri
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MN_Customized_MessageBox_14020230
{
public partial class MyMessageBox : Form
{
static MyMessageBox newMessageBox;
//public static string Button_id;
public static DialogResult dialogResult;
public static DialogResult ShowBox(string txtMessage)
{
newMessageBox = new MyMessageBox();
newMessageBox.lblMessage.Text = txtMessage;
newMessageBox.ShowDialog();
return dialogResult;
}
public static DialogResult ShowBox(string txtMessage, string txtTitle)
{
newMessageBox = new MyMessageBox();
newMessageBox.lblMessage.Text = txtMessage;
newMessageBox.Text = txtTitle;
newMessageBox.ShowDialog();
return dialogResult;
}
private void btnOk_Click(object sender, EventArgs e)
{
//Button_id = "1";
dialogResult = DialogResult.OK;
newMessageBox.Dispose();
}
private void btnCancel_Click(object sender, EventArgs e)
{
//Button_id = "2";
dialogResult = DialogResult.Cancel;
newMessageBox.Dispose();
}
public MyMessageBox()
{
InitializeComponent();
}
}
}
---------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MN_Customized_MessageBox_14020230
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyMessageBox.ShowBox("Do you want to exit?");
}
private void button2_Click(object sender, EventArgs e)
{
MyMessageBox.ShowBox("Do you want to exit?", "Exit");
}
private void button3_Click(object sender, EventArgs e)
{
//string btnClicked = MyMessageBox.ShowBox("Do you want to exit");
DialogResult d1 = DialogResult.Cancel;
d1 = MyMessageBox.ShowBox("Do you want to exit?");
if (d1 == DialogResult.OK)
{
//User clicked OK button. Do some processing
this.Close();
}
else
{
//User clicked Cancel button. Do some processing.
this.Close();
}
}
}
}
Tuhin PaulPosted May 21, 2023, 4:45 PM
The ShowBox() overloads in the MyMessageBox class are not returning any value is because you are not assigning the return value to a variable. In the button3_Click() event handler, you are assigning the return value of the ShowBox() method to the d1 variable. You can then use the d1 variable to determine which button was clicked.
Mohammad NouriPosted May 21, 2023, 7:05 AM
Hello Dear Mr. Mohamed Azarudeen
hope you have joyful days.
Thank a lot for your kindness to debug the code. But unfortunately does not wrork yet. the ShowBox method's overloads do not return any value.. Have any another codes for customized message box or Input Box in C# that work well? If you have, can you do a favor and send to me?
Best Regards
M Nouri
Mohamed Azarudeen ZPosted May 20, 2023, 3:27 PM
Hi Nouri,
you haven't properly initialized the
newMessageBoxvariable in yourMyMessageBoxclass, you should do it in a static contructorI have made a few changes in the code hope it helps.
aso kindly Make sure the
MyMessageBoxclass and theForm1class are in the same namespace.Hope this helps