Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have some code below which is a URL Parser and the if statement doesn't work instead of executing the code when it is true it executes all the code in all the if statements.
public static class var
{ //Global variables
public static string stre = System.Uri.UnescapeDataString(Environment.CommandLine);
public static string[] str = stre.Remove(stre.Length - 1).Split(new string[] { "cmd:" }, StringSplitOptions.None);
}
public CMD()
{ //I suppose this class runs first so all my is statements are here
AClsMsgBox.Show("Parsing...", "CMD Parser", 1000); //msgbox
if (var.stre.Length > 5) { InitializeComponent();} //This executes even if I type "cmd:" which should trigger the statement below. (Always executes) This should run when typing "cmd:echo a command"
else if (var.stre.Length == 4) { Process.Start("cmd.exe"); } //This should run when typing "cmd:"(Never execute)
else { AClsMsgBox.Show("This is a URL Parser use it by typing 'cmd:<command>' in the URL adress bar field of a browsers.","CMD Parser",60000); } //Always executes
}
private void Form1_Load(object sender, EventArgs e)
{
string stra = var.str[1].Replace("&", "&&");
label1.Text += stra;
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("cmd.exe", " /k " + var.str[1]);
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void label1_Click(object sender, EventArgs e) { }
}
This is C# GUI so I am running InitializeComponent() only if I need to display the rest of buttons and stuff. (I think it should stop the GUI)
And there are no errors! But appear after building the code.
On running debugger:
System.IndexOutOfRangeException
HResult=0x80131508
Message=Index was outside the bounds of the array.
Source=CMD Parser GUI
StackTrace:
at CMD_Parser_GUI.CMD.Form1_Load(Object sender, EventArgs e) in D:\bat\CMD Parser GUI\Form1.cs:line 61
line 61 is "string stra = var.str[1].Replace("&", "&&");"
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
On this code I am starting and stopping random sampling based on a fixed sampling time.
When I clicked stop sampling, timers stops correctly.
But when I start back again, the times do not start correctly.
could you please help to check what is wrong here?
public partial class MainWindow : Window
{
public float samplingTime = 10f;
double currentDateTime = 0.0;
DateTime dtEnd;
public bool running;
public MainWindow()
{
InitializeComponent();
timer1 start.code()
void timer_Tick(object sender, EventArgs e)
{
dtEnd = DateTime.Now.AddSeconds(samplingTime);
someCode();
}
}
private void startSample_Click(object sender, RoutedEventArgs e)
{
timer2 start code()
void windowtimer_Tick(object sender, EventArgs e)
{
currentDateTime = (dtEnd - DateTime.Now).TotalSeconds;
if (currentDateTime < 0.1 && running == true)
{
code();
}
}
}
private void stopSample_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
windowTimer.Stop();
}
You are adding the Tick handler multiple times:
windowTimer.Tick += windowtimer_Tick;
is executed every time you start, but you don't unhook it. Probably you should set the handler elsewhere (in the designer maybe).
Or you can add:
windowTimer.Tick -= windowtimer_Tick;
to the Stop handle
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to make a kind of miner without purpose. If you press start the program generates numbers between 1-900. And example 4 has 3 point you will get 3 point if you generated 4. But my problem is making the function that the code will be shown in the textbox.
public partial class Box : Form
{
public void MineEnabled()
{
Random rnd = new Random();
int a = 0;
for (a = 10; a < 200; a++)
{
int coin = rnd.Next(1, 900);
textBox1.Text = coin;
}
}
public Box()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
Close();
}
private void button2_Click(object sender, EventArgs e)
{
MineEnabled();
}
}
It throws an error at textBox1.Text = coin;:
Cannot implicitly convert type 'int' to 'string'
Can someone give me advise what I should do?
There are two problems with your code:
You're only able to see the number that is generated when a = 199
You need to convert your coin integer to a string by textBox1.Text = coin.ToString();
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am sorry to ask. But I'm new to C#.
And I have something wrong in my code (Visual Studio told me it). But I can't find what is wrong.
Can you help me?
I just trying some simple interactive game.
namespace FSociety
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "root"
&& textBox2.Text == "toor") ;
{
progressBar1.Increment(100);
**}**
else
{
MessageBox.Show("Wrong username or password");
}
}
}
}
After bolded } Visual Studio is telling me to expect } but its already there and when I add one more I have like 5 more errors.
Please help.
Thank you.
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "root" && textBox2.Text == "toor") // there was a ; at the end of the if
{
progressBar1.Increment(100);
}
else
{
MessageBox.Show("Wrong username or password");
}
}
Tip: If visual studio can not format code you should check all your closing tags. visual studio recognize the code structure and will make the code look better.
namespace FSociety {
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "root"
&& textBox2.Text == "toor")
{
progressBar1.Increment(100);
}
else
{
MessageBox.Show("Wrong username or password");
}
}
}
}
This is the working code.
in C# you don't have to put any ; after an if(condition).
The correct syntax is:
if(condition)
{
//true condition
}
else
{
//false condition
}
Hope this helps.
Remove the ; after your if statement
Just so you know for the future, if you ever get an error that says "Unexpected [character]" What it actually means is "Hey, I expected there to be something before that character". For instance, if I have code like this
function foo(){
print 'bar'
}
I'm going to get an error that says " Unexepected ' } ' ". This is because the computer is expecting a semicolon right after "print 'bar'" So this would fix the error
function foo(){
print 'bar';
}
So when you get "Unexpected X" start looking right before X for something you left out (or accidentally added in extra)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm quite new to C# and I'm trying to change the height of a rectangle to the value of the number in my textbox when i press a button.
So when I hardcode it I get
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
rct2010.Height = 150;
}
this is without the texbox and worked fine.
I thought I had to do this if I use a textbox:
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
rct2010.Height = txt2010.Text;
}
But this doesn't work. Can somebody help me with this?
Height is an integer, but Text is a string. This isn't safe, in case the string can't parse to an integer, but it will work for your simple example.
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
rct2010.Height = int.Parse(txt2010.Text);
}
To be really safe you would use TryParse.
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
int height;
if(int.TryParse(txt2010.Text,out height))
{
rct2010.Height = height;
}
else
{
rct2010.Height = 150;
}
}
convert it to an int
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
rct2010.Height = int.Parse(txt2010.Text);
}
or you can go a step further
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
int i = 0;
if(int.TryParse(txt2010.Text, out i)
rct2010.Height = i;
else
MessageBox.Show("That's not a number");
}
Presumably the type of Height is int, and so assigning a value of type string won't work and you'll get a compile-time error stating as such (feel free to clarify); you'll need to convert the type, such as:
int height = 0;
if (int.Parse(txt2010.Text, out height)) {
rct2010.Height = height;
}
The TryParse (as opposed to the otherwise suggested Parse) will ensure your application doesn't encounter an exception if the value cannot be parsed (i.e. it's bad, unexpected input). But then, on the other hand, this means your application apparently does nothing with the input (because it doesn't), so you may wish to use an else case to inform the user.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How To Start And Stop A Continuously Running Background Worker Using A Button
I have 2 buttons the first one it's name "Continuous" .. the second one "Stop"
I want to call a method when press the continuous button :
private void continuous_Click(object sender ,EvantArgs e)
{
// continuous taking pictures ...
}
my question is : how can I stop the execution by pressing the stop button ??
I've written a code to take a picture and I've succeeded to take pictures ...
now I want the camera to take continuous snapshots ... but if I press stop button the camera should stop taking pictures ...
I've used BackGroundWorker but the code does not work !!!
this is the code :
private void ContinousSnaps_Click(object sender, EventArgs e)
{
Contiguous.DoWork += Contiguous_DoWork;
Contiguous.RunWorkerCompleted += Contiguous_RunWorkerCompleted;
Contiguous.RunWorkerAsync();
}
private void Contiguous_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; ; i++) TakeSnapShotCommand();
}
private void Contiguous_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
MessageBox.Show("complete");
}
//------------------------------------------------------------------//
private void Stop_Click(object sender, EventArgs e)
{
Contiguous.CancelAsync();
}
//--------------------------------------------------------------------//
how can I achieve the result that I want ?!
Try and see if this is going to work:
In your _DoWork event:
private void Contiguous_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; ; i++)
{
if (Contiguous.CancellationPending)
{
e.Cancel = true;
return;
}
TakeSnapShotCommand();
}
}
And in the Stop_Click to the following:
private void Stop_Click(object sender, EventArgs e)
{
if (Contiguous.WorkerSupportsCancellation)
Contiguous.CancelAsync();
}
Also make sure you allow cancellation (and if you want to take my advice here - move these event registrations in a the form load, so they will be executed once, not every time the button is clicked - leave just the Continuous.RunWorkerAsync()):
// your form load <---
private void Form1_Load(object sender, EventArgs e)
{
Contiguous.DoWork += Contiguous_DoWork;
Contiguous.RunWorkerCompleted += Contiguous_RunWorkerCompleted;
Contiguous.WorkerSupportsCancellation = true; // allowing cancellation
}
private void ContinousSnaps_Click(object sender, EventArgs e)
{
// not a bad idea if you disable the button here at this point
Contiguous.RunWorkerAsync();
}