Resume Count in IsolatedStorageSettings - c#

When I press the button count the number perfectly, but when you exit the application and return to count starts counting again and not when the number that was saved in IsolatedStorageSettings!!
How can I make it when the counting of the number that was saved in IsolatedStorageSettins?
(I use Windows phone 8.1 silverlight)
IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;
int Points;
// Constructor
public MainPage()
{
InitializeComponent();
this.Loaded += Page2_Loaded;
}
private void Page2_Loaded(object sender, RoutedEventArgs e)
{
if (setting.Contains("save"))
{
PointsText.Text = setting["save"].ToString();
}
}
private void Counts_Click(object sender, RoutedEventArgs e)
{
Points = Points + 1;
setting["save"] = Points;
PointsText.Text = setting["save"].ToString();
}
}

According to your code Points will always initialize to 0 when the page loads and when you click count it will increment from 0. You need to load the count from appsettings and put it into Points
private void Page2_Loaded(object sender, RoutedEventArgs e)
{
if (setting.Contains("save"))
{
//Initialize Points with the value from settings
Points = int.Parse(setting["save"].ToString());
PointsText.Text = Points.ToString();
}
}

Related

NumericupDown comparison Visual Studio c#

I have to make a quantile configuration and I have to set a condition so that the second value is higher than the first, the third value is higher than the second and so on. I am using numericupdowns and the value is set by the user. I tried to implement this code but it always shows a message box error even if the values are correct. This is my code so far (using Visual studio and C#):
decimal min = 1;
//when the first numeric is changed:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = min;
min = numericUpDown1.Value;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown2.Minimum = min;
min = numericUpDown2.Value;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown3.Minimum = min;
min = numericUpDown3.Value;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown4.Minimum = min;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value )
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
You need to keep all the NumericUpDown value inside a List or an array where you can manipulate them like a single entity trhough a loop.
Of course changing the minimum value of one of the numeric elements should be linked to a check for the current value inside that control because you can't have a current value lesser than the new minimum value.
So the first thing to do is to create a global variable inside your form class where you keep the references to all the numeric controls that you want to synchronize
public class Form1
{
private List<NumericUpDown> numbers = new List<NumericUpDown>();
public Form1 : Form
{
InitializeComponent();
numbers.AddRange(new [] {n1, n2,n3,n4,n5});
}
......
}
Now you can write a method like this one that adjust the minimum on all the numeric included in the list
private void UpdateMinimum()
{
for (int x = 0; x < numbers.Count-1; x++)
{
if(numbers[x].Value > numbers[x+1].Value)
numbers[x+1].Value = numbers[x].Value;
numbers[x+1].Minimum = numbers[x].Value;
}
}
finally you have all your NumericUpDown event ValueChanged call the same method
void numerics_ValueChanged(object sender, EventArgs e)
{
UpdateMinimum();
}
If you want to set a condition so that the second value is higher than the first, the third value is higher than the second and so on, you can refer to the following code:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = 1;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
numericUpDown2.Minimum = numericUpDown1.Value + 1;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
numericUpDown3.Minimum = numericUpDown2.Value + 1;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
numericUpDown4.Minimum = numericUpDown3.Value + 1;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value)
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
Here is the test result:

c# real time update a chart

