Two radio button in may program.
names HEX and ASCII
When user checked radio button, text is chanege
void rdo_HEX_CheckedChanged(object sender, EventArgs e)
{
if (rdo_HEX.Checked)
{
try
{
textbox1.Text = AsciiToHex(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
try
{
textbox1.Text = HexToAscii(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
But I don't want occur checkedChange evnet When an error occurs...
if error occurs when checkedChange, just radio button check is change and text is retained.
For example,
First text is 'ABCD' and ASCII is checked and it convert to '41424344' when HEX radio button is checked.
And '4142434' convert to Ascii, error ouccur, so text is '4142434' but ASCII radio button is checked..
So user check HEX radio button '4142434' convert to '34313432343334'
I dont want this...I want not change checked radio button when error occur.
How can I do?
Just set the one checked after disabling the CheckChanged
void rdo_HEX_CheckedChanged(object sender, EventArgs e)
{
if (rdo_HEX.Checked)
{
try
{
textbox1.Text = AsciiToHex(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
rdo_HEX.CheckedChanged -= rdo_HEX_CheckedChanged;
rdo_HEX.Checked = false;
rdo_HEX.CheckedChanged += rdo_HEX_CheckedChanged;
}
}
else
{
try
{
textbox1.Text = HexToAscii(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
rdo_HEX.CheckedChanged -= rdo_HEX_CheckedChanged;
rdo_HEX.Checked = true;
rdo_HEX.CheckedChanged += rdo_HEX_CheckedChanged;
}
}
}
if im understanding you correctly you dont want to keep the radio-button checked in case an error is thrown. Im not seeing the implementation on both buttons here, im just seeing one of them. So you will have to replicate this for your ascii radio button.
void rdo_HEX_CheckedChanged(object sender, EventArgs e)
{
if (rdo_HEX.Checked)
{
try
{
textbox1.Text = AsciiToHex(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
rdo_HEX.Checked = false;
}
}
else
{
try
{
textbox1.Text = HexToAscii(textbox1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Related
I have an UserControl CambioContraseña with two textBox of other customized UserControl called txtAlfanumerico. This UserContol is very simple but I want to add an ErrorProvider to check the fields are not empty. This is a screen capture of UserControl:
And this is an code:
public bool FaltaCampos() {
bool falta = false;
foreach(txtAlfanumerico txt in Controls.OfType < txtAlfanumerico > ()) {
if (txt.Text == "") {
errorProviderFalta.SetError(txt, "Falta " + txt.Tag.ToString());
falta = true;
} else {
errorProviderFalta.SetError(txt, "");
}
}
return falta;
}
And the code in where I use this UserControl:
private void buttonConfirmar_Click(object sender, EventArgs e) {
try {
if (!cambioContraseña1.FaltaCampos()) {
string actual = cambioContraseña1.TextBoxContraseñaActual();
string nueva = cambioContraseña1.TextBoxNuevaContraseña();
persona.CambiarContraseña(actual, nueva);
}
} catch (Exception ex) {
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
But my problem is that ErrorProvider do not work in the Form that I use, icons do not appear directly.
I did a breakpoint into FaltaCampos and these are results:
I can to resolve my problem I think that I had not compiled the UserControl when I did the changes, so the ErrorProvider didn't appear.
I am trying to make a text box in a WPF C# application populate a text box from a variable gathered from an external database using WCF and having little luck. Currently the text box states ScoreBoardClientTest.FeedServiceAgent instead of the value of agentsavailable. I was able to make this exact same code work in a console application when using this line of code inside of OnMessageReceived
console.writeline(e.cmsdata.skill.agentsavailable.tostring());
so I assumed I could do something similar here.
any help understanding where I'm going wrong would be great.
DisplayNumber is the name of the textbox.
public void TextBlock_Loaded(object sender, EventArgs e)
{
using (var data = new FeedServiceAgent())
{
data.MessageReceived += OnMessageReceived;
data.Subscribe("92", 3);
DisplayNumber.Text = data.ToString();
}
}
public static void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
try
{
if (e == null)
return;
if (e.CmsData == null)
{
e.CmsData.Skill.AgentsAvailable.ToString();
}
// if (!String.IsNullOrEmpty(e.Message))
// Console.WriteLine(e.Message);
}
catch (Exception ex)
{
// logger.Error(" Exception " + ex);
// throw ex;
}
}
Edit
Changed:
DisplayNumber.Text =e.CmsData.Skill.AgentsAvailable.ToString();
to:
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { DisplayNumber.Text = e.CmsData.Skill.AgentsAvailable.ToString() ; }
This will handle multithreaded calls. You might have to add a using System.Threading statement for the DispatcherPriority enum
EndEdit
It is still unclear how to get from the data of Type FeedServiceAgent to a Skill.AgentsAvailable property in your Loaded event handler. We need more information on how to make that navigation. Is the assignment even necessary in the Loaded handler? I've marked the location in the code below.
I've also made what appears to be the necessary changes to the message handler method.
public void TextBlock_Loaded(object sender, EventArgs e)
{
using (var data = new FeedServiceAgent())
{
data.MessageReceived += OnMessageReceived;
data.Subscribe("92", 3);
//DisplayNumber.Text = data.ToString();
//Is this assignment even necessary?
DisplayNumber.Text = /*Still unclear what goes here because we don't know what how to get from `data` to `Skill`*/
}
}
public static void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
try
{
if (e == null)
return;
if (e.CmsData == null)
{
//e.CmsData.Skill.AgentsAvailable.ToString();
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { DisplayNumber.Text = e.CmsData.Skill.AgentsAvailable.ToString() ; }));
}
// if (!String.IsNullOrEmpty(e.Message))
// Console.WriteLine(e.Message);
}
catch (Exception ex)
{
// logger.Error(" Exception " + ex);
// throw ex;
}
}
I am trying to hide default datagridview error dialog.
I put in the code this event handler:
this.dataGridView2.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(dataGridView2_DataError);
private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//empty so it doesn't show anything
}
But still when i try this and leave datagridview cell empty ( delete everything from it), it show me dialog box with error.
Screenshot of error:
Try to Handle and Cancel the event:
private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
}
Also, subscribe to the event in InitializeComponent()
private void InitializeComponent()
{
//...
this.dataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView2_DataError);
}
try to use this code to handle the event:
private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
}
Try the next code , it's work!
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
try
{
//To handle 'ConstraintException' default error dialog (for example, unique value)
if ((e.Exception) is System.Data.ConstraintException)
{
// ErrorText glyphs show
dataGridView1.Rows[e.RowIndex].ErrorText = "must be unique value";
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "must be unique value";
//...or MessageBox show
MessageBox.Show(e.Exception.Message, "Error ConstraintException",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//Suppress a ConstraintException
e.ThrowException = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR: dataGridView1_DataError",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I am saving my bound datagridview and on the event, I'm am saving the record with this method:
void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
Entity.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error while saving. Check the current row for errors.{0}{0}Exception:{0}{0}{1}", Environment.NewLine, ex.Message));
// I'd like to push it back to the last cell edited here
}
}
If an exception occurred, I'd like to push the users focus back to the last cell edited. Is this possible?
Is there a way to do this?
string tbname;
void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
Entity.SaveChanges();
tbname = Entity.Accessible.Name; //save the name of the last edit textbox
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error while saving. Check the current row for errors.{0}{0}Exception:{0}{0}{1}", Environment.NewLine, ex.Message));
tbname.focus(); //will return the focus to that textbox last edited
}
}
This question already has answers here:
How can I make something that catches all 'unhandled' exceptions in a WinForms application?
(4 answers)
Closed 9 years ago.
I want a general catch all error that works for all functions. Right now I have a try and catch for each individual button click for specific events. I need a catch all error for my whole program which includes all button clicks that can catch any individual exception I might have missed. Can someone please point me in the right direction
My current code:
private void Show_btn_Click(object sender, EventArgs e)
{
try
{
//do something
}
catch (Exception error)
{
outputLOG.Append(error.ToString());
{
}
private void Submit_btn_Click(object sender, EventArgs e)
{
try
{
//do something
}
catch (Exception error)
{
outputLOG.Append(error.ToString());
}
}
Goal: I want to catch ALL buttons just in case I missed individual exceptions
EDIT: I am using winforms
Add these lines before entering the first form of your application (usually in the main method in program.cs)
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
......
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
string msg = e.Exception.Message;
if (e.Exception.InnerException != null)
{
msg = msg + "\r\nPrevious error:" + e.Exception.InnerException.Message;
}
msg = msg + "\r\n\r\nDo you wish to continue with the application?";
DialogResult dr = MessageBox.Show(msg, "Exception", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.No) Application.Exit();
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
if (ex != null)
{
string msg = ex.Message;
if (ex.InnerException != null)
{
msg = msg + "\r\nPrevious error :" + ex.InnerException.Message;
}
msg = msg + "\r\n\r\nIt is not possible to continue. Contact support service!";
MessageBox.Show(msg, "Fatal Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}