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
}
}
Related
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();
}
}
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.
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();
}
...
}
}
Im making few buttons(picturebox) that then you click them they change image.
I tryed this code but it always skips to else.
Images are loaded from resources.
private void pictureBox7_Click(object sender, EventArgs e)
{
if (pictureBox7.Image == KaminuSkaiciuokle.Properties.Resources.IcopalA)
{
pictureBox7.Image = KaminuSkaiciuokle.Properties.Resources.IcopalB;
}
else
{
pictureBox7.Image = KaminuSkaiciuokle.Properties.Resources.IcopalA;
}
}
Figured that out.
Insted comparing picturebox.image I set picturebox.tag and compare.
pictureBox7.Tag = "B";
if (pictureBox7.Tag.ToString() == "A")
{
pictureBox7.Image = KaminuSkaiciuokle.Properties.Resources.IcopalB;
pictureBox7.Tag = "B";
}
else
{
pictureBox7.Image = KaminuSkaiciuokle.Properties.Resources.IcopalA;
pictureBox7.Tag = "A";
}
You should keep local reference to your resources, because when you invoke KaminuSkaiciuokle.Properties.Resources... you will always get new instance of object:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap _icopalABitmap = KaminuSkaiciuokle.Properties.Resources.IcopalA;
Bitmap _icopalBBitmap = KaminuSkaiciuokle.Properties.Resources.IcopalB;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (pictureBox7.Image == _icopalABitmap)
{
pictureBox7.Image = _icopalBBitmap;
}
else
{
pictureBox7.Image = _icopalABitmap;
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox7.Image = _icopalABitmap;
}
}
please attention this code:
Bitmap _icopalABitmap = KaminuSkaiciuokle.Properties.Resources.IcopalA;
_icopalABitmap:it is name you ideal that.
KaminuSkaiciuokle: it aqual your project name.
IcopalA: is your picture name
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.