Here I have a chart (graph1) that normally should add a random point every 1second. but it doesn't... I tried to find out what the problem is but here I don't have anymore ideas...
The timer is started, label1 change every seconds but the chart doesn't change... with button one when I click it adds a new point.
what did I miss? please help... thanks a lot.
namespace Test_Chart1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
graph1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
graph1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
graph1.ChartAreas[0].AxisX.ScaleView.Size = 40;
System.Timers.Timer _Timer1s = new System.Timers.Timer(1000); //object
_Timer1s.Elapsed += _Timer1sElapsed; //event in object
_Timer1s.Start(); //start counting
}
private void _Timer1sElapsed(object sender, EventArgs e)//Timer each 100ms
{
if (label1.BackColor == Color.Red)
{
label1.BackColor = Color.Blue;
PutValueInGraph1();
}
else label1.BackColor = Color.Red;
}
private void button1_Click(object sender, EventArgs e)
{
PutValueInGraph1();
}
private void PutValueInGraph1()
{
graph1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
graph1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
graph1.ChartAreas[0].AxisX.ScaleView.Size = 100;
Random Rand_Value = new Random();
int ValueToAdd = Rand_Value.Next(1, 100);
listBox1.Items.Add(ValueToAdd.ToString());
graph1.Series["Data1"].Points.AddY(ValueToAdd);
if (graph1.ChartAreas[0].AxisX.Maximum-10 > graph1.ChartAreas[0].AxisX.ScaleView.Size)
{
graph1.ChartAreas[0].AxisX.ScaleView.Scroll(graph1.ChartAreas[0].AxisX.Maximum);
graph1.Series["Data1"].Points.RemoveAt(0);
}
}
}
}
ok here is the new one:
public partial class Form1 : Form
{
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Interval = 1;
}
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
Random Rand_Value = new Random();
int ValueToAdd = Rand_Value.Next(1, 100);
listBox1.Items.Add(ValueToAdd.ToString());
graph1.Series["Data1"].Points.AddY(ValueToAdd);
if (graph1.ChartAreas[0].AxisX.Maximum - 10 > graph1.ChartAreas[0].AxisX.ScaleView.Size)
{
graph1.ChartAreas[0].AxisX.ScaleView.Scroll(graph1.ChartAreas[0].AxisX.Maximum);
graph1.Series["Data1"].Points.RemoveAt(0);
}
}
private void btn_Start_Click_1(object sender, EventArgs e)
{
graph1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
graph1.ChartAreas[0].AxisX.IsLabelAutoFit = true;
graph1.ChartAreas[0].AxisX.ScaleView.Size = 100;
myTimer.Start();
BlinkLed.BackColor = Color.YellowGreen;
}
private void btn_Stop_Click(object sender, EventArgs e)
{
myTimer.Stop();
BlinkLed.BackColor = Color.AliceBlue;
}
}
Do you think it's better?
What about the changing thread?
If I had a button:
private void PutValueInGraph1()
{
Random Rand_Value = new Random();
int ValueToAdd = Rand_Value.Next(1, 100);
listBox1.Items.Add(ValueToAdd.ToString());
graph1.Series["Data1"].Points.AddY(ValueToAdd);
if (graph1.ChartAreas[0].AxisX.Maximum-10 > graph1.ChartAreas[0].AxisX.ScaleView.Size)
{
graph1.ChartAreas[0].AxisX.ScaleView.Scroll(graph1.ChartAreas[0].AxisX.Maximum);
graph1.Series["Data1"].Points.RemoveAt(0);
}
}
private void button1_Click(object sender, EventArgs e)
{//try to raise exception
PutValueInGraph1();
}
and I change the event like this:
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{//try to raise exception
PutValueInGraph1();
}
The data input accelerate when I'm started the timer and I click all the time on the button1.
Why is there no exception as tom_imk said??
because we can access the same function at the same time....?
Thanks for your answers.
I tried below sample code and it is working fine for me.
public Form7()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.Maximum = 100;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Interval = 1;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Random Rand_Value = new Random();
int ValueToAdd = Rand_Value.Next(1, 100);
chart1.Series[0].Points.AddY(ValueToAdd);
}
I'm surprised you didn't get an exception. You are manipulating UI elements outside the UI thread, something you musn't do, ever.
Refer to the answer in this question:
How to update the GUI from another thread in C#?
EDIT:
To make clear why the timerelapsed method does not run on the UI thread: It's simply the wrong class that is used here. So the easy solution would be to not created a System.Timers.Timer in the Form-constructor but to drop a timer on the form in the form designer and use that instead. The solution by sowjanya attaluri should be marked as the correct answer.

How to store user input in a 2D array?

