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.
}
}
}
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 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
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I tried to retrieve data from access database. I wrote the function inside of a class and i got this error. So please anyone know an answer for this question please let me know how can i fix it. the below function is wrote in a class and i need to know how to display data to listview control by using this function and how to call it form load event. i tried to fix this lots of time and i still didn't get a solution for this.
public List<String> displayRoom()
{
List<String> rooms = new List<String>();
String query = "select * from room";
cmd = new OleDbCommand(query, connect);
reader = cmd.ExecuteReader();
while(reader.Read()){
rooms.Add(reader["buyer_name"].ToString());
rooms.Add(reader["room_type"].ToString());
rooms.Add(reader["date_from"].ToString());
rooms.Add(reader["date_to"].ToString());
}
return rooms;
}
If you can read data properly from database then just in the form load or constructor of the form class write following line:
//here dbClass is an instance of your database class
List<String> rooms = dbClass.displayRoom(); //though `get/load rooms()` would be more appropriate
foreach (string s in rooms)
{
ListViewItem lv=new ListViewItem(s);
listView1.Items.Add(lv); //listView1 is the ListView instance in your form
}
EDIT
If your Form has name Form, then either in Form1_Load(object sender, EventArgs e) or in public Form1() constructor (after InitializeComponent is called), you can put the above code.
The ExecuteReader requires an open connection:
connect.Open();
Insert just above
reader = cmd.ExecuteReader();
And close the connection when you are done
}
connect.Close();
return rooms;
See MSDN for more info...
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 am using Forms in Visual Studio.I would like to display an error message when save is clicked on the save file dialog without giving the filename. How do I do it?
I've tried the following code but it did not work:
I've tried 2 logics.
1)
if (string.IsNullOrEmpty(saveFileDialog1.FileName))
{
MessageBox.Show("Enter the Filename");
}
2) This is the 2nd Logic
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
if (saveFileDialog1_FileOk == saveFileDialog1.FileName)
{
MessageBox.Show("Enter the Filename");
}
else
{
string name = saveFileDialog1.FileName;
string testvar = textBox1.Text;
File.WriteAllText(name, testvar);
}
}
I want to display an error message when save button is clicked without entering anything in the File Name.
I hope the question is clear!
SaveDialog doesn't really work like this - it won't return an empty string using the OK button, so you really need to check the DialogResult rather than the text of the string. Perhaps something like:
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
if (System.IO.File.Exists(saveFileDialog1.FileName))
{
//overwrite existing file here
}
else
{
//save as new file here
}
}
else
{
//dialog did not return from an OK button (e.g. cancel)
}
Also consider reading this answer which handles whether the file already exists in a different way.
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 8 years ago.
Improve this question
I am new to c# and do not know how to explain this but I wanted to know if it was possible to add a charge for every character that is entered into a text box.
For example if the customer was to enter "HELLO HI" they should be charged £5 with a additional cost of £1 per letter or number this £1 charge should also apply for any spaces entered.
Sorry if I have not explained this properly but this is the best way I can.
Thank you
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int amount;
private void textBox1_TextChanged(object sender, EventArgs e)
{
amount = 5;
amount = amount + textBox1.Text.Length;
label1.Text = amount.ToString();
}
}
}
Well, it pretty much looks like this, I guess:
string str = "Happy Birthday";
int price = str.Length + 5;
Find the length of entered string in textbox using TextboxID.text.Length and add your fix charge that 5 , as ALEX explain you.
please see alex's answer.