I'm trying to write information to a TextFile through TextBoxes (TextFile in this case is something like a database). It's working when I use File.AppendAllText in the Form, but I'm trying to make it happen through Methods.
I created a method called AddClient()
Client getClient = new Client();
public void AddClient()
{
string client = getClient.FirstName + "," + getClient.LastName + "\r\n";
File.AppendAllText(textFilePath, client);
}
And the Register button in the Form is called RegisterButton and contains :
ClientRepository getMethods = new ClientRepository();
Client getClient = new Client();
private void RegisterButton_Click(object sender, EventArgs e)
{
getClient.FirstName = FirstNameTextBox.Text;
getClient.LastName = LastNameTextBox.Text;
getMethods.AddClient();
}
FirstName and LastName ofc are properties of public class Client
After the button click, they both get null values.
Help please ?
You don't pass the instance of the Client class that has the firstName and lastName correctly set from the input textboxes to the AddClient method, instead you create a new instance of a Client class inside the ClientRepository class, and, of course, this new instance has nothing in its properties.
Change your code to pass the instance of Client created in your form
in ClientRepository class
public void AddClient(Client clientToAdd)
{
string client = clientToAdd.FirstName + "," + clientToAdd.LastName + "\r\n";
File.AppendAllText(textFilePath, client);
}
in your form code
ClientRepository getMethods = new ClientRepository();
Client clientToAdd = new Client();
....
private void RegisterButton_Click(object sender, EventArgs e)
{
clientToAdd.FirstName = FirstNameTextBox.Text;
clientToAdd.LastName = LastNameTextBox.Text;
getMethods.AddClient(clientToAdd);
}
Related
I have a form that contains two combobox (cmbSection,cmbGrade) and two textbox(txtName,txtSectionSize)
i want to get text from combobox and txtSectionSize and put it in txtName so my code lock like this
public partial class FRM_Item : Form
{
//public string State = "Add";
BL.CLS_Item prd = new BL.CLS_Item();
public FRM_Item()
{
InitializeComponent();
cmbSection.DataSource = prd.Get_All_Items();
cmbSection.DisplayMember = "Name_SectionType";
cmbSection.ValueMember = "ID_SectionType";
cmbGrade.DataSource = prd.Get_All_Grade();
cmbGrade.DisplayMember = "Name_Grade";
cmbGrade.ValueMember = "ID_Grade";
}
private void cmbSection_SelectedIndexChanged(object sender, EventArgs e)
{
this.txtName.Text = cmbSection.Text + txtSectionSize.Text + "-" + cmbGrade.Text;
}
private void cmbGrade_SelectedIndexChanged(object sender, EventArgs e)
{
this.txtName.Text = cmbSection.Text + txtSectionSize.Text + "-" + cmbGrade.Text;
}
private void txtSectionSize_TextChanged(object sender, EventArgs e)
{
this.txtName.Text = cmbSection.Text + txtSectionSize.Text + "-" + cmbGrade.Text;
}
when i open the form i get System.Data.DataRowView in txtName but when i pick up any text from combobox i get the right value in textbox
i solve this problem by moving this code to form load
private void FRM_Item_Load(object sender, EventArgs e)
{
cmbSection.DataSource = prd.Get_All_Items();
cmbSection.DisplayMember = "Name_SectionType";
cmbSection.ValueMember = "ID_SectionType";
cmbGrade.DataSource = prd.Get_All_Grade();
cmbGrade.DisplayMember = "Name_Grade";
cmbGrade.ValueMember = "ID_Grade";
}
the problem that i have now when i open this form from button in another form
the combobox always shwo the first value not the value from datagrid
private void btnEdit_Click(object sender, EventArgs e)
{
FRM_Item frm = new FRM_Item();
frm.txtName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
frm.cmbSection.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
frm.txtSectionSize.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
frm.cmbGrade.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
frm.ShowDialog();
}
how i can solve this problem
Populate the comboboxes before you assign them. Right now, its backwards.
I recommend moving this code to the child form constructor, or to a method that can be called before you try to assign properties in the parent form.
cmbSection.DataSource = prd.Get_All_Items();
cmbSection.DisplayMember = "Name_SectionType";
cmbSection.ValueMember = "ID_SectionType";
cmbGrade.DataSource = prd.Get_All_Grade();
cmbGrade.DisplayMember = "Name_Grade";
cmbGrade.ValueMember = "ID_Grade";
As far as the events if you put the code in the constructor, you could have a boolean property to not run the event code if you are not finished with the constructor OR have not have the events in the designer and do it explicitly in the constructor AFTER you populate the comboboxes.
So I am trying to do this for a hw and in my class I have this double method for converting from cm to inch:
public decimal Conversion(decimal Input)
{
return Input * (decimal)0.393701;
}
The problem is when I try to call it in a form, and I need to send arguments to it.
public partial class frmCmVoInch : Form
{
public frmCmVoInch()
{
InitializeComponent();
}
public frmCmVoInch(mainForm parent)
{
InitializeComponent();
MdiParent = parent;
}
private void btnKonverzija_Click(object sender, EventArgs e)
{
decimal parsed;
decimal.TryParse(txtVlez.Text, out parsed);
CmVoInch cmVoInch = new CmVoInch(decimal.Parse(txtVlez.Text));
rtbIzlez = txtVlez.Text + " cm = " + cmVoInch.Konverzija(parsed);
}
}
This is basically a typo:
rtbOutp = txtInp.Text + " cm = " + CmVoInch.Conversion(decimal.Parse(txtInp.Text));
Should be:
rtbOutp = txtInp.Text + " cm = " + cmVoInch.Conversion(decimal.Parse(txtInp.Text));
Note the change in case of CmVoInch. The first is the type name, so attempts to call an instance method without an instance, hence the error an object reference is required for the nonstatic field method or property. The second correctly refers to the instance of that class you created on the previous line.
You also need to change to set the text property of the RichTextBox, as you can't assign a string to it:
rtbOutp.Text = txtInp.Text + " cm = " + cmVoInch.Conversion(decimal.Parse(txtInp.Text));
but it is String, and it wont accept it even if I convert it to
decimal. Any way I can fix this?
the issue is not with the Conversion method.
the issue is the btnKonverzija_Click method returns void and you're trying to return a string value. you cannot do that.
private void btnKonverzija_Click(object sender, EventArgs e)
{
CmVoInch cmVoInch = new CmVoInch(decimal.Parse(txtInp.Text));
return txtInp.Text + " cm = " + CmVoInch.Conversion(decimal.Parse(txtInp.Text));
}
note - C# is a case-sensitive language, currently, you're calling the Converion method as if it belongs to a class rather you should use cmVoInch.Conversion not CmVoInch.Conversion.
full solution:
private void btnKonverzija_Click(object sender, EventArgs e)
{
CmVoInch cmVoInch = new CmVoInch(decimal.Parse(txtInp.Text));
rtbIzlez.Text = txtInp.Text + " cm = " + cmVoInch.Conversion(decimal.Parse(txtInp.Text));
}
i have passes some values though a constructor from one from to another. the values are successfully transferred in another form through constructor but when i display them in a textbox the values become null and the textbox remains empty. i am showing the code of the constructor for both forms.
starting form which opens another form
private void lbl_imgpass_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string img = img_name.Text;
Setimgpass st = new Setimgpass(img);
st.Show();
}
form1(from where the values are transferred to another form)
else
{
first = textBox2.Text;
second = textBox3.Text;
third = textBox4.Text;
Adduser add = new Adduser(x1low, x1up, x2low, x2up, x3low, x3up, y1low, y1up, y2low, y2up, y3low, y3up, first, second, third);
this.Hide();
}
second form(where the values are received)
public Adduser(string x1low, string x1up, string x2low, string x2up, string x3low, string x3up, string y1low, string y1up, string y2low, string y2up, string y3low, string y3up,string first, string second, string third)
{
InitializeComponent();
frst = first;
secnd = second;
thrd = third;
lowx1 = x1low;
upx1 = x1up;
lowx2 = x2low;
upx2 = x2up;
lowx3 = x3low;
upx3 = x3up;
lowy1 = y1low;
upy1 = y1up;
lowy2 = y2low;
upy2 = y2up;
lowy3 = y3low;
upy3 = y3up;
txt_imgpass.Text = frst + " " + secnd + " " + thrd;
}
txt_imgpass is the textbox in which i want values.
In the first form the values are passed to the object "add" that you created .
You can simply use static variables to pass data from one form to another .
EDIT
You might as well want to try something like this instead of static variables
Form 1 :
assuming this is the button you click on to show the second form
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2(textBox1.Text);
form.Show();
}
in Form 2 you can do something like this
public Form2(string text)
{
InitializeComponent();
label1.Text = text;
}
Working on a project for school that has 2 forms. I want to add items to the listbox in the Display form when I click the show data button. It's showing the form but the form is blank. I think this is because the form object is being created 2x once when I click the add button and another when I click the show data button. How can I create a new object of the display form that can be used in any method in my main form?
Sorry there is a few things in here that I am still working on that were just ideas. I am a beginner so please keep the help in simple terms if at all possible. Thanks :)
private void addEmployee(Employee newEmployee)
{
//Get data from textboxes and use set methods in employee class
newEmployee.Name = EmployeeNameTextBox.Text;
newEmployee.BirthDate = EmployeeBirthDateTextBox.Text;
newEmployee.Dept = EmployeeDeptTextBox.Text;
newEmployee.HireDate = EmployeeHireDateTextBox.Text;
newEmployee.Salary = EmployeeSalaryTextBox.Text;
}
private void AddButton_Click(object sender, EventArgs e)
{
//New list for employee class objects - employeelist
List<Employee> employeeList = new List<Employee>();
//Create new instance of Employee class - newEmployee
Employee newEmployee = new Employee();
bool errorCheck = false;
CheckForms(ref errorCheck);
if (!errorCheck)
{
//Gather input from text boxes and pass newEmployee object
addEmployee(newEmployee);
//Add object to employeeList
employeeList.Add(newEmployee);
Display myDisplay = new Display();
myDisplay.OutputListBox.Items.Add(" Bob");
//" " + newEmployee.BirthDate + " " +
//newEmployee.Dept + " " + newEmployee.HireDate + " " + newEmployee.Salary);
You are creating two separate instances of mydisplay.Create a single instance when the Form Loads and refer to that when you call ShowDataButton_Click
namespace WK4
{
public partial class MainForm : Form
{
Display myDisplay;
public MainForm()
{
InitializeComponent();
}
//Method to clear form input boxes
private void ClearForm()
{
EmployeeNameTextBox.Text = "";
EmployeeBirthDateTextBox.Text = "";
EmployeeDeptTextBox.Text = "";
EmployeeHireDateTextBox.Text = "";
EmployeeSalaryTextBox.Text = "";
FooterLabel.Text = "";
}
//Method to check for blank input on textboxes
private void CheckForms(ref bool error)
{
if (EmployeeNameTextBox.Text == "" || EmployeeBirthDateTextBox.Text == "")
{
MessageBox.Show("Please do not leave any fields blank");
error = true;
}
else if (EmployeeDeptTextBox.Text == "" || EmployeeHireDateTextBox.Text == "")
{
MessageBox.Show("Please do not leave any fields blank");
error = true;
}
else if (EmployeeSalaryTextBox.Text == "")
{
MessageBox.Show("Please do not leave any fields blank");
error = true;
}
else
error = false;
}
private void addEmployee(Employee newEmployee)
{
//Get data from textboxes and use set methods in employee class
newEmployee.Name = EmployeeNameTextBox.Text;
newEmployee.BirthDate = EmployeeBirthDateTextBox.Text;
newEmployee.Dept = EmployeeDeptTextBox.Text;
newEmployee.HireDate = EmployeeHireDateTextBox.Text;
newEmployee.Salary = EmployeeSalaryTextBox.Text;
}
private void AddButton_Click(object sender, EventArgs e)
{
//New list for employee class objects - employeelist
List<Employee> employeeList = new List<Employee>();
//Create new instance of Employee class - newEmployee
Employee newEmployee = new Employee();
bool errorCheck = false;
CheckForms(ref errorCheck);
if (!errorCheck)
{
//Gather input from text boxes and pass newEmployee object
addEmployee(newEmployee);
//Add object to employeeList
employeeList.Add(newEmployee);
Display myDisplay = new Display();
myDisplay.OutputListBox.Items.Add(" Bob");
//" " + newEmployee.BirthDate + " " +
//newEmployee.Dept + " " + newEmployee.HireDate + " " + newEmployee.Salary);
//Clear Form after adding data
ClearForm();
//Print footer employee saved info
FooterLabel.Text = ("Employee " + newEmployee.Name + " saved.");
}
}
//Exit the form/program
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
//Method to clear the form and reset focus
private void ClearButton_Click(object sender, EventArgs e)
{
ClearForm();
EmployeeNameTextBox.Focus();
}
private void ShowDataButton_Click(object sender, EventArgs e)
{
myDisplay.ShowDialog();
}
private void MainForm_Load(object sender, EventArgs e)
{
myDisplay = new Display();
}
}
}
You are making 2 different instances of display class, in first instance you are adding data and you are displaying it using the second instance thats why you are getting a blank form
create a display class object on your MainForm_Load
private void MainForm_Load(object sender, EventArgs e)
{
Display myDisplay = new Display();
}
and the use this object (myDisplay) to add and display data in your AddButton_Click and ShowDataButton_Click methods respectively.
I have a "simple" problem with assign variables from FORM1 (in my code Form1) and using those variables in FORM2 (in my code frLeczenie). So I started to create a public string variables:
public string wynikImie;
public string wynikUmaszczenie;
public string wynikDataUrodzenia;
public string wynikPlec;
public string wynikZnakiSzczegolne;
public string wynikCzyWykastrowane;
To those variables I'll assign data from SQL Database:
private void dgZwierze_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string zapytanie = "SELECT IMIE_ZWIERZECIA, DATA_URODZENIA, PLEC, ZNAKI_SZCZEGOLNE, UMASZCZENIE, CZY_WYKASTROWANE FROM tbZwierze WHERE tbZwierze.IMIE_ZWIERZECIA = '" + wynikImie + "' AND tbZwierze.UMASZCZENIE = '" + wynikUmaszczenie + "'";
SqlCommand cmdZapytanie = new SqlCommand(zapytanie, cs);
cs.Open();
SqlDataReader reader = cmdZapytanie.ExecuteReader();
if (reader.Read())
{
wynikImie = reader.GetValue(0).ToString();
wynikDataUrodzenia = reader.GetValue(1).ToString();
wynikPlec = reader.GetValue(2).ToString();
wynikZnakiSzczegolne = reader.GetValue(3).ToString();
wynikUmaszczenie = reader.GetValue(4).ToString();
wynikCzyWykastrowane = reader.GetValue(5).ToString();
}
cs.Close();
}
To this moment all is great, but problem occurs when I opened the FORM2:
private void btnLeczenie_Click(object sender, EventArgs e)
{
frLeczenie leczenie = new frLeczenie();
leczenie.ShowDialog();
}
Suddenly my all data assigned to public string variables is missing, and I can't using their in the FORM 2:
private void frLeczenie_Load(object sender, EventArgs e)
{
Form1 formaglowna = new Form1();
textBox1.Text = formaglowna.wynikImie;
textBox2.Text = formaglowna.wynikDataUrodzenia;
textBox3.Text = formaglowna.wynikPlec;
textBox4.Text = formaglowna.wynikZnakiSzczegolne;
textBox5.Text = formaglowna.wynikUmaszczenie;
textBox6.Text = formaglowna.wynikCzyWykastrowane;
}
What I'm doing wrong? Maybe I missing something? Could you take a look on this?
Regards,
Peter.
new Form1(); creates a new instance of your first form, but you want to use the already initialized form with it's variables instead. So you could pass the form instance via constructor to your second form and store it in a property:
in first form:
frLeczenie leczenie = new frLeczenie(this);
second form constructor:
public frLeczenie(Form1 formaglowna)
{
InitializeComponent();
this.Formaglowna = formaglowna;
// ...
}
public Form1 Formaglowna{ get; set; }
Now you access them via property:
textBox1.Text = Formaglowna.wynikImie;
// ...
Note that you set the values in an instance of the form in it's dgZwierze_CellContentClick method. However in form 2 you are creating a new Form1 that has never been shown or had that method called. So the values are empty.
Form1 formaglowna = new Form1();
textBox1.Text = formaglowna.wynikImie;
What you need is to pass the real form1 into form2 and not create a new form 1
When you create the new form1 in the last part of your code, it is a new blank instance of the class. Each instance of a class or form will have its own variable values. If you really need these variables to be available and shared on all objects of a given class, make them static.