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 6 years ago.
Improve this question
So far this is my code. The problem I am encountering is that the file is not being found.
namespace Assignment_Forms_Aplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Define Variable
string[] words = new string[10];
// Read the text from the text file, and insert it into the array
StreamReader SR = new StreamReader(#"Library.txt");
//
for (int i = 0; i < 10; i++)
{
words[i] = SR.ReadLine();
}
// Close the text file, so other applications/processes can use it
SR.Close();
}
}
}
Hi Gailen use the following method:
File.ReadAllLines(#"location");
If location is correct then this will work
Assign to variable, for example
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 4 months ago.
Improve this question
1. Summarize the problem
The following for cycle keeps on running as i want, but always give me the same clicked button that is "0".
It does not give me an error. But by playing the game i can see that it's always the same number.
2. Describe what you've tried
I've tried searching around the internet for people like me. but sadly i couldn't find anything.
3. Show some code
Code that i'm talking about.
int ButtonNum;
public void Start()
{
for (int i = 0; i < ButtonsPage.Length; i++)
{
ButtonsPage[i].GetComponent<Button>().onClick.AddListener(delegate { ButtonClicked(ButtonNum); });
}
}
public void ButtonClicked(int i)
{
Debug.Log("Clicked" + i);
if (WhichType == "Nose")
{
NoseColor.sprite = NosesColor[i];
NoseOutline.sprite = NosesOutline[i];
}
//ButtonNum will be used to say which one is clicked. Still haven't add it though cause i wanted to fix this problem before
}
You are not modifying ButtonNum in any way, I assume the goal is to use i as button number, try changing your code to:
public void Start()
{
for (int i = 0; i < ButtonsPage.Length; i++)
{
var temp = i;
ButtonsPage[i].GetComponent<Button>().onClick.AddListener(delegate { ButtonClicked(temp); });
}
}
Temporary variable is required due to how closures work in C#.
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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I am trying to save the username entered in the "enter playername" field in order to create a highscore list, can anyone help me with that ?
namespace TikTakTo
{
public partial class Anmeldung : Form
{
public Anmeldung()
{
InitializeComponent();
}
private void button_play_Click(object sender, EventArgs e)
{
Form1.setSpielerName(Spieler_1.Text, Spieler_2.Text);
Form1 frm = new Form1();
frm.Show();
this.Hide();
}
private void Spieler_2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString() == "\r")
button_play.PerformClick();
}
}
}
Most recommanded option for windows-forms applications is a settings file located in the Properties folder.
The data there can be saved per-user and per-application, and stored in the application's AppData folder.You can read the settings-table fields, change their values and save your changes.
e.g:Properties.Settings.Default["SomeProperty"] = "Some Value";Properties.Settings.Default.Save();
Read this question for more info.
Basically you have two options, you can either save the info to file, or save it to database. This should help you: Best way to store data locally in .NET (C#)
Always try to make your own search before asking a question!
you can write in txt file in your project folder , and after restarting the app, you can control the file in that location , if its exist you can read data and put the form.
like a cookie.
example code.
public static string _debugPath { get { return Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); } }
public static void WriteToText(string txt)
{
DateTime _dt = DateTime.Now;
using (StreamWriter wr = File.AppendText(Path.Combine(_debugPath, "Info.Inc")))
{
wr.WriteLine(txt+"|");
wr.Close();
}
}
public static bool ReadInfo()
{
FileInfo fi = new FileInfo(Path.Combine(_debugPath , "Info.Inc"));
if (fi.Exists)
{
using (StreamReader rd = new StreamReader(Path.Combine(_debugPath , "Info.Inc")))
{
//take data in here and put the form.
}
}
}
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 6 years ago.
Improve this question
I am building a program for my course in which i need global objects as i intend to have the object accessible from many forms and edited also. Before saying just use global variables i cant the specs state for use of OOP.
my latest attempt to fix this is using a public class but this gave a protection error problem
Code:
Form1.cs (forgot to rename and not re doing all the code and design)
public class ObjectsGlobal
{
Bays bay1 = new Bays();
Bays bay10 = new Bays();
}
frmInput.cs
private void btnAdd_Click(object sender, EventArgs e)
{
if ( 1 == Convert.ToInt32(nudBayNum))
{
ObjectsGlobal.bay1.CarMake = txtMake.Text;
}
}
any ideas are welcome at this point
Change it to:
public static class ObjectsGlobal
{
public static Bays bay1 = new Bays();
public static bay10 = new Bays();
}
Also, as recommended in a comment I have now read, take a look at the Singleton Pattern.
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 9 years ago.
Improve this question
I am pretty new to C#, I have been searching for an hour or so now for what I need and can't find it. I am trying to look at the contents of 32 different text box's using for loops.
The code I have at the moment is:
private void btnCalculate_Click(object sender, EventArgs e)
{
string ElementString;
Control ElementControl;
double Num;
Boolean errorMsg = false;
for (int x = 1; x <= 4; x++)
for (int y = 1; y <= 4; y++)
{
ElementString = "txtA" + x.ToString() + y.ToString();
ElementControl = this.Controls[ElementString];
ElementString = ElementControl.Text.Trim();
if (!double.TryParse(ElementString, out Num))
{
errorMsg = true;
break;
}
}
if (errorMsg)
MessageBox.Show("Error Processing Input Matricies, invalid entries");
}
Ok changing this part, sorry for not putting more information, but hopefully this will help.
The program crashes when the button is clicked. It crashes when running the line:
ElementString = ElementControl.Text.Trim();
And gives the error message:
Object reference not set to an instance of an object.
Thank you
The first thing you need to do is check if ElementControl is null. Do that right before you call the line that is throwing the exception.
Another way to do this would be to search for all of the TextBox controls in your container and check them that way. You can use the following to do that:
foreach(Control c in this.Controls)
{
if (c is TextBox)
{
// Do whatever you want to do with your textbox.
}
}
That is much more dynamic than trying to call retrieve the controls by name.
put an if statement around the part where it crashes:
if(ElementControl != null)
{
ElementString = ElementControl.Text.Trim();
}
else
{
//Handle error if element control is null
}
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 have a question about creating calculator in C# Windows Form Application.
I want it to be possible write with form buttons an expression in textbox so for example 2+3+7= and after pressing "=" button program will read all digits and signs and perform calculation... I don't know from where to start and how could do it in such a way. Any help to any reference or smth to look at how to start doing such a expressions?
Main thing is how to read, seperate and after calculate values from textbox.
Thanks.
With the Split method you could solve this rather easy.
Try this:
private void button1_Click(object sender, EventArgs e)
{
string[] parts = textBox1.Text.Split('+');
int intSum = 0;
foreach (string item in parts)
{
intSum = intSum + Convert.ToInt32(item);
}
textBox2.Text = intSum.ToString();
}
If you would like to have a more generic calculation, you should look at this post:
In C# is there an eval function?
Where this code snippet would do the thing:
public static double Evaluate(string expression)
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("expression", string.Empty.GetType(), expression);
System.Data.DataRow row = table.NewRow();
table.Rows.Add(row);
return double.Parse((string)row["expression"]);
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Evaluate(textBox1.Text).ToString();
}