How to access Parameter Value of public method inside private method - c#

can I pass EditQuestionMaster(int qid_value) this qid_value ,within this private void button1_Click(object sender, EventArgs e) button click ,if yes than how can i do this so that I can UpdateQuestion properly
public partial class EditQuestionMaster : Form
{
DbHandling db = new DbHandling();
public EditQuestionMaster(int qid_value)
{
InitializeComponent();
string subNtop = db.GetEditSubNTopic(qid_value);
string[] subNtopData = subNtop.Split('~');
cmbSubject.Text = subNtopData[2];
}
private void button1_Click(object sender, EventArgs e)
{
int qid = ; //here i want the value of int qid_value
string AnsOp = "";
if (radioButton1.Checked == true)
AnsOp = "1";
else if (radioButton2.Checked == true)
AnsOp = "2";
else if (radioButton3.Checked == true)
AnsOp = "3";
else if (radioButton4.Checked == true)
AnsOp = "4";
else
{
MessageBox.Show("Answer Option Not Selected");
return;
}
string Marks = cmbMarks.SelectedItem.ToString();
if (db.UpdateQuestion(qid, txtQuestion.Text, txtOption1.Text, txtOption2.Text, txtOption3.Text, txtOption4.Text, AnsOp, Marks, "T"))
MessageBox.Show("Question Updated Successfully");
else
MessageBox.Show("Failed to Update Question");
}
}
thanks in advance for any help