So I have this GUI application that is supposed to calculate shipping costs. First I need to enter the number of packages that is being shipped (already done), then choose package # from a domainUpDown tool(also complete), and then input dimensions for selected package in a text box. This is where I'm stuck. This is what I have thus far.
const int WEIGHT = 0
const int NMBR_OF_PROPETIES = 7;
const int MAX_PACKAGES = 10;
const double FLAT_RATE = 4.99;
int numOfPackages, packageNumber;
double[,] packagesArray = new double[MAX_PACKAGES, NMBR_OF_PROPETIES];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//label texts get initialized
}
private void button1_Click(object sender, EventArgs e)
{
if (!int.TryParse(numOfPackagesTextBox.Text, out numOfPackages))
{
//message box tells that number cant be parsed
}
else if(numOfPackages > 0 && numOfPackages < 10)
{
//everything in the program is enabled
}
else
{
//message box saying that you can't ship more than 10 packages
}
//adding package numbers to the domainUpDown tool
DomainUpDown.DomainUpDownItemCollection items = packageUpDown.Items;
for (int packageNumber = 1; packageNumber <= numOfPackages; packageNumber++)
{
items.Add(packageNumber);
}
}
private void weightBox_TextChanged(object sender, EventArgs e)
{
packagesArray[(int)packageUpDown.SelectedItem, WEIGHT] = Convert.ToInt32(weightBox.Text);
//also here goes the rest of the dimension entry
}//from here i don't know what to do, and im sure that this isn't right..
The thing I'm confused about is how to save entered text if user somehow changes package number from domainUpDown tool.
Have you tried registering to the OnChanged or SelectedItemChanged events of the DomainUpDown ?
Perform saving on the OnChanged event handler.
Example:
myDomainUpDown.OnChanged += new EventHandler(myDomainUpDown_OnChanged);
void OnChanged(object sender, EventArgs e)
{
//save all the information you need in whatever collection/file/db you want
}

Changing Framerate & System.Threading Aforge.video C# ASP.NET

I have a small project whereby I capture images from a webcam and decode the qr.
The following code captures an image and stores it to a local file, but only when it is not in the while loop. The system.threading appears to make the captured image just black. If i don't use it(the loop), it captures far too many images a second.
So is there a way of changing the aforge.video framerate so that i can capture an image every x seconds without while loop?
public partial class WebForm1 : System.Web.UI.Page
{
public int FrameRate { get; set; }
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;
protected void Page_Load(object sender, EventArgs e)
{
inputDevices.Items.Clear();
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
inputDevices.Items.Add(VideoCaptureDevice.Name);
}
inputDevices.SelectedIndex = 0;
}
public void Start_OnClick(object sender, EventArgs e)
{
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[inputDevices.SelectedIndex].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
int i = 0;
while (i < 10)
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
video.Save("C:\\Users\\Wayneio\\Desktop\\image\\test" + i + ".jpg");
i++;
System.Threading.Thread.Sleep(5000);
}
}
public void Stop_OnClick(object sender, EventArgs e)
{
this.FinalVideo.Stop();
}
}
Additionally I get this error when trying to stop the capture via the asp button:
Object reference not set to an instance of an object on this.FinalVideo.Stop();
Tried this to no avail:
((VideoCaptureDevice)FinalVideo).DesiredFrameRate = 10;
before you code start video set the framreate like this
FinalVideo.DesiredFrameRate = 10;
FinalVideo.Start();
Another option to skip frames that you save is to use a function that is not always true
if you have a global counter value myCounter
do a modulo calulation like below ix mycounter devided by 10 equals 1
myCounter++
if (m(ycounter %% 10))==1) { //code to save your bitmap }

Create Clapper software with Naudio

I'd like to create a software that listens after claps thru microphone..
my first implementation will be to try to get the software to warn when i hears high volume sound.
but i was wondering if someone could help me in the right direction?
public partial class ClapperForm : Form
{
WaveIn waveInStream;
public ClapperForm()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
//start the streaming
waveInStream = new WaveIn();
waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable);
waveInStream.StartRecording();
}
void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
{
//check out what volume it is
}
private void btnStop_Click(object sender, EventArgs e)
{
if (waveInStream != null)
{
//Stop streaming
waveInStream.StopRecording();
waveInStream.Dispose();
waveInStream = null;
}
}
}
Assuming you are recording 16 bit audio (which is the default), then the contents of e.Buffer can be interpreted like this:
for (int n = 0; n < e.BytesRecorded; n += 2)
{
short sampleValue = BitConverter.ToInt16(e.Buffer, n);
}
Then you can look for high values of Math.Abs(sampleValue).

Categories

Resources