Make points progress like Visual studio? - c#

Iam a novice programmer who basically just started,im not sure what I should search for my issue,what I wanna do is making those points like Visual studio if someone already saw VS 2012-13 already installing , those points keep progressing and dissapearing ,hope some can help me out.

Have you tried with ascii and a label?
You can manage to wait for the process load and stuff so meanwhile you can show this:
label l1 = new label();
label.Location = new Point(X location, Y location) //Location despends on you
and then just add this in:
public Form1()
{
InitializeComponent();
label l1 = new label();
label.Location = new Point(X location, Y location) //Location despends on you
_load();
}
Create a timmer method:
private async void _load()
{
string[] n = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
while (true)
{
l1.Text = "• • • • •";
await Task.Delay(500);
for(int i=0; i<10; i++)
{
if (i < 4)
{
await Task.Delay(50);
l1.Text = "• • • •" + n[i] + "•";
}
else if(i >4 && i< 7)
{
await Task.Delay(100);
l1.Text = "• • • •" + n[i] + "•";
}
else
{
await Task.Delay(180);
l1.Text = "• • • •" + n[i] + "•";
}
}
}
}
You just have to use the head to guess how to make it look like the VS's one D:
Strings are funny :3

Don't do that. Microsoft is violating their own UI guidelines. (See #11 here].
Instead, indicate progress using a standard ProgressBar control. That's what it is for, and all your users already recognize its meaning.
The animation you indicate is most similar to the Marquee style of the standard progress bar.

Related

Trying to use a for loop to try and match user input to a premade array. Getting a warning for unreachable code and the program won't recognize match

I must get the user to enter a zip code and then the program searches a premade array to either confirm or deny their zip code.
It works fine when the zip codes don't match but it doesn't recognize it when it does.
Also, the line where I am getting the 'unreachable code' warning is in my loop controls:
for (int i = 0; i < zips.Length; i++)
The rest of the code looks like this:
string[] zips = { "I won't bother you with all the zip codes manually entered here" };
System.Console.WriteLine("Enter your zip code > ";
string userZip = System.Console.ReadLine();
for (int i = 0; i < zips.Length; i++)
{
if (userZip == zips[i])
System.Console.WriteLine("Delivery to " + userZip + " ok.");
else
System.Console.WriteLine("Sorry - no delivery to " + userZip);
break;
}
Here's one working solution (includes some sample made up ZIP codes).
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var zips = new [] {"12345", "23456", "34567"};
System.Console.WriteLine("Enter your zip code > ");
string userZip = System.Console.ReadLine();
var match = zips.FirstOrDefault(z => z == userZip);
if(match!=null) System.Console.WriteLine("Delivery to " + userZip + " ok.");
else System.Console.WriteLine("Sorry - no delivery to " + userZip);
}
}
Explanation: instead of manually writing a for loop, I used a System.Linq function FirstOrDefault() to look through the list of zip codes instead. Internally it uses a loop but its just a simpler way of writing the logic required (IMO). It will either return the first match or null if none.
And you can run and try it right here: https://dotnetfiddle.net/yHDfEk
Sample input/output (no match):
Enter your zip code >
12344
Sorry - no delivery to 12344
Sample input/output (match):
Enter your zip code >
23456
Delivery to 23456 ok.
An example of where this is easier is that it deals with loop termination for you. (As noted in a comment by #Flydog57) you have the break statement in the wrong place which causes your program to misbehave. That extra logic is handled implicitly in the example I gave. Instead, you would want something like this:
var found = false;
for (int i = 0; i < zips.Length; i++)
{
if (userZip == zips[i])
{
found = true;
System.Console.WriteLine("Delivery to " + userZip + " ok.");
break;
}
}
if(!found) System.Console.WriteLine("Sorry - no delivery to " + userZip);
Note that if you wanted to use a manual loop, I would recommend foreach instead of for in most situations where you have a simple array, list, or similar data structure to loop through. foreach is just easier to get right.
var found = false;
foreach(var zip in zips)
{
if (userZip == zip)
{
found = true;
System.Console.WriteLine("Delivery to " + userZip + " ok.");
break;
}
}
if(!found) System.Console.WriteLine("Sorry - no delivery to " + userZip);
The question is not very much clear and also not formatted well. I guss this would be the solution:
public class Run {
public static void main(String[] args) {
String[] zips = {"11", "13", "30", "50"};
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
Stream<String> stream = Stream.of(zips);
if(stream.anyMatch(x -> input.equals(x))){
System.out.println("Delivery to " + input + " ok.");
}else {
System.out.println("Sorry - no delivery to " + input);
}
}
}
It seems like this code might actually be written in C#, is this the language that you're targeting? For instance, Java does not have a System.Console.WriteLine method available, however C# does:
https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-6.0#system-console-writeline(system-char())
A question like this might be best suited for the Code Review StackExchange:
https://codereview.stackexchange.com/
EDIT
It looks like you may simply be missing a closing ) in your call to WriteLine.
the break isn't necessary as it will leave the for loop after the first iteration, not allowing you to search the rest of the string[] It looks like you should put the break in the body of the first if, which will require you to add {} to it
https://replit.com/#mynameishank/PrudentForthrightCodec#main.cs
class Program {
static void Main(string[] args) {
string[] zips = { "1", "2" };
System.Console.WriteLine("Enter your zip code > ");
string userZip = System.Console.ReadLine();
for (int i = 0; i < zips.Length; i++) {
if (userZip == zips[i]) {
System.Console.WriteLine("Delivery to " + userZip + " ok.");
break;
}
else
System.Console.WriteLine("Sorry - no delivery to " + userZip);
}
}
}