public partial class EditQuestionMaster : Form
{
DbHandling db = new DbHandling();
int qid; // here is the class variable
public EditQuestionMaster(int qid_value)
{
InitializeComponent();
this.qid = qid_value; // set the value here
string subNtop = db.GetEditSubNTopic(qid_value);
string[] subNtopData = subNtop.Split('~');
cmbSubject.Text = subNtopData[2];
}
private void button1_Click(object sender, EventArgs e)
{
qid // use it here

Expanding on chancea's answer:
You can define a business object to handle the updates like so:
public class QuestionEditor
{
DbHandling db = new DbHandling();
int questionId;
public QuestionEditor(int questionId)
{
this.questionId = questionId;
}
public void SetAnswer(string answerOption)
{
db.UpdateQuestion(qid, answerOption);
}
}
When your form is created, have it create a new instance of your business object:
public partial class EditQuestionMaster : Form
{
QuestionEditor editor;
public EditQuestionMaster(int qid_value)
{
InitializeComponent();
editor = new QuestionEditor(qid_value);
}
}
Now other methods in your form can call into the editor to perform operations.
private void button1_Click(object sender, EventArgs e)
{
string answerOption;
// switch populates answerOption
editor.SetAnswer(answerOption);
}
By moving your logic to perform updates away from the form where the user interacts is called an abstraction. This abstraction allows the UI code to evolve somewhat independently of how the question data is accessed/stored. This is the basis for n-Tier architecture.

Related

The name "svar" does not exist in the current context

I'm very new to programming and I am writing a short hangman game for my programming class, I have two private voids, one when you change the text in the textbox for the correct answer and one for when you guess a character. I need to transfer the variable "svar" from the first instance to the other, when I try to use the variable "svar" in the second instance I get the error message "The name "svar" does not exist in the current context"
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TbxSvar_TextChanged(object sender, EventArgs e)
{
if (tbxSvar.TextLength == 6)
{
pbxGubbe.Top = 6;
tbxVisa.Text = "??????";
tbxGissa.Enabled = true;
string svar = tbxSvar.Text;
tbxSvar.Text = "";
}
else
{
tbxVisa.Text = "";
}
}
private void TbxGissa_TextChanged(object sender, EventArgs e)
{
if (tbxGissa.Text == "") return;
string gissning = tbxGissa.Text;
int index = svar.indexOf(gissning);
}
}
You have defined svar as a variable in a method so it won't be visible elsewhere (unless you pass it as a method argument). Instead define it as a field in your class.
public partial class Form1 : Form
{
string svar; // <----------- place here. Now it is a 'field'
public Form1()
{
InitializeComponent();
}
private void TbxSvar_TextChanged(object sender, EventArgs e)
{
if (tbxSvar.TextLength == 6)
{
pbxGubbe.Top = 6;
tbxVisa.Text = "??????";
tbxGissa.Enabled = true;
svar = tbxSvar.Text; // <---------- use svar here
tbxSvar.Text = "";
}
else
{
tbxVisa.Text = "";
}
}
private void TbxGissa_TextChanged(object sender, EventArgs e)
{
if (tbxGissa.Text == "") return;
string gissning = tbxGissa.Text;
int index = svar.indexOf(gissning); // <---------- ...and here
}
}

Returning the textbox value of another form after initializing it?

I'm trying to make my own custom input dialogue by designing a form. How would I initialize it so that once I press OK, I can receive the value of the textbox in it, back to where I initially called it?
You can create a form that exposes a property like this:
public class InputDialog:Form
{
public string Result { get; set; }
private void OK_Click(object sender, EventArgs e)
{
Result = txtResult.Text;
this.Close();
}
}
And in your base form you do:
var dialog = new InputDialog();
dialog.ShowDialog();
string Result = dialog.Result;
You can use events for communication between forms. This way InputForm hides logic, properties from outside world.
public class InputEventArgs : EventArgs
{
public string Input { get; private set; }
public InputEventArgs(string input)
{
Input = input;
}
}
public class InputDialog : Form
{
public EventHandler<InputEventArgs> InputSet;
private void OkClick(object sender, EventArgs e)
{
var ev = InputSet;
if (ev != null)
{
ev(this, new InputEventArgs(txtInput.Text));
}
}
}
and in your calling form:
private void ShowInputForm()
{
using (var frm = new InputDialog())
{
frm.InputSet += (s, e) =>
{
txtResult.Text = e.Input;
}
frm.ShowDialog();
}
}

c# - Cannot convert from void to list

So, I got kind of stuck over my head while I tried to program something new.
I'm trying to add objectBeer_pluche or objectBeer_Elektro to my OBJberenlijst on the Beren Main form from the details Form, so I can add both instances of 2 classes to the same list.
I'm not even sure this is possible by the way. So, I would like feedback if what I am trying to do is possible to start with. I already figured VOID is not right but I am really clueless here.
This is my main beren.cs form with an OBJberenlist, that's where I try to add objectBeer_pluche or objectBeer_Elektro into it:
public partial class Beren : Form
{
public interface Berenlijst { }
public List<Berenlijst> OBJberenLijst = new List<Berenlijst>();
public Beren()
{
InitializeComponent();
}
private void Beren_Load(object sender, EventArgs e)
{
}
private void BTNToevoegen_Click(object sender, EventArgs e)
{
this.Hide();
Details Details = new Details();
if (Details.ShowDialog(this) == DialogResult.OK)
{
OBJberenLijst.Add(Details.getdetails());
}
Details.Close();
Details.Dispose();
}
public void LijstLaden()
{
foreach(Beer berenobject in OBJberenLijst)
{
LST_beren.Items.Add(berenobject.Naam);
}
}
}
}
from this form called details.cs
public partial class Details : Form
{
public Details()
{
InitializeComponent();
BTN_toevoegen.DialogResult = DialogResult.OK;
BTN_cancel.DialogResult = DialogResult.Cancel;
}
private void Details_Load(object sender, EventArgs e)
{
RDB_pluche.Checked = true;
BTN_ok.Enabled = false;
}
private void RDB_pluche_CheckedChanged(object sender, EventArgs e)
{
PANEL_pluche.Visible = true;
PANEL_elektro.Visible = false;
}
private void RDB_elektro_CheckedChanged(object sender, EventArgs e)
{
PANEL_pluche.Visible = false;
PANEL_elektro.Visible = true;
}
private void BTN_toevoegen_Click(object sender, EventArgs e)
{
open_foto.Filter = "jpg (*.jpg)|*.jpg|bmp(*.bmp)|*.bmp|png(*.png)|*.png";
if (open_foto.ShowDialog() == System.Windows.Forms.DialogResult.OK && open_foto.FileName.Length > 0)
{
TXT_adres.Text = open_foto.FileName;
PIC_beer.Image = Image.FromFile(open_foto.FileName);
}
}
private void BTN_ok_Click(object sender, EventArgs e)
{
}
public void getdetails()
{
if (RDB_pluche.Enabled == true)
{
Pluche_Beer objectBeer_pluche = new Pluche_Beer(TXTNaam_pluche.Text, open_foto.FileName, "(Wasprogramma: " + TXT_wasprogramma.ToString() + " Graden Celsius");
}
else
{
Elektronische_Beer objectBeer_Elektro = new Elektronische_Beer(TXTNaam_elekro.Text, open_foto.FileName, "aantal Batterijen: " + CMBOBatterijen.ToString());
}
}
private void Details_MouseMove(object sender, MouseEventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
TextBox textBox = c as TextBox;
if (textBox.Text != string.Empty)
{
BTN_ok.Enabled = true;
}
}
}
}
}
}
The problem is between this line...
OBJberenLijst.Add(Details.getdetails());
...and this line.
public void getdetails()
List.Add() requires an object to add, but getdetails() returns void. You probably want to change getdetails() to something like the following:
public Berenlijst getdetails()
{
if (RDB_pluche.Enabled == true)
{
return new Pluche_Beer(TXTNaam_pluche.Text, open_foto.FileName, "(Wasprogramma: " + TXT_wasprogramma.ToString() + " Graden Celsius");
}
return new Elektronische_Beer(TXTNaam_elekro.Text, open_foto.FileName, "aantal Batterijen: " + CMBOBatterijen.ToString());
}
Hopefully Pluche_Beer and Elektronisch_Beer inherent from Berenlijst. Otherwise you'll have to revise your logic in a broader way.

