Adding multiple expanders event programmatically wont work - c#

I've got a problem with adding Expanded event to my Expanders. I have expanders on my Window and I want to get the effect when I expand my expander all other will go down. I write functions that let me do this and it work correct. The problem is that I have 96 expanders I don't want add 96 events for Expand and 96 events for Collapse so I thought that I can add this programmatically.
look at the code:
private void InitExpanders()
{
var expanders = GetExpanders(); // List<Expander> - list of expanders
for (int i = 0; i < expanders.Count; i++)
{
if (i % 6 == 1)
{
expanders[i - 1].Expanded += new RoutedEventHandler(delegate(object sender, RoutedEventArgs args)
{
DisableBigExpanders(1); // problem is here!
});
}
}
}
this code works fine but for each expander function parameter will be 1.
Ive tried to add integer and increment it but it wont works.
private void InitExpanders()
{
var expanders = GetExpanders();
int x = 0;
for (int i = 0; i < expanders.Count; i++)
{
if (i % 6 == 1)
{
expanders[i - 1].Expanded += new RoutedEventHandler(delegate(object sender, RoutedEventArgs args)
{
DisableBigExpanders(x);
});
x++;
}
}
}
Thanks for all replies.

I suspect you are finding x in the delegate is always the highest value it reached during the loop. This is due to the way the compiler instantiates the instance of the anonymous method you've defined. It looks at what captured outer variables there are around the delegate and decides whether it requires separate instances or if it can use a single instance. In this case, you have no captured outer variables; therefore, the compiler is permitted to use the same instance of the delegate and therefore the same value of x.
To get around this issue, all you need to do is add a more granular variable just before the delegate and assign it the value of x. This will work:
private void InitExpanders()
{
var expanders = GetExpanders();
int x = 0;
for (int i = 0; i < expanders.Count; i++)
{
if (i % 6 == 1)
{
int y = x++;
expanders[i - 1].Expanded += delegate
{
DisableBigExpanders(y);
};
}
}
}
See here for more info on the theory: http://en.csharp-online.net/ECMA-334%3A_14.5.15.4_Anonymous_method_evaluation

Related

NumericUpDown ValueChanged preventDefault?

