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
Related
I am new to C# and I want to utilize the forms with one another.
I have 2 forms. (1)MMCMLibrary_home and (2)MMCMLibrary_reserve.
In this project, I'm in the stage of changing the label background colors in Form 1 but can't seem to utilize Form 2 to process it.
These are my necessary codes so far:
FORM 1
namespace MMCM_Library
{
public partial class MMCMLibrary_home : Form
{
public static MMCMLibrary_home instance;
//DCR1 Labels
public Label lbl1_1;
public Label lbl1_2;
public Label lbl1_3;
public Label lbl1_4;
public MMCMLibrary_home()
{
InitializeComponent();
instance = this;
lbl1_1 = lblDCR1_9;
lbl1_2 = lblDCR2_11;
lbl1_3 = lblDCR1_1;
lbl1_4 = lblDCR1_3;
public void btnDCR1_Click(object sender, EventArgs e)
{
var reserveDCR1 = new MMCMLibrary_reserve();
reserveDCR1.Show();
}
public void btnDCR2_Click(object sender, EventArgs e)
{
var reserveDCR2 = new MMCMLibrary_reserve();
reserveDCR2.Show();
}
public void btnDCR3_Click(object sender, EventArgs e)
{
var reserveDCR3 = new MMCMLibrary_reserve();
reserveDCR3.Show();
}
public void btnDCR4_Click(object sender, EventArgs e)
{
var reserveDCR4 = new MMCMLibrary_reserve();
reserveDCR4.Show();
}
}
}
FORM 2
when I click any reserve now button in form 1 it will open form 2. However, if I pick a radio button, the background change will always be applied to Discussion Room 1 even I reserved for discussion room 2
namespace MMCM_Library
{
public partial class MMCMLibrary_reserve : Form
{
public static MMCMLibrary_reserve instance;
public MMCMLibrary_reserve()
{
InitializeComponent();
instance = this;
}
private void MMCMLibrary_reserve_Load(object sender, EventArgs e)
{
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{
}
private void btnDCR1_Click(object sender, EventArgs e)
{
}
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
if (rbtn9.Checked)
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
}
}
}
Can you help me to device an efficient way to solve this. Can you also suggest a database method suitable for my beginner project.
You've said that:
the background change will always be applied to Discussion Room 1
Well, yes. It seems you don't pass specific object into Form2. There's strightforward:
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
So you'll be always changing backcolor of lbl1_1 of your Form1. What needs to be done is to indicate which room has been selected. You can assign room after you click the button by passing the int parameter:
public void btnDCR1_Click(object sender, EventArgs e)
{
var reserveDCR1 = new MMCMLibrary_reserve(1);
reserveDCR1.Show();
}
or:
public void btnDCR2_Click(object sender, EventArgs e)
{
var reserveDCR2 = new MMCMLibrary_reserve(2);
reserveDCR2.Show();
}
Then, in Form2, at the very top, add something like:
int room; to be able to assign the room number in Form2:
public MMCMLibrary_reserve(int roomNumber)
{
InitializeComponent();
room = roomNumber;
instance = this;
}
And then you could just select room you clicked, by:
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
if (rbtn9.Checked)
{
if(room == 1)
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
else if(room == 2)
{
MMCMLibrary_home.instance.lbl1_2.BackColor = System.Drawing.Color.Red;
}
else if(room == 3)
{
MMCMLibrary_home.instance.lbl1_3.BackColor = System.Drawing.Color.Red;
}
else if(room == 4)
{
MMCMLibrary_home.instance.lbl1_4.BackColor = System.Drawing.Color.Red;
}
}
else if(radioButton2.Checked)
{
//etc.
}
else if(radioButton3.Checked)
{
//etc.
}
else if(radioButton4.Checked)
{
//etc.
}
}
I think that was the problem. Try it and let us know.
First of all, I'm new to C# and .NET development. I was working on a console app and decided to switch to graphical using WPF. Everything was fine with the console app but I'm having troubles right now. So basically, I have this window:
Window 1
When I click on the "Add Task" button, this new window opens: Window 2. I want to perform a sequential series of save tasks (the app is made to copy directories and eventually encrypt them if the users wants to) and in order to do that, I'm saving all the copy parameters in lists of string in a third class where there's a method to run through them and execute copies.
What I want to do is display all the information the user entered in the Window1 in the datagrid, select the save tasks I want to perform and then call the method that copies when I click on the "Launch Save" button but I can't figure how. I can't retrieve the data stored in the lists in the third class with the Window2 from the Window1, it seems even like I can't store multiple save parameters in the lists I made, only one at a time. Tbh idk what to do, I've been searching for hours for a way to do that but I don't find a clue. I'm pretty sure that the way I'm coding is wrong and that I'm missing some logic/reasoning things that are important (or just a lack of knowledge idk) so that's why I came here asking for help.
Here's the code of the window1:
public partial class NewSave : Window
{
SeqSave sqSv1 = new SeqSave();
public NewSave()
{
InitializeComponent();
List<SeqSave> caracSave = new List<SeqSave>();
if (sqSv1.savedNamesList.Count > 0)
{
for (int i = 0; i < sqSv1.savedNamesList.Count; i++)
{
caracSave.Add(new SeqSave() { SaveTaskName = sqSv1.savedNamesList[i], SourcePath = sqSv1.srcPathList[i], DestinationPath = sqSv1.dstPathList[i], SaveType = sqSv1.saveTypeList[i], Backup = sqSv1.didBackupList[i] });
}
saveListsDisplay.ItemsSource = caracSave;
}
}
private void BusinessSoftware(object sender, RoutedEventArgs e)
{
}
private void AddTask(object sender, RoutedEventArgs e)
{
AddTask aT1 = new AddTask();
aT1.Show();
}
private void saveListsDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void LaunchSave(object sender, RoutedEventArgs e)
{
//for(int i = 0; i < sqSv1.savedNamesList.Count; i++)
//{
// Console.WriteLine(sqSv1.savedNamesList[i] + "\n"
// + sqSv1.srcPathList[i] + "\n"
// + sqSv1.dstPathList[i] + "\n"
// + sqSv1.saveTypeList[i] + "\n"
// + sqSv1.didBackupList[i]);
//}
sqSv1.launchSave();
}
}
And here's the code of the Window2:
public partial class AddTask : Window
{
List<string> listExtension = new List<string>();
SeqSave sqSv1 = new SeqSave();
public AddTask()
{
InitializeComponent();
}
private void GetSourcePath(object sender, RoutedEventArgs e)
{
FolderBrowserDialog sourcePath = new FolderBrowserDialog();
DialogResult result = sourcePath.ShowDialog();
string strSourcePath = sourcePath.SelectedPath;
sqSv1.srcPathList.Add(strSourcePath);
DirectoryInfo dirInfo = new DirectoryInfo(strSourcePath);
foreach (FileInfo f in dirInfo.GetFiles())
{
if (!listExtension.Contains(f.Extension))
{
listExtension.Add(f.Extension);
}
}
for(int i = 0; i < listExtension.Count; i++)
{
lstB1.Items.Add(listExtension[i]);
}
}
private void GetDestinationPath(object sender, RoutedEventArgs e)
{
FolderBrowserDialog destinationPath = new FolderBrowserDialog();
DialogResult result = destinationPath.ShowDialog();
string strDestinationPath = destinationPath.SelectedPath;
sqSv1.dstPathList.Add(strDestinationPath);
}
private void Encrypt(object sender, RoutedEventArgs e)
{
}
private void Return(object sender, RoutedEventArgs e)
{
}
private void Confirm(object sender, RoutedEventArgs e)
{
sqSv1.savedNamesList.Add(taskNameProject.Text);
if (RadioButton1.IsChecked == true)
{
sqSv1.saveTypeList.Add("1");
}
else if(RadioButton2.IsChecked == true)
{
sqSv1.saveTypeList.Add("2");
}
if (Checkbox1.IsChecked == true)
{
sqSv1.didBackupList.Add(true);
}
else
{
sqSv1.didBackupList.Add(false);
}
MessageBox.Show("Task successfully added.");
this.Visibility = Visibility.Hidden;
}
}
You need to create public properties in your second form. I added public to your code like this. I also added dialog results to both forms.
public partial class AddTask : Window
{
public List<string> ListExtension { get; } = new List<string>();
public SeqSave SqSv1 { get; }= new SeqSave();
public AddTask()
{
InitializeComponent();
}
private void GetSourcePath(object sender, RoutedEventArgs e)
{
FolderBrowserDialog sourcePath = new FolderBrowserDialog();
DialogResult result = sourcePath.ShowDialog();
string strSourcePath = sourcePath.SelectedPath;
SqSv1.srcPathList.Add(strSourcePath);
DirectoryInfo dirInfo = new DirectoryInfo(strSourcePath);
foreach (FileInfo f in dirInfo.GetFiles())
{
if (!ListExtension.Contains(f.Extension))
{
ListExtension.Add(f.Extension);
}
}
for(int i = 0; i < ListExtension.Count; i++)
{
lstB1.Items.Add(ListExtension[i]);
}
}
private void GetDestinationPath(object sender, RoutedEventArgs e)
{
FolderBrowserDialog destinationPath = new FolderBrowserDialog();
DialogResult result = destinationPath.ShowDialog();
string strDestinationPath = destinationPath.SelectedPath;
SqSv1.dstPathList.Add(strDestinationPath);
}
private void Encrypt(object sender, RoutedEventArgs e)
{
}
private void Return(object sender, RoutedEventArgs e)
{
}
private void Confirm(object sender, RoutedEventArgs e)
{
SqSv1.savedNamesList.Add(taskNameProject.Text);
if (RadioButton1.IsChecked == true)
{
SqSv1.saveTypeList.Add("1");
}else if(RadioButton2.IsChecked == true)
{
SqSv1.saveTypeList.Add("2");
}
if (Checkbox1.IsChecked == true)
{
SqSv1.didBackupList.Add(true);
}
else
{
SqSv1.didBackupList.Add(false);
}
MessageBox.Show("Task successfully added.");
this.Close();
DialogResult = DialogResult.OK;
}
}
And then access those in the original form like this:
private void AddTask(object sender, RoutedEventArgs e)
{
AddTask aT1 = new AddTask();
DialogResult results = aT1.ShowDialog();
if(results == DialogResult.OK)
{
List<string> listExt = aT1.ListExtension;
}
}
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
}
}
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.
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.