How to pass a list generated in one form to another form (C#)? [duplicate]

This question already has answers here:
Passing Values Between Windows Forms c# [duplicate]
(2 answers)
Closed 7 years ago.
I have a 2 different forms, in one I generate a list of customers, in the other one I need to retrieve the information added into the list. How can I pass the list to my second form?
Here's the first form
List<Customers> new_customer = new List<Customers>();
private void newCustomer_Load(object sender, EventArgs e)
{
}
private void fNameTxtBox_TextChanged(object sender, EventArgs e)
{
}
private void lNameTxtBox_TextChanged(object sender, EventArgs e)
{
}
private void addressTxtBox_TextChanged(object sender, EventArgs e)
{
}
private void phoneNumTxtBox_TextChanged(object sender, EventArgs e)
{
}
private void emailTxtBox_TextChanged(object sender, EventArgs e)
{
}
private void IDTxtBox_TextChanged(object sender, EventArgs e)
{
}
private void addNewCustButton_Click(object sender, EventArgs e)
{
if (fNameTxtBox.Text != "" && lNameTxtBox.Text != "" && addressTxtBox.Text != "" && phoneNumTxtBox.Text != "" && emailTxtBox.Text != "" && IDTxtBox.Text != "")
{
new_customer.Add(new Customers { FName = fNameTxtBox.Text, LName = lNameTxtBox.Text, Address = addressTxtBox.Text, phoneNum = phoneNumTxtBox.Text, emailAdd = emailTxtBox.Text, ID = int.Parse(IDTxtBox.Text) });
MessageBox.Show("Thanks for Registering");
}
else
{
MessageBox.Show("Customer not added! Please fill out the entire form!");
}
}
}
}
And here's the second form:
namespace WindowsFormsApplication1
{
public partial class Current_Customers : Form
{
public Current_Customers()
{
InitializeComponent();
}
private void currCustComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Create a new constructor of form2 like this and also create a list in second form as well.
public partial class Current_Customers : Form
{
List<Customers> new_customer = new List<Customers>();
public Current_Customers(List<Customers> customers)
{
new_customer=customers;
}
}
And when you will create object of this form in form1 do this
Current_Customers cus=new Current_Customers(new_customer);
This will pass list to second form.
You have two possible ways of doing this.
1) Make the lists public fields / Properties on both forms. If both forms exist in the same scope, they can reference each other.
2) Add the list to a third class, preferably a static class, that both forms can access. This would be my personal preference.
public static class StaticData
{
public static readonly List<Customers> _Customers = new List<Customers>();
public static List<Customers> CustomerList
{
get
{
if (_Customers.Count < 1)
{
//Load Customer data
}
return _Customers;
}
}
}
public class Form1
{
private List<Customers> new_customer = null;
public Form1()
{
this.new_customer = StaticData.CustomerList;
}
}
public class Current_Customers
{
private List<Customers> new_customer = null;
public Current_Customers()
{
this.new_customer = StaticData.CustomerList;
}
}
It has to be though that my example here is not really threadsafe and is meant to just point you in the right direction.

