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)
Related
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("&", "&&");"
I am working on a project and i have to perform a task in that project that i have to print/find data from a MS Access Database 2016 File through a specific text or keyword i'll try everything but can't solve my problem so after trying everything i decided to post my problem here to get some help to solve it.
I attached my code you can see that but that code doesn't performing any thing and i got a error while try to search anything the error is
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
If there is a handler for this exception, the program may be safely continued.
This is the error I am facing while perfroming the task.
namespace Vechile_Registration_System
{
public partial class Verification : Form
{
public Verification()
{
InitializeComponent();
}
private void Verification_Load(object sender, EventArgs e)
{
}
private void btnsearch_Click(object sender, EventArgs e)
{
searchDataBase();
}
private void searchDataBase()
{
string strsearch = txtsearch.Text.Trim().ToString();
StringBuilder sb = new StringBuilder();
vehicleBindingSource.Filter = string.Format("[Registration No] LIKE '%{0}%'", strsearch);
string strFilter = sb.ToString();
vehicleBindingSource.Filter = strFilter;
if (vehicleBindingSource.Count != 0)
{
dataGridView1.DataSource = vehicleBindingSource;
}
else
{
MessageBox.Show("No Records Found. \n The Vehcile may not register or you have enter wrong Registration Number.");
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 MMenu = new Form1();
MMenu.ShowDialog();
}
}
}
PLEASE READ CAREFULLY:
You've deleted a very important line and because of this, no data is being loaded into your data source.
We are making the first change in Verification.cs file. Change the Verification_Load exactly like this:
private void Verification_Load(object sender, EventArgs e)
{
vehicleTableAdapter.Fill(vehicleDataSet.Vehicle);
// If you want the grid view to show no data at the beginning
// Uncomment the following line
// vehicleBindingSource.Filter = "1 = 0";
}
You've somehow managed to remove the searchbutton_Click event handler.
Please apply these steps exactly as they are suggesting:
In the solution explorer double click the Verification.cs. This should open the form in design mode.
On the form, right click the SEARCH button and select Properties from the menu.
In the Properties window at the top, there are some icons. Find the "Events" icon with a thunderbolt (lightning) shape. Click this.
Now, at the very top, there is the "Click" event.
Be very careful with typing and enter btnsearch_Click
And that's all.
Hope this helps.
Please mark as answer if it does
**** ORIGINAL ANSWER ****
This should solve your problem.
Please use exactly this code.
private void searchDataBase()
{
string strsearch = txtsearch.Text.Trim().ToString();
StringBuilder sb = new StringBuilder();
vehicleBindingSource.Filter = string.Format("[Registration No] LIKE '%{0}%'", strsearch);
if (vehicleBindingSource.Count != 0)
{
dataGridView1.DataSource = vehicleBindingSource;
}
else
{
MessageBox.Show("No Records Found. \n The Vehcile may not register or you have enter wrong Registration Number.");
}
}
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 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 7 years ago.
Improve this question
I'm coding a Skype Tool.
I'm trying to get it to be black listed so they can spam people they won't from a checked list box but I set up everything right that I know of and I receive this on skype. system.windows.forms.checkedlistbox+checkeditemcollection it spams to that instead of the selected people I've chosen? Here's the code:
private void metroButton6_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(checkedListBox1.CheckedItems.ToString()) && !string.IsNullOrEmpty(metroTextBox5.Text))
{
timer3.Start();
}
}
private void timer3_Tick(object sender, EventArgs e)
{
skype.SendMessage(checkedListBox1.CheckedItems.ToString(), metroTextBox5.Text);
}
private void metroButton9_Click(object sender, EventArgs e)
{
timer3.Stop();
}
The SelectedItem of ComboBox is always a object which cannot be compared with a string. that's why you are getting such exception, so i suggest you to use:
metroComboBox1.SelectedItem.ToSting();
Hence your if will be like the following:
if (!string.IsNullOrEmpty(metroComboBox1.SelectedItem.ToString()) && !string.IsNullOrEmpty(metroTextBox1.Text))
{
// code here
}
and the sending message will be like this:
skype.SendMessage(metroComboBox1.SelectedItem.ToString, metroTextBox1.Text);
The reason for this error is metroComboBox1.SelectedItem is of type object. The method string.IsNullOrEmpty expects a string as parameter. So you can try this
if (!string.IsNullOrEmpty(metroComboBox1.SelectedItem as string) && ...
The as operator returns a string or null if metroComboBox1.SelectedItem is null or not a string.
But if SelectedItem is not a string you may rather use this:
if (!(metroComboBox1.SelectedItem == null ||
string.IsNullOrEmpty(metroComboBox1.SelectedItem.ToString())) && ...
I am working on this piece of code and need help with the Coding and Form Design in Visual Studios. I need assistance on writing the code or figuring out what Toolbox control I need to use to have a circle with an "!" in the middle next to confirm password if the password field and confirm password field do not match up. I am not sure what to do to get this image to appear. It will not let me post a picture, but all I need is on the form for it to have a ref exclamation point to the left of it if the password and the confirm password do not match up. The instructions are below.
The password and confirm password fields need to be matched against each other, both when the page is submitted and also when the confirm password field has lost focus. If the form is not valid then the page will not be processed as described below. The password fields need to mask the password.
I have posted all the code I have done for this project so far, but all I really need help with is the conf password error symbol. I just decided to post all the code because I did not have much code for the password section.
namespace Forms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void passwordTxt_TextChanged(object sender, EventArgs e)
{
passwordTxt.PasswordChar = 'x';
passwordTxt.MaxLength = 11;
}
private void confPassTxt_TextChanged(object sender, EventArgs e)
{
confPassTxt.PasswordChar = 'x';
confPassTxt.MaxLength = 11;
}
private void Form1_Load(object sender, EventArgs e)
{
majorBox.Items.Add("Math");
majorBox.Items.Add("Science");
majorBox.Items.Add("English");
majorBox.Items.Add("Philosophy");
majorBox.Items.Add("History");
}
private void submitBtn_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(firstNameTxt.Text) || string.IsNullOrEmpty(lastNameTxt.Text)
|| string.IsNullOrEmpty(userNameTxt.Text) ||
string.IsNullOrEmpty(passwordTxt.Text) || string.IsNullOrEmpty(confPassTxt.Text)
|| string.IsNullOrEmpty(majorBox.Text) || string.IsNullOrEmpty(specialtyBox.Text))
{
MessageBox.Show("You must enter in all fields before moving forward");
}
}
private void majorBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (majorBox.SelectedItem.ToString() == "Math")
{
specialtyBox.Items.Clear();
specialtyBox.Items.Add("Calculus");
specialtyBox.Items.Add("Statistics");
}
else if (majorBox.SelectedItem.ToString() == "Science")
{
specialtyBox.Items.Clear();
specialtyBox.Items.Add("Biology");
specialtyBox.Items.Add("Chemestry");
}
else if (majorBox.SelectedItem.ToString() == "English")
{
specialtyBox.Items.Clear();
specialtyBox.Items.Add("18th Centruy");
specialtyBox.Items.Add("Teacher");
}
else if (majorBox.SelectedItem.ToString() == "Philosophy")
{
specialtyBox.Items.Clear();
specialtyBox.Items.Add("Aristotal");
specialtyBox.Items.Add("Socrates");
}
else
{
specialtyBox.Items.Clear();
specialtyBox.Items.Add("Peace");
specialtyBox.Items.Add("War");
}
}