So a sequel to my last post...
I have PieChart with Legend and i want to be able to right click on Legend title and copy the value.
So this is the Legend Mouse Right click Event:
private void pieLegend_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var hoveredItem = this.pieLegend.Items.FirstOrDefault(x => x.IsHovered);
Clipboard.SetText(hoveredItem.Title);
}
As you can see i am copy the value the the Clipboard but i want to open simple Copy menu so i create ContextMenu and i have the ContextMenu Click Event:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
}
Not inside this Click Event i want to get the Clipboard but because pieLegend_MouseRightButtonDown event is fired before this Click Event i want to find a way to get this Clipboard value.
So i was thinking about create a Static variable that the first Event will set the Clipboard value and from the Click Event just get this value but my question is if this is the best way/appropriate way to do that.
Well, there's nothing wrong with doing both.
I think you are over thinking this, you really need to consider your requirements.
In this particular scenario, I would not suggest using the clipboard to transfer data from the two events. Why? Well, because the user can easily just copy something else onto their clipboard which may be completely unrelated to your application, therefore it could be erroneous.
So, I expect defining a private or static variable (This really depends on the scope, you don't need to define a static variable if you don't have to) is the correct way to go.
private string _HoveredItemTitle;
private void pieLegend_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var hoveredItem = this.pieLegend.Items.FirstOrDefault(x => x.IsHovered);
Clipboard.SetText(hoveredItem.Title);
_HoveredItemTitle = hoveredItem.Title;
}
And in your other method, it'd be a good idea to check if there is something in this variable, otherwise it may not be a good idea to go a head with the execution.
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(_HoveredItemTitle))
{
//TODO: Something useful
return;
}
//TODO
}
I think the main idea I'm trying to get across here is that you should ensure that there is nothing (or as little as possible) that the user can do to cause errors in your application. In this scenario, copying the text to the clipboard is nice, but it's more of a feature than a requirement. The application should not have to rely on the clipboard, simply because there might be complete nonsense on the clipboard, instead of the data that you are actually expecting.
Related
How do I change the following class/method to accept global variables and also return a value?
private void radioButton_CheckedChanged(object sender, EventArgs e)
Ideally, I want it to return either a string or a text. But that doesn't happen just by changing void to int or string and using return command inside it. Making the same public also doesn't seem to work.
Any inputs or suggestions, please?
---------------------EDIT-ADDED-LATER---------------------
What I am trying to do was for this - C# - How do I separately return int values from each radio GroupBox?
I am afraid, what you’re saying is not possible to be achieved.
This is an event handler and the event is raised internally when the checked state of your radio button is changed.
The signature and return method is decided my Microsoft obviously and you cannot change the arguments of return type of event handlers.
Also returning a data type to a radio button doesn’t sound too good, and you never need that. I think your requirement might be to change some properties or controls that is existing in the UI. That could be easily done from within the event handler and doesn’t require you to modify them.
Hence there is no need for changing their signature. If you could tell more about your requirement, we could suggest a solution based on that.
Based on another thread, where some suggestion was made, I found that I am able to get this via the following way.
int value = 0;
protected void radioButton_CheckedChanged(object sender, EventArgs e)
if (radioButton.Checked)
{
value = 100;
}
Can somebody review and tell whether any mistakes are there in this? It does what I want, but at what cost?
This is about ComboBox used in Winforms. I need to stop the selected item being heighlighted. I know I can get it done if I set the style of the combobox to ComboBoxStyle.DropDownList.
But I'm looking for a solution where I don't have to use that. Instead, at the moment what I have done is using ComboBoxStyle.DropDown.
I don't have any other option, because if I set it to DropDown, I have to deal with some other issue in my code. It's due to something else which I cannot avoid.
Can someone suggest an alternative pls ?
use the following code in your form's Paint event.
private void myForm_Paint(object sender, PaintEventArgs e)
{
comboBox1.SelectionLength = 0;
}
or pass focus to another control in your combo box selected index changed event:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Focus();
}
I have made a textbox and I want the user to type in a string of numbers and hit enter. I have setup the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
String UserBarcode;
Focus();
UserBarcode = Console.ReadLine();
MessageBox.Show(UserBarcode);
}
When I enter any key into the textbox, I get a message box with nothing in it. I want to have the program wait til it hears the enter key then display the contents of the textbox.
The Textbox.TextChanged event fires as soon as the text in the textbox is changed at all. If you want a message box with the full string, you probably want to consider using the Textbox.LostFocus event or a button's Click event.
So you could have something like (I'm taking a stab at this here, as I've used VB rather than C#)
private void textBox1_LostFocus(object sender, EventArgs e)
{
MessageBox.Show(sender.Text)
}
If you're using a button, the above function should work, but you'll want to substitute textBox1.Text for sender.Text.
Take a look at Focus and Validation Events
There are several events that you can handle, depending on your goals and how your application is designed. If you want to perform validation and/or are using data binding, you may want to go with handling the validating/validated events. By default data bindings update a bound property after OnValidating. If you use LostFocus and read the value from a bound object, instead of your control, you will get inconsistent results.
I was able to figure it out finally. For some reason when I manually entered the code I kept getting multiple random errors. I started a new Visual C # Windows Forms Application, Made a textbox, chose the keydown property and double clicked on it to have the program inject the code for the keydown function and then I filled in the if statement pointing to the enter key. The final code looks like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(textBox1.Text);
}
}
I am very new to WPF so forgive me if the question doesn't make sense. Is there an event that is fired before data context change? I want to commit the pending data changes before the data context is switched away.
There is no DataContextChanging event, but the DataContextChanged event provides the old value of the DataContext:
private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
object oldDataContext = e.OldValue;
...
}
There is no such event, if you want to make sure data is saved or that the user can choose to abort edits you should look into navigational architectures where screens are changed in a managed way.
I've inherited some code and wanted to run this modification by you all, my concern is memory management.
Let us say I have a "base" Form with a bunch of buttons that open "dialog" forms. What is the recommended pattern for opening the dialog forms? Currently we display the "dialog" form like so (in the "base" Form code, upon button click):
ChangePasswordForm frm = new ChangePasswordForm();
frm.ShowDialog();
Then close it like so (in the "dialog" form code):
private void bCancel_Click(object sender, EventArgs e)
{
this.Close();
//this.Dispose(); <-- this is what I am considering adding.
}
My rationale for adding Dispose is that I am worried if this form is displayed and closed many times that each time a new instance of the form is created and its resources are never really released -- is this correct? Also, if the form has the "close" X in the top right, should I put a Dispose() call in the FormClosed event as well?
Thanks in advance.
I would use a using statement:
using (var frm = new ChangePasswordForm()) {
frm.ShowDialog();
}
Combine this with a DialogResult:
private void bCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
Setting the DialogResult, will close the Dialog, and the caller/owner has some feedback.
And you don't have to worry about Close or Dispose.
According to MSDN you need to dispose under two conditions:
The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.
MSDN Form.Close
Declaring the form in a using statement would be the appropriate way to handle this.
using (ChangePasswordForm frm = new ChangePasswordForm())
{
frm.ShowDialog();
}