c# chart Windows Forms zoom work, but scroll not worked

I have one chart in my form. I use windows forms application.
The zoom in the chart work perfectly, but when I want to scroll on axis X I receive the message:
The method or operation is not implemented.
My code is:
chart1.ChartAreas[0].AxisX.Minimum = double.NaN;
chart1.ChartAreas[0].AxisX.Maximum = double.NaN;
chart1.ChartAreas[0].RecalculateAxesScale();
chart1.Series["Min"].BorderWidth = 3;
chart1.Series["Avg"].BorderWidth = 3;
chart1.Series["Max"].BorderWidth = 3;
int i = 1;
foreach (DataRow row in dt3.Rows)
{
chart1.Series["Min"].Points.AddXY(i, row.ItemArray[0]);
chart1.Series["Avg"].Points.AddXY(i, row.ItemArray[1]);
chart1.Series["Max"].Points.AddXY(i, row.ItemArray[2]);
i++;
string rowz = string.Format("Min Q - {0}" + Environment.NewLine + "Avg Q - {1}"
+ Environment.NewLine + "Max Q - {2}" + Environment.NewLine + "Year - {3}" + Environment.NewLine
+ Environment.NewLine,
row.ItemArray[0], row.ItemArray[1], row.ItemArray[2],
row.ItemArray[3]);
s1 += "" + rowz;
}
And I want to show the values on the chart ? How can I do this.

Vertical scroll Scintilla Textbox during Text Changed event

