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...
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 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.
This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 7 years ago.
I want to transfer data from Form2 into Form1's listBox1. Form2 includes several textboxes,
I have tried to face this problem by placing all of this code into Form1 but I cannot get the values needed from Form2.
Can anybody help, I understand I'm not very clear but I can answer any questions about the code.
private void button3_Click(object sender, EventArgs e)
{
List<Form1Other> FileList = new List<Form1Other>();
Form1Other[] f1Other = new Form1Other[10];
Form1 testForm1 = new Form1();
string TName = textBox1.Text;
string TDesc = textBox2.Text;
decimal TPrior = numericUpDown1.Value;
string TDate = dateTimePicker1.Value.ToShortDateString();
string TCompl = "UNFINISHED";
FileList.Add(new Form1Other(TName, TDesc, TPrior, TDate, TCompl));
testForm1.listBox1.Items.Add(FileList[0].tName);
testForm1.listBox1.Items.Add(FileList[0].tDesc);
testForm1.listBox1.Items.Add(FileList[0].tPrior);
testForm1.listBox1.Items.Add(FileList[0].tDate);
testForm1.listBox1.Items.Add(FileList[0].tCompl);
System.Diagnostics.Debug.WriteLine(FileList[0].tDesc);
You most likely need some sort of messaging/event mechanism so you can communicate across different views/forms.
You either need to implement your own using a Queue and Publish/Subscribe. System.Messaging
Or use one that is already implemented.
A few to note that I use are:
The MVVM Light Messenger
EventAggregator.Net
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();
}
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.