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);
}
}
Related
Here is the form i made
When I enter an empty value in the textbox I meet exception unhandled page. I want to show an error messagebox when user enter an empty value to the textbox how can I do that ?
namespace Random_çalışması
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random rnd = new Random();
int number;
int answer;
private void button1_Click(object sender, EventArgs e)
{
number = rnd.Next(1, 101);
label1.Text = number.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
answer = Convert.ToInt32(textBox1.Text);
if(answer == number)
{
MessageBox.Show("Welcome to the page", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if(String.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Just add the empty test at the beginning of the method:
private void button2_Click(object sender, EventArgs e)
{
if ( textBox1.Text == "" )
{
MessageBox.Show(...);
return;
}
answer = Convert.ToInt32(textBox1.Text);
...
}
You can also use int.TryParse() instead of Convert to better catch conversion errors:.
How the int.TryParse actually works
You can also simply set the button disable by default instead and add this handler on the TextChanged event:
private void textBox1_TextChanged(object sender, EventArgs e)
{
button.Enable = textBox1.Text != "";
}
And also do the int.TryParse here instead too:
private void textBox1_TextChanged(object sender, EventArgs e)
{
button.Enable = textBox1.Text != "" && int.TryParse(textBox1.Text, out _);
}
Hence the button is enabled only if not empty and convertible and you have a consistent UX.
Now the button click handler is:
private void button2_Click(object sender, EventArgs e)
{
answer = Convert.ToInt32(textBox1.Text);
if ( answer == number )
MessageBox.Show("Welcome to the page", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Icorrect entrance", "You can not acces to the page", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
I have a button(named: open port) when it clicked the following code executed:
private void button1_Click(object sender, EventArgs e)
{
try
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
else
{
MessageBox.Show("Port is Open by other party!");
}
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show(ex.Message);
}
}
what I want to do is :
The button keep clicked and its name changes to (close port)
I press it and I want it to perform the following code:
serialPort1.Close();
Form1 myForm = new Form1();
this.Close();
can you help me?
If you just want a button that toggles opening and closing the port then it should be sufficient to just do the following:
private void button1_Click(object sender, EventArgs e)
{
try
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();
this.button1.Text = "Close Port";
}
else
{
serialPort1.Close();
this.button1.Text = "Open Port";
}
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show(ex.Message);
}
}
As for the extra code:
Form1 myForm = new Form1();
this.Close();
This new instance of Form1 would be disposed once it is out of scope, and closing this form means you no longer have any UI.
The closest thing you'll get to a toggle button in winform is using a ToolStripButton that you have to add to a ToolStrip.
Set the ToolStripButton.CheckOnClick to true and then code the ToolStripButton.CheckStateChangedEvent.
private void toolStripButton1_CheckStateChanged(object sender, EventArgs e)
{
// Checked means it's clicked
if (toolStripButton1.Checked)
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();
toolStripButton1.Text = "Close Port";
}
}
else
{
if (serialPort1.IsOpen())
{
serialPort1.Close();
toolStripButton1.Text = "Open Port";
}
}
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dResult;
dResult = MessageBox.Show("You have entered: " + textBox1.Text, "Message Box Info", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Information);
label1.Text = "You clicked " + dResult.ToString();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(this, KeyEventArgs.Empty);
}
}
This is my code to see if the enter key was pressed on a text box, and i want it to click the button when the enter key is pressed. I run the program, and nothing happens. Im new to C# as you can tell. Any help or comments would be helpful.
Thank you in advance.
You can use a shared method:
public void SharedMethod()
{
DialogResult dResult;
dResult = MessageBox.Show("You have entered: " + textBox1.Text, "Message Box Info", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Information);
label1.Text = "You clicked " + dResult.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
SharedMethod();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SharedMethod();
}
}
You should definitely refactor your code the way pcnThird did it, but I recreated your code in an otherwise empty WinForms app and it worked just fine, even with passing KeyEventArgs.Empty.
The way you've got it, the code should run.
I suspect your KeyDown event is not actually attached to textBox1_KeyDown.
Check your properties window at design-time to make sure it's actually attached.
Or temporarily, type the following into your form's constructor:
this.textBox1.KeyDown += new KeyEventHandler(this.textBox1_KeyDown);
I am trying to get my application to minimize to the notification area and that part is working. The problem is, when I double click it, it is not showing the window again.
This is what I'm doing, I hope it's something simple I'm doing wrong:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
CreateNotifyIcon();
}
private void CreateNotifyIcon()
{
mynotifyicon.BalloonTipIcon = ToolTipIcon.Info;
mynotifyicon.BalloonTipText = "[Balloon Text when Minimized]";
mynotifyicon.BalloonTipTitle = "[Balloon Title when Minimized]";
mynotifyicon.Icon = Resources.lightning;
mynotifyicon.Text = "[Message shown when hovering over tray icon]";
}
private void MainLoad(object sender, EventArgs e)
{
Resize += MainResize;
MouseDoubleClick += MainMouseDoubleClick;
}
private void MainResize(object sender, EventArgs e)
{
try
{
if (FormWindowState.Minimized == WindowState)
{
mynotifyicon.Visible = true;
mynotifyicon.ShowBalloonTip(3000);
ShowInTaskbar = false;
Hide();
}
else if (FormWindowState.Normal == WindowState)
{
mynotifyicon.Visible = false;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
private void MainMouseDoubleClick(object sender, MouseEventArgs e)
{
try
{
Show();
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
mynotifyicon.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
Something I forgot to mention was I put a debug stop on MainMouseDoubleClick and it's never hitting that point.
Thanks for the help!
** EDIT **
I changed the double click to have a try/catch and it's not being reached at all. Not even to the try.
You need to add the click event to the notifyicon. At the moment you are registering the handler on the form.
mynotifyicon.MouseDoubleClick += MainMouseDoubleClick;
Attach the MouseDoubleClick event at the end of CreateNotifyIcon:
mynotifyicon.MouseDoubleClick += MainMouseDoubleClick;
I would recommend renaming the event handler MainMouseDoubleClick to something more appropriate and unregistering it from the form's double click event.
I have used this and its working :
this.WindowState = FormWindowState.Normal;
this.Activate();
I've written this code to change the serialport name:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
serialPort1.PortName = (string)comboBox1.SelectedValue;
}
catch (Exception)
{
MessageBox.Show("The SerialPorts's Name Does Not Change !! ");
}
}
The result is that I get an Exception. The SerialPort's name never changes.
Could be one of many issues.
Is (string)comboBox1.SelectedValue String.Empty?
Is (string)comboBox1.SelectedValue null?
Is serialPort1 open?
It would be easier to answer the question if we had the actual exception message, instead of the custom message you defined.
To use the SelectedValue Mr. JaredPar MSFT explained ComboBox SelectedItem, SelectedValue, SelectedWhat???.
I guess, your trying get the PortName not the SelectedValue
Solution:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1) return;
if (string.IsNullOrWhiteSpace(comboBox1.Text)) return;
serialPort1.PortName = comboBox1.Text;
}
or
your trying get the SelectedValue not the PortName
Solution:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue == null) return;
if (string.IsNullOrWhiteSpace(comboBox1.Text)) return;
serialPort1.PortName = (int)comboBox1.SelectedValue ;
}
Try to check your properties of ComboBox
Where: Display Member is your PortName
Value Member is your PortId or SelectedValue
You have to put a check in to see if the SelectedIndex is -1 or SelectedValue (or SelectedItem) is null. That's the value when there is no selected item.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1)
return;
serialPort1.PortName = (string)comboBox1.SelectedValue;
}
In order to figure out what the exception message is, try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
serialPort1.PortName = (string)comboBox1.SelectedValue;
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
The problem is that you don't handle exception properly. The exception that is thrown probably tells you what is the problem, but you're not looking at it.
Change your catch statement to
catch (Exception e) { MessageBox.Show(e.Message); }
Then see what is the exception, and fix it accordingly.
The SerialPort.PortName setter can throw an array of exceptions, so you need to know which one is it.