Setting a Scintilla.Net textbox with a string and scrolling to last line doesn't work.
This Q & A How make autoscroll in Scintilla? has the answer but it wont work at the same time as setting the text.
Bare bones repro:
private void button1_Click(object sender, EventArgs e)
{
string s = RandomString(400);
scintilla1.Text = s + " " + s + " " + s + " " + s + " " + s;
scintilla1.Scrolling.ScrollBy(0, 10000); //<-doesn't work (but does work eg in a Button2_click)
}
private static Random random = new Random((int)DateTime.Now.Ticks);
private string RandomString(int size)
{
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
Does anyone know how to scroll vertically down to end line after setting the text?
Well you can try to put Refresh() after adding the text;
scintilla1.Text = s + " " + s + " " + s + " " + s + " " + s;
scintilla1.Refresh();
for this case i found out that you will need to Refresh() twice depend on the length of the string you put on the textbox.
For anyone wondering in the end I ditched Scintilla in favor of ICSharpCode.TextEditor. <- This one was a little unstable so I used the Digitalrune version of the ICsharp.TextEditor
I found enhancing the ICSharpCode.TextEditor was trivial compared with Scintilla.
Another huge benefit of ICSharpCode.TextEditor is that allows you to customize/build your own Syntax Highlighting, eg: https://github.com/icsharpcode/SharpDevelop/wiki/Syntax-highlighting

How to make a label scroll a word back and forth?

I came up with the idea of making a label scroll a word to one side and then change the word and scroll back to the other like so
"ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping "
" ping"
" pong"
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
" pong "
"pong "
I want it to do ^^ only in a constant loop but I don't know how I would even get started doing that I would REALLY appreciate it if someone could help me with this. The max length of the text has to be 15 characters.
I don't care if it is smooth scrolling.
I want it to be a Winforms application and use .Net framework 4.0.
Here's what I would do, it seemed to work just fine when I tested it out, I created a windows form with a timer and label on it. Make sure to call timer.Start() when you open the form and it will start bouncing around the screen. If you change iUBound to a larger value it will move more spaces across the screen.
string _sPing = "ping";
string _sPong = "pong";
bool bGoingUp = true;
int iUBound = 15;
int iCnt = 1;
public Form1()
{
InitializeComponent();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (bGoingUp)
{
label1.Text = " " + label1.Text;
iCnt++;
}
else
{
label1.Text = label1.Text.Substring(1,label1.Text.Length - 1);
iCnt--;
}
if (iCnt == iUBound)
{
bGoingUp = false;
label1.Text = label1.Text.Replace(_sPing, _sPong);
}
else if (iCnt == 1)
{
bGoingUp = true;
label1.Text = label1.Text.Replace(_sPong, _sPing);
}
}
I'd keep the label contents the same and just move the label, feels like it should be less CPU load and the scrolling will be smoother.
Make a for-loop that runs from 0 to 11 (15 - length of "ping"). With new String(' ', i) you can create a string that is i spaces long. Then set the Text of your label to this space string concatenated with the word "ping".
Now you can make another loop, running from 11 down to 0 doing the same but with the word "pong".
If you enclose both loops in an endless loop (while (true) { ... }) this will run indefinitely.
You also might want to add a pause each time after you set the label text with Thread.Sleep(200). Where you specify the time is in milliseconds.
EDIT (since it is not homework):
Go to the events tab in the properties window and add a Shown event handler
private void frmMarquee_Shown(object sender, EventArgs e)
{
while (true) {
for (int i = 0; i <= 11; i++) {
label1.Text = new String(' ', i) + "ping";
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
for (int i = 11; i >= 0; i--) {
label1.Text = new String(' ', i) + "pong";
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
}
}
Note, this solution is not perfect, as the form will not close properly. You will have to abort the program. A solution using a timer will work smoother and the form will behave as expected when closing, however this is a straightforward and simple solution.
I found this example. Pretty close to what you want. The two key elements are (1)using StringBuilder functions to append characters and (2) an asynchronous delegate to put the animation in a different thread.
The idea of the StringBuilder is great because it should be more efficient that dealing with String. And I like Asynchronous delegate because it sounds way more impressive than Timer

Timer in winform with an iteration

foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
{
makeSelfMoves = replay[item.Key];
codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
PrintPieces(codeFile.PieceState());
// MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] + "____a is: " + a);
}
i want to execute this whole iteration, that i responsible for a game replay, in 1 second intervals. i put a timer in my form and set it to 1 second (this should make the pieces to be moved at 1 second intervals). i made an event, and before the loop i put the statement, timer1.enabled=true. The iteration will reach the end in a quick manner.. how do you use a timer to set iteration to execute each second using the timer?
public void ReplayGame()
{
Class2.replayIsOn = true;
replay=serializeMeh.giveBackDictionary();
int[] makeSelfMoves=new int[4];
//Timer t = new Timer();
//t.Interval = 1000;
//t.Tick += timer1_Tick;
//t.Enabled = true;
//t.Start();
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
//foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
//{
// makeSelfMoves = replay[item.Key];
// codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
// PrintPieces(codeFile.PieceState());
// MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] );
//}
}
The above method is activated if i want a replay.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int[] makeSelfMoves = new int[4];
foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
{
makeSelfMoves = replay[item.Key];// delivers an array of a single move location startrow,startcolumn,endrow,endcolun
codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
PrintPieces(codeFile.PieceState());// prints the code on the board
System.Threading.Thread.Sleep(1000);
}
}
it does the work, but the problem that i had is after the loop finishes, the pieces that existed disappear. is it because of the background worker, cause without it , i have no pieces disappear at the end of the process. The second time that i activate the method, it happens
Run it in a background thread and put Thread.Sleep(1000) in the loop.
This way it will be time based and not freeze your app.
We need to see more of your code, but it's pretty simple:
using System.Windows.Forms;
SomeMethod(...)
{
Timer t = new Timer();
t.Interval = 1000;
t.Tick += t_Tick;
t.Enabled = true;
t.Start();
}
void t_Tick(...)
{
foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
{
makeSelfMoves = replay[item.Key];
codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
PrintPieces(codeFile.PieceState());
// MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] + "____a is: " + a);
}
}
Personally, I would put this into a seperate thread and use Thread.sleep(). In combination with accessing GUI-elements, I would suggest a BackgroundWorker instead (see MSDN, easy to implement).
If you insist in using a timer, I would suggest you use the suggestion of 'Ed S.'. The timer could also be placed on your form, instead of creating it in code of course. Just what you prefer.

Categories