I want to create a form that allows the user to set a certain amount of points in five different fields (NumericUpDown). When that amount of points reaches 0, the user can't add any more. (I still want the user to be able to remove points, though.)
Here is my code so far:
private void calculateValue() {
decimal tempValue = CMB_num_Aim.Value + CMB_num_Reflexes.Value +
CMB_num_Positioning.Value + CMB_num_Movement.Value + CMB_num_Teamwork.Value;
controlValue = currentValue - tempValue;
MyBox.CMB_tb_cv.Text = controlValue.ToString();
}
This Method (calculateValue) is calculates how many points the user have left (controlValue).
private void CMB_num_Aim_ValueChanged(object sender, EventArgs e) {
calculateValue();
if (controlValue < 0) {
//Prevent Default here
MessageBox.Show("You are out of points!");
}
}
This method (CMB_num_Aim_ValueChanged) fires when the value of the NumericUpDown control has changed. I have one of these for each field, each doing the same thing.
The method fires as expected, but I can't prevent it from happening - the user can apply more points than they have. How can I prevent the user from applying more points?
(I thought about making a mouseUp method, but I don't know if the user will use the mouse or if he will type in the value using the keyboard.)
Seems like you want to create a point distribution system between some skills - aim, movement, teamwork etc. You can do that easily by setting Maximum value of NumericUpDown control when you enter it. Subscribe all skill updown controls to the same event handler:
private void SkillNumericUpDown_Enter(object sender, EventArgs e)
{
var skill = (NumericUpDown)sender;
var availablePoints = 42;
var maxSkillPoints = 20; // usually you cannot assign all points to one skill
var unassignedPoints = availablePoints - SkillPointsAssigned;
skill.Maximum = Math.Min(maxSkillPoints, unassignedPoints + skill.Value);
if (unassignedPoints == 0)
{
MessageBox.Show("You are out of points!");
return;
}
if (skill.Value == maxSkillPoints)
{
MessageBox.Show("Skill maximized!");
return;
}
}
private decimal SkillPointsAssigned =>
CMB_num_Aim.Value +
CMB_num_Reflexes.Value +
CMB_num_Positioning.Value +
CMB_num_Movement.Value +
CMB_num_Teamwork.Value;
Benefit - you will not be able to input illegal value neither by arrows nor manually.
Replace
if (controlValue < 0) {
By
if (controlValue <= 0) {

Continuously adding and removing to flow-layout panel win forms

I have a c# win forms application which has a flowLayoutPanel in it.
I need to update all the children in this panel every second.
here is my current code which gets called in a system timer every 1 seconds:
public void RefreshReceiversPage()
{
if (checkBox_enableReceivers.Checked)
{
var receivers = OutgoingReceiverManager.GetCopyOfActiveRecieverHolders();
for (int i = 0; i < flowLayoutPanel_receivers.Controls.Count; i++)
{
var tmp = flowLayoutPanel_receivers.Controls[i];
flowLayoutPanel_receivers.Controls[i].Dispose();
tmp.Dispose();
}
flowLayoutPanel_receivers.Controls.Clear();
foreach (var item in receivers.ToList())
{
var tmpUc = new ucReceiverItem(item);
if (flowLayoutPanel_receivers != null)
{
try
{
flowLayoutPanel_receivers.Controls.Add(tmpUc);
}
catch
{
}
}
}
receivers = null;
}
}
now this code works perfectly for about 2 minutes and then all of a sudden I start getting error creating window handle Hence the reason for my try catch in the code above.
But after this happens the pane view goes funny and I cant recover it without starting up the program again.
I have searched high and low and I cant seem to find anything on the exception being thrown?
All that I can think is that im maybe not disposing of object properly and that its running out of memory some where?
Does any one have any suggestions or solutions?
EDIT:
here is UCRecieverItem:
public partial class ucReceiverItem : UserControl
{
public ucReceiverItem(ReceiverHolder item)
{
InitializeComponent();
ConstructItem(item);
item = null;
}
private void ConstructItem(ReceiverHolder item)
{
label_name.Text = item.ReceiverDb.Name;
label_numberOfConnections.Text = item.ReceiverOutgoingConnectionManager.GetNumberOfConnections().ToString();
label_mrFilters.Text = item.ReceiverDb.MrFilters;
label_multipleConnections.Text = item.ReceiverDb.MultipleConnections.ToString();
//
int count = item.GetActiveBufferCount();
int size = item.GetActiveBufferSize();
//
label_bufferCount.Text = count + #" / " + size;
progressBar_buffer.Maximum = size;
progressBar_buffer.Minimum = 0;
progressBar_buffer.Value = count;
}
}
This code is a problem:
for (int i = 0; i < flowLayoutPanel_receivers.Controls.Count; i++)
{
var tmp = flowLayoutPanel_receivers.Controls[i];
flowLayoutPanel_receivers.Controls[i].Dispose();
tmp.Dispose();
}
flowLayoutPanel_receivers.Controls.Clear();
It is only disposing half of the controls in the container. The other half get removed by the Controls.Clear(); call, but those controls do not get disposed — so they still exist and are using up memory.
Doing this every second compounds the problem: that's potentially a lot of controls.
The immediate work-around would be to properly dispose of the controls:
while (flowLayoutPanel_receivers.Controls.Count > 0) {
flowLayoutPanel_receivers.Controls[0].Dispose();
}
After that, I would question the need to do this every second — seems like a harsh environment for a user to work in.

Model as dependency property used within different threads

I do have the following szenario. I created a WPF C# project that follows the MVVM pattern. Furthermore I developed a user control that is some kind of a image drawer. Now I create a Model within the viewmodel and share it with the usercontrol as dependency property. This is done like this:
User control:
public DXModel Model
{
get { return (DXModel)GetValue(ModelProperty); }
set { SetValue(ModelProperty, value); }
}
public static readonly DependencyProperty ModelProperty =
DependencyProperty.Register("Model", typeof(DXModel), typeof(DXControl),
new PropertyMetadata(null, OnChanged));
static void OnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as DXControl).OnChanged();
}
void OnChanged()
{
if (Model != null)
Model.PropertyChanged += new PropertyChangedEventHandler(Model_Changed); // comes from the ui thread...comes only when init model...ok
textTitle.Text = Model.Title;
internalModel = (DXModel)Model;
}
void Model_Changed(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (!receiveTimer.Enabled)
{
receiveTimer.Interval = receiveDelay;
receiveTimer.Start();
refreshTimer.Start();
}
}
So I also get the event when the Model was changed in the viewmodel and I then perform the drawing. Basically I have a list of points which I completely overwrite within the viewmodel several times per second:
ViewModel:
dxModel1.Series[0].Points = list1;
In the User control the points are redrawn also several times per second. To do so, I have to iterate through the points first:
User control:
int length = internalModel.Series[i].Points.Count-1;
float lastX = internalModel.Series[i].Points[0].X;
for (int j = 0; j <= length; j++)
{
float x = ((float)targetImage.Width - 0) / (internalModel.Axes[0].Max - internalModel.Axes[0].Min) * (internalModel.Series[i].Points[j].X- lastX - internalModel.Axes[0].Min);
float y = (0 - (float)targetImage.Height) / (internalModel.Axes[1].Max - internalModel.Axes[1].Min) * (internalModel.Series[i].Points[j].Y - internalModel.Axes[1].Min) + (float)targetImage.Height;
tmpPoints.Add(new SharpDX.Vector2(x, y));
}
What is important, the Model is changed in a worker thread within the viewmodel. The user control is living in the ui thread. The problem I have is that this iteration through the points gives me an exception from time to time. I guess, this is because The points are overwritten in the worker thread at the same time it iterates through the points.
I tried to lock the internalModel in the usercontrol and the dxModel1 within the viewmodel but that did not help me.
Am I right with my approach why it fails and if so, how could I overcome this issue?
if you are working with .net4.5, I think by using async-await in your method should take care of this issue. some link I came across with: http://www.i-programmer.info/programming/c/1514-async-await-and-the-ui-problem.html
I'm not sure that I fully understand your problem as you didn't tell us the Exception details, but if you just want to ensure that your code runs on the UI thread, you can do something like this:
RunOnUiThread((Action)delegate()
{
int length = internalModel.Series[i].Points.Count-1;
float lastX = internalModel.Series[i].Points[0].X;
for (int j = 0; j <= length; j++)
{
float x = ((float)targetImage.Width - 0) / (internalModel.Axes[0].Max - internalModel.Axes[0].Min) * (internalModel.Series[i].Points[j].X- lastX - internalModel.Axes[0].Min);
float y = (0 - (float)targetImage.Height) / (internalModel.Axes[1].Max - internalModel.Axes[1].Min) * (internalModel.Series[i].Points[j].Y - internalModel.Axes[1].Min) + (float)targetImage.Height;
tmpPoints.Add(new SharpDX.Vector2(x, y));
}
});
...
public object RunOnUiThread(Delegate method)
{
return Dispatcher.Invoke(DispatcherPriority.Normal, method);
}

How to refresh canvas

I am trying to create a visual representation of any sorting algorithm where the data is represented in an int[] array. An example of bubble sort on wikipedia:
My sorting algorithms all raise an event ItemsSwapped when two items in the int[] array are swapped. I am trying to display the data after every event on canvas, this is my code:
// Handler for ItemsSwapped event.
private void Render(object sender, ItemsSwapEventArgs e)
{
canvas.Children.Clear();
int numberOfElements = e.Data.Length;
for (int x = 0; x < numberOfElements; x++)
{
RenderValue(x, e.Data[x]);
}
// Here I should somehow refresh canvas.
}
private void RenderValue(int x, int y)
{
var value = new Ellipse
{
Width = 5,
Height = 5,
Stroke = Brushes.Black,
StrokeThickness = 2,
};
Canvas.SetTop(value, x);
Canvas.SetLeft(value, y);
canvas.Children.Add(value);
}
The problem is, that the canvas doesn't refresh itself, it just displays the final solution after some time. How can I refresh it after every raised event?
Edit - I tried with UpdateLayout, InvalidateMeasure and Dispatcher object, but neither worked.
Maybe you start your sort algorithm on the UI thread, so it won't update until finished. Try sorting in another thread and update the Canvas children using the Dispatcher, by calling Invoke or BeginInvoke.
If your ItemsSwapped handler is called from a separate thread, it may look like this:
private void Render(object sender, ItemsSwapEventArgs e)
{
Dispatcher.Invoke((Action)(() =>
{
canvas.Children.Clear();
int numberOfElements = e.Data.Length;
for (int x = 0; x < numberOfElements; x++)
{
RenderValue(x, e.Data[x]);
}
}));
}
Are you using threads? You must do your work in a separate thread from the main UI. Here is a link to get you started: How to update the GUI from another thread in C#?

How do I check the state of a thread

I have winform application where i am trying to play multiple videos and i am creating threading for that. My code is :
public String[,] vpath = new String[3, 7];
public Video[,] video = new Video[3, 7];
public static Thread[,] th = new Thread[3, 7];
public void playclick(object sender, EventArgs e)
{
int i, j;
for (j = 0; j <= 7 - 1; j++)
{
for (i = 0; i <= 3 - 1; i++)
{
if (btnp[i, j].Capture)
{
//play();
th[i, j] = new Thread(new ThreadStart(play));
th[i, j].IsBackground = true;
th[i, j].Start();
}
}
}
}
public void play()
{
int i, j;
for (j = 0; j <= 7 - 1; j++)
{
for (i = 0; i <= 3 - 1; i++)
{
if (th[i, j].ThreadState == ThreadState.Running) // Having problem here
{
if (video[i, j].State != StateFlags.Running)
{
video[i, j].Play();
}
}
}
}
}
So with out that if statement it will run all the videos on single button press. But i want to run the particular video which the thread is in ..
pls help me guys
ThreadState is a bitmask-type property (enum has the [Flags] property, that's always the hint), so you don't check it directly using ==, you only need to check the relevant bit:
if ((t.ThreadState & ThreadState.Running) == ThreadState.Running) { ...
Read here about the meanings of the ThreadState values. From reading that and possibly the whole article, or whole book (highly recommended!) you'll also most likely notice that yours is probably not the ideal approach.
Not knowing your exact endgame though, it;s hard to suggest an exact one.
As to why you are getting an exception, HaemEternal nailed that in his comment. You are only initializing one thread at a time, yet you are checking all of them. A null thread object does not have a ThreadState value.
May I suggest though, that you change your design altogether;
There is no need to constantly check which thread was activated. You can change the signature of the Play() method to accept an Object, and you can pass the correct video to the method using that Object.
public void playclick(object sender, EventArgs e)
{
int i, j;
for (j = 0; j <= 7 - 1; j++)
{
for (i = 0; i <= 3 - 1; i++)
{
if (btnp[i, j].Capture)
{
//play();
th[i, j] = new Thread(new ParameterizedThreadStart(play));
th[i, j].IsBackground = true;
th[i, j].Start(video[i,j]);
}
}
}
}
public void play(object video)
{
Video vid = video as Video;
if (vid.State != StateFlags.Running)
{
vid.Play();
}
}
An even better approach is to encapsulate these three elements in a single object that contains a Video object, a Thread object, and a path string.
If you own the Video class, you might even want to make the Thread and the string values fields of that class.
You might even want to create a field on your buttons of type of this new object, so each button will be associated with a button.
This is much more typical of object oriented design. There is no reason you should maintain four separate identically sized arrays, each of different type.
The answer by #tar gives some hints but the code is wrong (as commented by #Sampath).
This come from the fact that ThreadState is implemented in a questionable strange way:
Normally a bitmask state is implemented using for example bit 1 for state on and the
same bit for the opposite off. This is not the case, in fact, for example, the Running
state has a 0 value, whereas the 1 value is taken by StopRequested.
So it is not wise to do a bit check.
A first approach would be to check for state with an or statement:
while (t.ThreadState == ThreadState.Running ||
t.ThreadState == ThreadState.Background)
Application.DoEvents();
t.Join();
Keep in mind that if you start a process in background you will have the
ThreadState.Background enum value returned and not ThreadState.Running,
this is why I have put both.
The better and simpler approach is:
while (t.IsAlive)
Application.DoEvents();
t.Join();
if (th.ThreadState.Equals(ThreadState.Unstarted))
th.Start();

Categories

Resources