|
|
I have created game with signs.Computer randomly select random combination of four signs to be found.User click button with sign and clicked sign appears in pictureBox.How to make to remove sign in pictureBox when user clicks on it and let him select sign from button again ?
|
|
|
|
|
I set one pictureBox like this, I click button and it removes picture, but when I click button again it adds picture that was before removing and ads new picture from clicked button!!!
private void pBoxIgra00_Click(object sender, EventArgs e)
{
pBoxIgra00.Image = null;
}
|
|
|
|
|
Just Update() the PictureBox after assigning a new image.
pBoxIgra00.Update();
|
|
|
|
|
It does not work.When I click again on button after remove image on first pictureBox it shows image on first pictureBox and on second pictureBox instead of only first pictureBox!!!
|
|
|
|
|
I don't understand your requirement complete - the expaining of the issue is not good
What have you allready coded around this function AND what happens exactly ?
|
|
|
|
|
I added this to pictureBox click.I click button with sign and it appears inside pictureBox,after that I click pictureBox image is removed,after I click other button with sign again it adds two images,in first pictureBox it adds image that was removed and in second pictureBox adds image that I clicked instead of just adding image that I clicked to first pictureBox !!!
private void pBoxIgra00_Click(object sender, EventArgs e)
{
pBoxIgra00.Image = null;
}
|
|
|
|
|
Show your complete code. How do you assign the Image-Property ?
When I tried at on my System it is working as awaited ...
|
|
|
|
|

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Slagalica
{
public partial class Skocko : MetroFramework.Forms.MetroForm
{
public Skocko()
{
InitializeComponent();
}
private SkockoClass objSkocko;
private void Skocko_Load(object sender, EventArgs e)
{
this.objSkocko = new SkockoClass();
}
private void pictureBox14_Click(object sender, EventArgs e)
{
}
private void pictureBox38_Click(object sender, EventArgs e)
{
}
private PictureBox findMyPictureBox(string parPrefix, int parRed, int parKolona)
{
return (PictureBox)base.Controls[string.Format("{2}{0}{1}", parRed, parKolona, parPrefix)];
}
private PictureBox findMyPictureBoxDobitna(int parBroj)
{
return (PictureBox)base.Controls[string.Format("pBoxDobitna{0}", parBroj)];
}
private void OsvezipBoxIgra()
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
if (this.objSkocko.glavnaMatrica[i, j] != 100)
{
PictureBox pictureBox = this.findMyPictureBox("pBoxIgra", i, j);
pictureBox.Image = this.imageList1.Images[this.objSkocko.glavnaMatrica[i, j]];
}
}
}
}
private void OsvezipBoxRezultat()
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
if (this.objSkocko.rezultatMatrica[i, j] != 100)
{
PictureBox pictureBox = this.findMyPictureBox("pBoxRezultat", i, j);
if (this.objSkocko.rezultatMatrica[i, j] == 1)
{
pictureBox.BackColor = Color.Yellow;
}
else
{
pictureBox.BackColor = Color.Red;
}
}
}
}
}
private void OsvezipBoxDobitna()
{
for (int i = 0; i < 4; i++)
{
PictureBox pictureBox = this.findMyPictureBoxDobitna(i);
pictureBox.Image = this.imageList1.Images[this.objSkocko.dobitnaKombinacija[i]];
}
}
private void Igraj(int parZnak)
{
this.objSkocko.Igraj(parZnak);
this.OsvezipBoxIgra();
this.OsvezipBoxRezultat();
if (this.objSkocko.Pobeda)
{
this.OsvezipBoxDobitna();
if (MessageBox.Show("POBEDILI STE!!!\nNova igra?", "POBEDA", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Restart();
}
else
{
Application.Exit();
}
}
if (this.objSkocko.trenutniPotez == 23)
{
this.OsvezipBoxDobitna();
if (MessageBox.Show("IZGUBILI STE!!!\nNova igra?", "PORAZ", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Restart();
}
else
{
Application.Exit();
}
}
}
private void btnIgrajSkocko_Click(object sender, EventArgs e)
{
this.Igraj(0);
}
private void btnIgrajZvezda_Click(object sender, EventArgs e)
{
this.Igraj(1);
}
private void btnIgrajPik_Click(object sender, EventArgs e)
{
this.Igraj(2);
}
private void btnIgrajKaro_Click(object sender, EventArgs e)
{
this.Igraj(3);
}
private void btnIgrajHerc_Click(object sender, EventArgs e)
{
this.Igraj(4);
}
private void btnIgrajTref_Click(object sender, EventArgs e)
{
this.Igraj(5);
}
}
}
Other class with code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Slagalica
{
class SkockoClass
{
public int trenutniPotez;
public int[,] glavnaMatrica;
public int[,] rezultatMatrica;
public int[] dobitnaKombinacija;
public bool Pobeda;
public SkockoClass()
{
this.Pobeda = false;
this.trenutniPotez = -1;
this.InicijalizacijaMatrice();
this.GenerisiDobitnuKombinaciju();
}
public void Igraj(int parIzabrano)
{
this.trenutniPotez++;
int num = Convert.ToInt32(Math.Floor((double)this.trenutniPotez / 4.0));
int num2 = this.trenutniPotez % 4;
this.glavnaMatrica[num, num2] = parIzabrano;
if (this.trenutniPotez % 4 == 3)
{
this.Provera(num);
}
}
public void Provera(int parRed)
{
int[] array = new int[4];
int[] array2 = new int[4];
for (int i = 0; i < 4; i++)
{
array[i] = this.glavnaMatrica[parRed, i];
array2[i] = this.dobitnaKombinacija[i];
}
int num = 0;
for (int i = 0; i < 4; i++)
{
if (array[i] == array2[i])
{
num++;
array[i] = 100;
array2[i] = 100;
}
}
int num2 = 0;
for (int i = 0; i < 4; i++)
{
if (array[i] != 100)
{
for (int j = 0; j < 4; j++)
{
if (array2[j] != 100 && array2[j] == array[i])
{
num2++;
array[i] = 100;
array2[j] = 100;
}
}
}
}
if (num == 4)
{
this.Pobeda = true;
}
this.PuniZuteCrvene(parRed, num2, num);
}
public void PuniZuteCrvene(int parRed, int parZutih, int parCrvenih)
{
int num = -1;
for (int i = 0; i <= parCrvenih - 1; i++)
{
this.rezultatMatrica[parRed, ++num] = 0;
}
for (int i = 0; i <= parZutih - 1; i++)
{
this.rezultatMatrica[parRed, ++num] = 1;
}
}
public void GenerisiDobitnuKombinaciju()
{
this.dobitnaKombinacija = new int[4];
Random random = new Random();
for (int i = 0; i < 4; i++)
{
this.dobitnaKombinacija[i] = random.Next(0, 6);
}
}
private void InicijalizacijaMatrice()
{
this.glavnaMatrica = new int[6, 4];
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
this.glavnaMatrica[i, j] = 100;
}
}
this.rezultatMatrica = new int[6, 4];
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
this.rezultatMatrica[i, j] = 100;
}
}
}
}
}
|
|
|
|
|
Sorry ... at first :
A lot of descriptions in your code are not understandable for me (because of your mother-language - Croatia ?).
What I missed was the code-part with which you have a Problem - the part where the Image-assignment is changed ...
|
|
|
|
|
public void FillCompVouType()
{
try
{
this.Cursor = Cursors.WaitCursor;
con = new SqlConnection(cs.sourceConn1);
con.Open();
scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
SqlDataReader DR = default(SqlDataReader);
vochcombox.Items.Clear();
DR = scmd.ExecuteReader();
foreach (int VouType in DR)
{
vochcombox.Items.Add(VouType);
}
while (DR.Read())
{
this.vochcombox.Items.Add(DR.GetOrdinal("VouType"));
}
DR.Close();
con.Close();
this.Cursor = Cursors.Default;
}
catch (Exception ex)
{
this.Cursor = Cursors.Default;
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
|
|
|
|
|
Which line is it complaining about? I see no explicit casts in there.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
foreach (int VouType in DR)
|
|
|
|
|
see my answer (under this) with it's question ...
|
|
|
|
|
DR = scmd.ExecuteReader();
foreach (int VouType in DR)
Are you sure that VouType is an Integer ?
|
|
|
|
|
|
Member 12899746 wrote: foreach (int VouType in DR)
{
vochcombox.Items.Add(VouType);
}
You can't use a SqlDataReader like that. Each record could contain multiple fields, and it would have no way of knowing which field you want to convert to an integer.
Member 12899746 wrote: while (DR.Read())
{
this.vochcombox.Items.Add(DR.GetOrdinal("VouType"));
}
That's closer, but still not right. GetOrdinal returns the index of the specified field, not the value of that field.
You need to use the GetInt32 method to retrieve the value of the field as an int .
You should also wrap your connection, command, and data-reader objects in using blocks.
public void FillCompVouType()
{
Cursor = Cursors.WaitCursor;
try
{
using (var connection = new SqlConnection(cs.sourceConn1))
using (var command = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", connection))
{
vochcombox.Items.Clear();
connection.Open();
using (var dr = command.ExecuteReader())
{
while (dr.Read())
{
vochcombox.Items.Add(dr.GetInt32("VouType"));
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Cursor = Cursors.Default;
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That error is telling you that he cannot convert DR object type to int.
DR type is SQLDataReader, you cannot convert data reader to int.
Why fill "vochcombox" twice?
While loop should work, but foreach doesn't for sure.
|
|
|
|
|
I first learned C# then C. As I've been working in C a lot lately, I've had to do without classes obviously since it's not really an OOP language.
I've gotten rather accustomed to passing stuff in as arguments and I'm writing a C# program right now where I've been passing "state" around via the method arguments rather than the typical storing fields/properties on the class and then accessing them from the class methods. Is there anything "wrong" about doing this? Is there anything "wrong" with not using the constructor to initialize member variables to certain values and instead just using a function in the class that calls the other business functions? E.G.
someObject myObject = new someObject();
var processResult = myObject.process(someString,Processor_Option_1);
Instead of:
someObject myObject = new someObject(someString,Processor_Option_1);
var processResult = myObject.result;
Note: Option 2 also uses class state member variables whereas 1 just passes it function-to-function as argument.
|
|
|
|
|
No, I don't think there is anything "wrong" with your code. But the way I see it (in option 2), it is clear that your constructor is not just a constructor but also a processor, it processes the operands and then puts the result in result field.
If you are going to do this, then write them like,
SomeObjectProcessorResult myObject = SomeObject.Process(someString,Processor_Option_1);
var res = myObject.result;
This will make much more sense in C#, it does not any longer depend on the states of SomeObject (it is static), but does the same — gets input, processes and then gives out output.
public class SomeObject {
public static SomeObjectProcessorResult Process(string someString, ProcessorType option) {
}
}
public class SomeObjectProcessResult {
public Type result;
}
This is a near C# definition of types, while ignoring the states.
Then in your first option, it is clear that the function is an instance one and will depend on states of object.
TheOnlyRealTodd wrote: Option 2 also uses class state member variables whereas 1 just passes it function-to-function as argument. Not so, you can ignore and from an external view I can easily think of implementation where I just process those parameters and ignore them (not save them). On the other hand, I can save those parameters in option 1 as state values.
That is the case in OOP, there are side effects everywhere. If you do not want to use side effects. Consider using F#, that is a functional programming language — but does have OOP.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Awesome thanks for the feedback.
|
|
|
|
|
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (row.Cells(this.invoiceNO.Index).Value == true)
{
a.Add(row.Cells(this.invoiceNO.Index).Value);
}
}
|
|
|
|
|
In C#, you access array elements using [] not (),
if (row.Cells[this.invoiceNO.Index].Value == true)
This will access the elements, instead of invoking it as a function.
Indexers (C# Programming Guide)[^]
Methods (C# Programming Guide)[^]
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
many thanks...but i get another error (Operator '==' cannot be applied to operands of type 'object' and 'bool')
|
|
|
|
|
|