|
How do I fix this mess!! trying to C# programme which displays changing minutes and seconds, representing them by two long rectangles, maximum width of rectangle equal to 600 pixels (10 pixels for each minute and each second). redraw the two rectangles every second. please help!!!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Random randomNumber = new Random();
//private void timer1_Tick();
private Graphics paper;
public Form1()
{
InitializeComponent();
paper = progressBar1.CreateGraphics();
timer1.Interval = 600;
timer1.Enabled = true;
//count = count + 1;
//label1.Text = Convert.ToString(count / 60);
}
private void start_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void stop_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void clear_Click(object sender, EventArgs e)
{
paper.Clear(Color.White);
}
private void timer1_Tick(object sender, EventArgs e)
{
//int x = 5;
//int y = 20;
int x, y, size;
Brush myBrush = new SolidBrush(Color.Black);
x = timer1.Tick+= progressBar1.Width;
y = timer1.TickGetType+= progressBar1.Height;
size = randomNumber.Next(1, 60);
paper.FillRectangle(myBrush, x,y,size,size);
|
|
|
|
|
ketto wrote: paper = progressBar1.CreateGraphics();
Disaster. Handle the paint event and invalidate your form in the tick event instead.
ketto wrote: size = randomNumber.Next(1, 60);
If it's a clock, why does it move randomly ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
any suggested alterations on this code???
|
|
|
|
|
Well, I'd say read up on the paint event and start again. If this is meant to be a clock, I can only assume you copied it from somewhere and you're trying to change it to work as a clock ? Start from scratch.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
yes I did copy frpm some where and I thought it would guide me in getting what am trying to do. I now can't make sense of what am trying to do hence my post
|
|
|
|
|
Funny, I got a 1 and a 5 in the same thread....
Copying code you don't understand is never a good start. Someone posted code that should do all of what you want. I'd have preferred to walk you through it bit by bit, but you have the code on this thread, now I recommend you take the time to study it and research the bits you don't understand, so you learn from it.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
<br />
using System;<br />
using System.Windows.Forms;<br />
using System.Drawing;<br />
<br />
namespace WindowsApplication1<br />
{<br />
public partial class Form1 : Form<br />
{<br />
private Brush myBrush;<br />
private Graphics paper;<br />
<br />
private const int barWidth = 40;<br />
<br />
int seconds, minutes;<br />
<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
<br />
this.myBrush = new SolidBrush(Color.Black);<br />
this.paper = pictureBox1.CreateGraphics();<br />
<br />
this.timer1.Interval = 1000;<br />
<br />
this.seconds = 0;<br />
this.minutes = 0;<br />
}<br />
<br />
private void button1_Click(object sender, EventArgs e)<br />
{<br />
this.timer1.Start();<br />
}<br />
<br />
private void button2_Click(object sender, EventArgs e)<br />
{<br />
this.timer1.Stop();<br />
}<br />
<br />
private void timer1_Tick_1(object sender, EventArgs e)<br />
{<br />
this.paper.Clear(SystemColors.Control);<br />
<br />
if (this.seconds < 60)<br />
this.seconds++;<br />
<br />
else<br />
{<br />
this.seconds = 0;<br />
this.minutes++;<br />
}<br />
<br />
paper.FillRectangle(myBrush, 0, 0, (seconds * 10), barWidth);<br />
paper.FillRectangle(myBrush, 0, 60, (minutes * 10), barWidth);<br />
}<br />
}<br />
}<br />
modified on Tuesday, April 29, 2008 1:25 AM
|
|
|
|
|
MarkBrock wrote: this.paper = pictureBox1.CreateGraphics();
This still sucks.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Just for you Christian .
<br />
using System;<br />
using System.Windows.Forms;<br />
using System.Drawing;<br />
<br />
namespace WindowsApplication1<br />
{<br />
public partial class Form1 : Form<br />
{<br />
private Brush myBrush;<br />
<br />
private const int barWidth = 40;<br />
<br />
int seconds, minutes;<br />
<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
<br />
this.myBrush = new SolidBrush(Color.Black);<br />
<br />
this.timer1.Interval = 100;<br />
this.timer1.Enabled = true;<br />
<br />
this.seconds = 0;<br />
this.minutes = 0;<br />
}<br />
<br />
protected override void OnPaint(PaintEventArgs e)<br />
{<br />
e.Graphics.FillRectangle(myBrush, 0, 0, (this.seconds*10), barWidth);<br />
e.Graphics.FillRectangle(myBrush, 0, 60, (this.minutes * 10), barWidth);<br />
<br />
base.OnPaint(e);<br />
}<br />
<br />
private void button1_Click(object sender, EventArgs e)<br />
{<br />
this.timer1.Start();<br />
}<br />
<br />
private void button2_Click(object sender, EventArgs e)<br />
{<br />
this.timer1.Stop();<br />
}<br />
<br />
private void timer1_Tick_1(object sender, EventArgs e)<br />
{<br />
this.Invalidate();<br />
<br />
if (this.seconds < 60)<br />
this.seconds++;<br />
<br />
else<br />
{<br />
this.seconds = 0;<br />
this.minutes++;<br />
}<br />
}<br />
}<br />
}<br />
|
|
|
|
|
Lovely
CreateGraphics has it's place, but it gets misused a lot, and a clock is surely something that could get obscured, even if it does repaint itself every second.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
hey ketto
i see your new to this forum, would you please put code in your future posts in code and pre tags... especially if you posting quite a few lines of code...
the reson for this is to keep the formatting (indentation)... EG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
now doesnt that look better?
good luck with your problem
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
I am really new as u can tell from question. New in programming indeed. thanks for your guidance
|
|
|
|
|
Good Day,
Say I have an array of numbers. For instance:
int[] myNumber = {1,11,21,42,32};
and I would like to randomly remove 2 items from that array. It is easy to implement, but mine was sort-of inefficient (I'm working with large arrays). So I'm asking for some advice.
Thanks.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.
|
|
|
|
|
Ian Uy wrote: like to randomly remove 2 items from that array. It is easy to implement, but mine was sort-of inefficient (I'm working with large arrays).
Let's see the code, how can where is it being inefficient?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Well, what I do is first I get the size of the array, say 10, then randomly generate a number from 1 to 10, then check if the number generated has already been removed, if not, generate again, if yes, remove it. So it's like a guessing game.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.
|
|
|
|
|
If you can't change it to a List as Christian suggests, then instead of generating a new value, simply increment (or decrement) until you find a non-removed value (be sure to wrap if you hit the end of the array).
|
|
|
|
|
There's no efficient way to remove from the middle of an array, use a list instead.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I have been doing C# (WinForms) development for the past 5 years. I have just started an ASP project. From my experience I have always programmed to type safe objects that implemented the needed interfaces to allow generics. The current projects is using DataTables as the primary object for storing all information and does not use type safe objects that implement generic interfaces. Today, I had a discussion with the programmer who wrote the system that uses only tables and was unable to give him a list of the true benefits that type safe objects using generics provide. Can anyone list what advantage using the type safe objects have over using table objects handling everything.
Thanks,
Steve
|
|
|
|
|
Well - the first one is typesafety. You know - letting the compiler catch errors rather than having to rely on runtime. Secondly, DataTables - yukk..... if you change a fieldname in your database then you are going to have one hell of a job catching all of the instances of the DataTable throughout the application. Also - it's not interoperable with none .NET systems without a great deal of work, and it's a hell of a big structure (serialization is a bitch with this).
There - is that enough to get on with?
|
|
|
|
|
hy everyone!
i was wondering if it is possible to combine number formating and dateformating in one function.
formating a number is done e.g. by using {#,##0.0000} and date formating is done by using {0:dd/MM/yyyy}.
i was wondering if it is possible to separate the numberformating and the dateformating. well, if there are these two, then you might try to get the difference by checking for the ":". But that's no "clean" way to do this.
public static string ConvertFormats (string input, string formatstring)
{
if (formatstring.Contains(":")
{
}
else
{
}
return input;
}
So what if this function should format stringlength as well? then this won't work anymore.
Anyone a better idea?
thanks!
Stephan.
|
|
|
|
|
stephan_007,
Why would you want to do this? there are already methods in place to do this.
Regards,
Gareth.
|
|
|
|
|
Why take a string as input? Even an object would be better:
if ( input is int ) ((int) input).ToString ( formatstring ) ;
else if ( input is System.DateTime ) ((System.DateTime) input).ToString ( formatstring ) ;
But would calling the appropriate ToString directly help?
someint.ToString ( "#,##0.0000" ) ;
somedate.ToString ( "dd/MM/yyyy" ) ;
(Of course, I also have to recommend that you use an ISO 8601 date format like "yyyy-MM-dd".)
|
|
|
|
|
i parse from a textfile which contains infos like
|value|{#,##0.0000}
|date|{0:dd/MM/yyyy}
when parsing the tokens (a token is e.g. |value|) is replaced by it's value and formated by the formatstring behind. so i parse the string after the token and format the string with this formatstring.
so the input is always a string.
i do also provide the culture info which corresponds to the string format so it is formated correctly.
<br />
input.ToString(FormatString, new CultureInfo(LocalString));<br />
|
|
|
|
|
Sounds like a bad design; go slap the person responsible.
Why doesn't the system that writes the file perform the format?
|
|
|
|
|
Hi frnds,
I am working with System.Timers.Timer
I have configured timer to trigger every 60 mins, its working fine on my machine but getting triggered every 7 minutes on server.
Is it an issue with the Timer class? I dont see any reason for this to happen...
Keep DotNetting!!
GeekFromIndia
|
|
|
|