the Button class is using, among other, the DialogResult Property, Which I use and it works great!
But...
know I whant to use DialogResult in combination with a PictureBox, which I use in severall cases as a button, to get rit of the
overload given by the button class.
I made a Custom Component PictureBox called "CPictureBox".
But know I have to implement the dialogresult into it.
How should this be done??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public partial class CPictureBox : System.Windows.Forms.PictureBox
{
public DialogResult Dialogresult
{
get;
set;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public partial class CPictureBox : System.Windows.Forms.PictureBox
{
private DialogResult _DialogResult;
public DialogResult DialogResult
{
get
{
return _DialogResult;
}
set
{
_DialogResult = value;
}
}
}
}
It is not working 100%, the return value is allways "Cancel".
Other options are; OK, Retry, Abort etc.
So there is a glitch in the transport of the correct value.
Why don't you zip up a complete sample project and post it here? Then folks can load your project, see what you are doing wrong and repost a corrected zip file.
1) In form2, you aren't setting the dialogresult from each picture box back to the dialog result of Form2. If you had derived from a button instead of a picture box, this would be connected up automatically for you. No matter, just set them in the click handlers.
Code:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void cPictureBox1_Click(object sender, EventArgs e)
{
DialogResult = cPictureBox1.DialogResult;
Close();
}
private void cPictureBox2_Click(object sender, EventArgs e)
{
DialogResult = cPictureBox2.DialogResult;
Close();
}
}
2) In the Form1 button handler, the logic for checking the dialogresult value from Form2 is wrong.
See how 'Yes' is displayed twice in the commented out code? Also, if 'Yes' is the dialog result, the error statement will also be displayed?
Code:
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = new DialogResult();
Form2 F2 = new Form2();
dr = F2.ShowDialog();
string msg;
switch(dr)
{
case DialogResult.Yes:
msg = "You Presses the Yes Custom Picture Box";
break;
case DialogResult.No:
msg = "You Presses the No Custom Picture Box";
break;
default:
msg = String.Format("Unknown DialogResult: {0}", dr);
break;
}
MessageBox.Show(msg);
//if (dr == DialogResult.Yes)
// MessageBox.Show("You Presses the Yes Custom Picture Box");
//if (dr == DialogResult.No)
// MessageBox.Show("You Presses the Yes Custom Picture Box");
//else
// MessageBox.Show("DialogResult Error......");
}
Now I want to go one step futher.
I want to make use of the TAB key, just like 'normal' buttons.
So I need OnLeave and OnEnter.
How can I place this in my CustomPictureBox. (need to derive from control I think)
and How do I fire them.
So let say if the 'button' has focus the Yes or No text will be underlined, something like that.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.