Access user control elements from parent

I am trying to change what is visible in my user control by checking some checkbox's in my form (the parent). I have tried changing it directly in the parent form and in a function on the child's side. From debug the property is being changed but then after the call the property seems to reset to the default value.
My code on the parent side
public partial class Program: DevExpress.XtraBars.Ribbon.RibbonForm
{
public Program()
{
InitializeComponent();
}
public class GlobalVariables
{
public static string categoryID = "1";
public static string currentPage = "Campus";
}
private void ribbonControl1_MouseDown(object sender, MouseEventArgs e)
{
RibbonControl ribbon = sender as RibbonControl;
RibbonHitInfo hitInfo = ribbon.CalcHitInfo(e.Location);
if (hitInfo.HitTest == RibbonHitTest.PageHeader)
{
System.Diagnostics.Debug.WriteLine(hitInfo.Page.Name + "");
pnlPanel.Panel2.Controls.Clear();
if(hitInfo.Page.Name == "Campus")
{
var Campus = new WindowsFormsApplication1.Campus();
pnlPanel.Panel2.Controls.Add(Campus);
GlobalVariables.currentPage = "Campus";
}
else if (hitInfo.Page.Name == "Report")
{
var Report = new WindowsFormsApplication1.Report();
pnlPanel.Panel2.Controls.Add(Report);
GlobalVariables.currentPage = "Report";
}
else if (hitInfo.Page.Name == "Admin")
{
var Admin = new WindowsFormsApplication1.Admin();
pnlPanel.Panel2.Controls.Add(Admin);
GlobalVariables.currentPage = "Admin";
}
}
}
private void Program_Load(object sender, EventArgs e)
{
var Campus = new WindowsFormsApplication1.Campus();
pnlPanel.Panel2.Controls.Add(Campus);
List<string> Category = GenerateCategory();
for(int j = 0; j<= Category.Count(); j++)
{
krpList.Items.Add(Category[j + 1]);
krpValue.Items.Add(Category[j]);
j++;
}
}
public void krpList_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = krpList.SelectedIndex;
krpValue.SelectedIndex = selectedIndex;
GlobalVariables.categoryID = krpValue.SelectedItem + "";
System.Diagnostics.Debug.WriteLine(krpValue.SelectedItem);
Reset();
}
public void Reset()
{
var Campus = new WindowsFormsApplication1.Campus();
pnlPanel.Panel2.Controls.Clear();
pnlPanel.Panel2.Controls.Add(Campus);
}
private void chkDisplay_EditValueChanged(object sender, EventArgs e)
{
string displayInfo = "";
var Campus = new WindowsFormsApplication1.Campus();
displayInfo = chkDisplay.Text;
Campus.UpdateAudit(displayInfo);
Campus.treeList1.ClearNodes();
}
}
}
User control code
public void UpdateAudit(string displayInfo) //Child Method
{
treeList1.ClearNodes(); //Adjusting property on user control element
treeList1.Columns[1].VisibleIndex = 1;
treeList1.Columns[2].VisibleIndex = 2;
//...more code similar to above
}
Any help would be appreciated.
well, here is the problem, you are using multiple instances of same form.
you should rather create a single instance and use it .
public partial class Program: DevExpress.XtraBars.Ribbon.RibbonForm {
UserControl Campus = null;
public Program()
{
InitializeComponent();
Campus = new WindowsFormsApplication1.Campus();
}
private void Program_Load(object sender, EventArgs e) {
// remove this object creation
//var Campus = new WindowsFormsApplication1.Campus();
pnlPanel.Panel2.Controls.Add(Campus);
...
}
public void Reset() {
// remove this object creation
// var Campus = new WindowsFormsApplication1.Campus();
}
private void chkDisplay_EditValueChanged(object sender, EventArgs e) {
string displayInfo = "";
// remove this object creation
// var Campus = new WindowsFormsApplication1.Campus();
}
private void ribbonControl1_MouseDown(object sender, MouseEventArgs e)
{
...
if(hitInfo.Page.Name == "Campus")
{
// remove this object creation
//var Campus = new WindowsFormsApplication1.Campus();
}
...
}
}

Categories