|
i get my issue....
i need to get the value from DataGridView that was selected into array.
can you write to me example code.
|
|
|
|
|
Sorry, but I don't understand what you said: remember I only get exactly what you type to work from.
Can you explain in more detail, perhaps?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
i need code for get selected column value from datagridview into array list
//foreach (DataGridViewRow row in this.dataGridView1.Rows)
//{
// if ((((bool)(row.Cells[this.invoiceNO.Index].Value)) == true))
// {
// a.Add(row.Cells[this.invoiceNO.Index].Value);
// }
//}
|
|
|
|
|
The problem is that you aren't thinking about what is going on here: the value you are casting to a bool is not a bool value, so the cast fails: that can't be "fixed" at that point - you need to find out what it actually is and why it isn't the value you are expecting.
And the only way to do that is to use the debugger while your code is running and look at what the actual data looks like. No amount of code I could give you would fix that - because either the index is wrong, the data is wrong, the table isn't organised as you think it is, or it's totally the wrong table.
We can't tell you which of those: but the debugger probably can almost instantly!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
As long as you keep mixing "business logic" in with your "data access" logic, your life will continue to be difficult.
You are simply copying and pasting each "case", without any thought to the amount of duplicate code you are creating.
I would tell you to rewrite what you've got instead of helping you to "fix" it (and continuing to support your bad habits).
Or are you getting paid by lines-of-code?
|
|
|
|
|
Do you know in the Visual Studio programming webcam with standard library to the laptop's webcam ? what's the library name ? if attached great example.
|
|
|
|
|
You can use Google to answer this question, or search MSDN.
|
|
|
|
|
|
I created one table like this:
CREATE TABLE Pitanja
(
ID int NOT NULL IDENTITY (1,1) PRIMARY KEY,
Pitanje nvarchar(1000) NOT NULL,
Odgovor int NOT NULL,
OpcijaA nvarchar(1000) NOT NULL,
OpcijaB nvarchar(1000) NOT NULL,
OpcijaC nvarchar(1000) NOT NULL,
OpcijaD nvarchar(1000) NOT NULL
);
This is one part of table Pitanja
INSERT INTO Pitanja(Pitanje,Odgovor,OpcijaA,OpcijaB,OpcijaC,OpcijaD) VALUES('Blizu kog grada se nalaze Sremski Karlovci',3,'Rume','Sremske Mitrovice','Novog Sada','Šida');
Answers column contains number of correct choice,not word of correct choice.How to check answer when user select one of four choices from four buttons?
|
|
|
|
|
You could put the answers into an array like so:
string[] Answers = new string[4];
string UserChoice = Answers[IndexOfChoice];
Just set the variable IndexOfChoice to a number from 0 to 3 depending upon which button was clicked.
IndexOfChoice is an int.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
 Here is complete code:
int QuesionCount;
int IndexOfChoice;
DataTable dt = new DataTable();
int[] Answers = new int[4];
string cs= @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\pavle\documents\visual studio 2015\Projects\Ko zna zna\Ko zna zna\Koznazna.mdf;Integrated Security=True";
private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(cs))
{
string query = "SELECT * FROM Pitanja ORDER BY NEWID()";
SqlCommand command = new SqlCommand(query, connection);
try
{
connection.Open();
dt.Load(command.ExecuteReader());
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
setQuestion();
}
private void provera()
{
int UserChoice = Answers[IndexOfChoice];
if (answer_id == UserChoice)
{
MessageBox.Show("Odgovor je tacan!");
}
else
{
MessageBox.Show("Odgovor nije tacan!");
}
}
private void button1_Click(object sender, EventArgs e)
{
Answers[IndexOfChoice] = 1;
provera();
}
private void button2_Click(object sender, EventArgs e)
{
Answers[IndexOfChoice] = 2;
provera();
}
private void button3_Click(object sender, EventArgs e)
{
Answers[IndexOfChoice] = 3;
provera();
}
private void button4_Click(object sender, EventArgs e)
{
Answers[IndexOfChoice] = 4;
provera();
}
private void button5_Click(object sender, EventArgs e)
{
try
{
this.QuesionCount++;
setQuestion();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
int answer_id;
private void setQuestion()
{
try
{
this.label2.Text = this.dt.Rows[this.QuesionCount]["Pitanje"].ToString();
this.button1.Text = this.dt.Rows[this.QuesionCount]["OpcijaA"].ToString();
this.button2.Text = this.dt.Rows[this.QuesionCount]["OpcijaB"].ToString();
this.button3.Text = this.dt.Rows[this.QuesionCount]["OpcijaC"].ToString();
this.button4.Text = this.dt.Rows[this.QuesionCount]["OpcijaD"].ToString();
this.label3.Text = this.dt.Rows[this.QuesionCount]["Odgovor"].ToString();
answer_id = Convert.ToInt32(this.dt.Rows[this.QuesionCount]["Odgovor"]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
How to shuffle choises and keep track of the correct solution?
|
|
|
|
|
Who's writing this application, you or us? How do you think you should shuffle the cards?
This space for rent
|
|
|
|
|
I am creating new database.This is one part of it:
{"a1":"Kina","a2":"Berlin","a3":"Živi","a4":"Cigla","a5":["Zid"],"b1":"Rešetke","b2":"Kriminal","b3":"Alkatraz","b4":"Kaucija","b5":["Zatvor"],"c1":"Artiljerija","c2":"Volt","c3":"Naboj","c4":"AA","c5":["Baterija","baterije"],"d1":"Bubreg","d2":"Nadležni","d3":"Tkivo","d4":"Transplantacija","d5":["Organ"],"rr":["Ćelija","ćelijski","celija","celijski"]}
How to make all this as answer inside column Answers: ["Ćelija","ćelijski","celija","celijski"]
modified 26-Dec-16 15:10pm.
|
|
|
|
|
That is not a database. That is a fragment of JSON, and should be treated as such.
This space for rent
|
|
|
|
|
This is one part of text file:
["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"]
["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"]
["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"]
["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"]
["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"]
["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"]
I want to get text from text file between [ and ?
I tried this commands but it doesn't work:
List<string> questions = File.ReadLines(path)
.Where(x => x.StartsWith("[") &&
x.EndsWith("?"))
.ToList();
foreach (string x in questions)
{
listBox1.Items.Add(x);
}
|
|
|
|
|
x does not endwith ? it contains ?
|
|
|
|
|
Look at each line, and use IndexOf and Substring:
string s = @"[""Koje ostrvo od Italije razdvaja Ligursko more.?"",2,""Sicilija"",""Korzika"",""Sardinija"",""Kapri""]";
int start = s.IndexOf('[') + 1;
int end = s.IndexOf('?');
string bitInTheMiddle = s.Substring(start, end - start);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
How to do that for all lines?
|
|
|
|
|
StartsWith and EndsWith perform no operations on the strings themselves - they are simply boolean methods that return true or false based on what the string Starts or Ends with - so your code will return no results.
Instead, you need to perform a "Select", and extract the part of the string you require. Two possible methods come to mind immediately:
File.ReadLines(path).Select(x=>x.SubString(1, x.IndexOf("?")-1).ToList();
or
File.ReadLines(path).Select(x=>x.Split(new char[]{'[','?'}, StringSplitOption.RemoveEmptyEntries)[0]).ToList();
You could also look at using Regex, but I think it might be a bit of overkill for this case.
Cheers,
Mick
------------------------------------------------
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
|
|
|
|
|
It's working:
List<string> questions = File.ReadLines(path)
.Select(x => x.Split(new char[] { '[', '?' }, StringSplitOptions.RemoveEmptyEntries)[0]).ToList();
foreach (var x in questions)
{
listBox2.Items.Add(x);
}
How to take separatly: ,2,"Sicilija","Korzika","Sardinija","Kapri"
In first listbox to put answers: 2,1,1,1
In second listbox to put chioce1: Sicilija","travama","Germanik","Japanski običaj" and same for other three choices?
["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"]
["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"]
["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"]
["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"]
["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"]
["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"]
|
|
|
|
|
How about you give it a go using the techniques I just showed you? There is enough there to do that. Also, look up the methods I demonstrated on MSDN - that'll give you added info on how it all works. We're not here to do it all for you - hopefully pointing you in the right direction will have you learning.
Cheers,
Mick
------------------------------------------------
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
|
|
|
|
|
Check out his previous questions - he doesn't seem to want to learn.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Now that you've completely changed your requirement, it's time you started reading the documentation on these methods and started experimenting on your own on how they work.
You're trying to learn by constantly asking questions that are easy to find the answers to and copying and pasting code you find on the internet. You're not making an effort to understand the code you're using. You have to start creating small programs that test the methods you're trying to use so you can see how they work. You also have to get to know how to use the debugger. It's there to debug YOU and better you're understanding of how your code works.
|
|
|
|
|
I think yours is an excellent example of "agile programming"; i.e. no planning, just "coding".
It is obvious the leading and trailing "[]" can be eliminated.
We then wind up with a "coma-delimited" file record; which can be split using string split (6 tokens).
Then it's just a question of testing the first token for an "?".
But, hey, as long as we come up with the "right answer", then it's all good. Right?
|
|
|
|
|
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
this.trenutneKontrole[0].Image = button.Image;
this.pokusaj[0] = (int)button.Tag;
}
break;
case 2:
{
this.trenutneKontrole[1].Image = button.Image;
this.pokusaj[1] = (int)button.Tag;
}
break;
case 3:
{
this.trenutneKontrole[2].Image = button.Image;
this.pokusaj[2] = (int)button.Tag;
}
break;
case 4:
{
this.trenutneKontrole[3].Image = button.Image;
this.pokusaj[3] = (int)button.Tag;
}
break;
This is full code:
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;
using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;
private PictureBox pictureBox6;
private PictureBox pictureBox5;
private PictureBox pictureBox4;
private PictureBox pictureBox3;
private PictureBox pictureBox2;
private PictureBox pictureBox1;
private GroupBox groupBox8;
private PictureBox pictureBox55;
private PictureBox pictureBox56;
private PictureBox pictureBox57;
private PictureBox pictureBox58;
private GroupBox groupBox9;
private Label label1;
private MenuStrip menuStrip1;
private ToolStripMenuItem igraToolStripMenuItem;
private GroupBox groupBox2;
private PictureBox pictureBox47;
private PictureBox pictureBox48;
private PictureBox pictureBox49;
private PictureBox pictureBox50;
private PictureBox pictureBox51;
private PictureBox pictureBox52;
private PictureBox pictureBox53;
private PictureBox pictureBox54;
private PictureBox pictureBox39;
private PictureBox pictureBox40;
private PictureBox pictureBox41;
private PictureBox pictureBox42;
private PictureBox pictureBox43;
private PictureBox pictureBox44;
private PictureBox pictureBox45;
private PictureBox pictureBox46;
private PictureBox pictureBox31;
private PictureBox pictureBox32;
private PictureBox pictureBox33;
private PictureBox pictureBox34;
private PictureBox pictureBox35;
private PictureBox pictureBox36;
private PictureBox pictureBox37;
private PictureBox pictureBox38;
private PictureBox pictureBox23;
private PictureBox pictureBox24;
private PictureBox pictureBox25;
private PictureBox pictureBox26;
private PictureBox pictureBox27;
private PictureBox pictureBox28;
private PictureBox pictureBox29;
private PictureBox pictureBox30;
private PictureBox pictureBox15;
private PictureBox pictureBox16;
private PictureBox pictureBox17;
private PictureBox pictureBox18;
private PictureBox pictureBox19;
private PictureBox pictureBox20;
private PictureBox pictureBox21;
private PictureBox pictureBox22;
private PictureBox pictureBox11;
private PictureBox pictureBox12;
private PictureBox pictureBox13;
private PictureBox pictureBox14;
private PictureBox pictureBox10;
private PictureBox pictureBox9;
private PictureBox pictureBox8;
private PictureBox pictureBox7;
private ToolStripMenuItem prikaziTabeluToolStripMenuItem;
private ToolStripMenuItem infoToolStripMenuItem;
private ToolStripMenuItem vratiPotezToolStripMenuItem;
private int brojKlikova = 0;
private int brojPoena = 0;
private bool pocetakIgre = true;
private bool igraZavrsena = false;
private int[] pokusaj;
private SkockoClass skocko;
public PictureBox[] trenutneKontrole;
public PictureBox[] rezultatPokusajaKontrole;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
public int rbrPokusaja;
public Skocko()
{
InitializeComponent();
int[] array = new int[4];
this.pokusaj = array;
this.skocko= null;
this.trenutneKontrole = null;
this.rezultatPokusajaKontrole = null;
this.rbrPokusaja = 1;
this.InitializeComponent();
this.pictureBox1.Tag = 0;
this.pictureBox2.Tag = 1;
this.pictureBox3.Tag = 2;
this.pictureBox4.Tag = 3;
this.pictureBox5.Tag = 4;
this.pictureBox6.Tag = 5;
this.button1.Tag = 0;
this.button2.Tag = 1;
this.button3.Tag = 2;
this.button4.Tag = 3;
this.button5.Tag = 4;
this.button6.Tag = 5;
}
private void SkloniSlike()
{
for (int i = 7; i < 59; i++)
{
Control[] array = base.Controls.Find("pictureBox" + i.ToString(), true);
PictureBox pictureBox = array[0] as PictureBox;
pictureBox.Image = null;
Application.DoEvents();
Thread.Sleep(3);
}
}
private void PrikaziBodove()
{
this.label1.Text = this.brojPoena.ToString();
}
private void PrikaziRezultatPokusaja(PictureBox[] resPicBoxs, Bitmap[] resPictures)
{
for (int i = 0; i < resPicBoxs.Length; i++)
{
resPicBoxs[i].Image = resPictures[i];
}
}
private void igraToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!this.pocetakIgre && !this.igraZavrsena)
{
if (MessageBox.Show("Da li ste sigurni da zelite da pocnete novu partuju?\nBodovi ce biti smanjeni za 10 poena", "Info", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
this.brojPoena -= 10;
this.label1.Text = this.brojPoena.ToString();
}
this.skocko= new SkockoClass();
this.brojKlikova = 0;
this.rbrPokusaja = 1;
if (!this.pocetakIgre)
{
this.SkloniSlike();
}
else
{
this.pocetakIgre = false;
}
this.igraZavrsena = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void prikaziTabeluToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void infoToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Z)
{
if (this.brojKlikova > 0 && this.brojKlikova < 4)
{
this.brojKlikova--;
this.trenutneKontrole[this.brojKlikova].Image = null;
}
}
}
private void vratiPotezToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.brojKlikova > 0 && this.brojKlikova < 4)
{
this.brojKlikova--;
this.trenutneKontrole[this.brojKlikova].Image = null;
}
}
private void Command_Click(object sender, EventArgs e)
{
Button button = sender as Button;
PictureBox pictureBox = sender as PictureBox;
this.brojKlikova++;
switch (this.rbrPokusaja)
{
case 1:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox7,
this.pictureBox8,
this.pictureBox9,
this.pictureBox10
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox11,
this.pictureBox12,
this.pictureBox13,
this.pictureBox14
};
break;
case 2:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox15,
this.pictureBox16,
this.pictureBox17,
this.pictureBox18
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox19,
this.pictureBox20,
this.pictureBox21,
this.pictureBox22
};
break;
case 3:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox23,
this.pictureBox24,
this.pictureBox25,
this.pictureBox26
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox27,
this.pictureBox28,
this.pictureBox29,
this.pictureBox30
};
break;
case 4:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox31,
this.pictureBox32,
this.pictureBox33,
this.pictureBox34
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox35,
this.pictureBox36,
this.pictureBox37,
this.pictureBox38
};
break;
case 5:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox39,
this.pictureBox40,
this.pictureBox41,
this.pictureBox42
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox43,
this.pictureBox44,
this.pictureBox45,
this.pictureBox46
};
break;
case 6:
this.trenutneKontrole = new PictureBox[]
{
this.pictureBox47,
this.pictureBox48,
this.pictureBox49,
this.pictureBox50
};
this.rezultatPokusajaKontrole = new PictureBox[]
{
this.pictureBox51,
this.pictureBox52,
this.pictureBox53,
this.pictureBox54
};
break;
}
switch (this.brojKlikova)
{
case 1:
{
this.trenutneKontrole[0].Image = button.Image;
this.pokusaj[0] = (int)button.Tag;
}
break;
case 2:
{
this.trenutneKontrole[1].Image = button.Image;
this.pokusaj[1] = (int)button.Tag;
}
break;
case 3:
{
this.trenutneKontrole[2].Image = button.Image;
this.pokusaj[2] = (int)button.Tag;
}
break;
case 4:
{
this.trenutneKontrole[3].Image = button.Image;
this.pokusaj[3] = (int)button.Tag;
}
break;
}
if (this.brojKlikova == 4)
{
this.brojKlikova = 0;
this.PrikaziRezultatPokusaja(this.rezultatPokusajaKontrole, this.skocko.ProveriPokusaj(this.pokusaj));
if (this.skocko.reseno)
{
switch (this.rbrPokusaja)
{
case 1:
this.brojPoena += 100;
break;
case 2:
this.brojPoena += 90;
break;
case 3:
this.brojPoena += 80;
break;
case 4:
this.brojPoena += 70;
break;
case 5:
this.brojPoena += 60;
break;
case 6:
this.brojPoena += 50;
break;
}
this.rbrPokusaja = 1;
this.igraZavrsena = true;
this.PrikaziBodove();
this.PrikaziResenje();
}
else if (this.rbrPokusaja == 6)
{
this.igraZavrsena = true;
this.rbrPokusaja = 1;
this.brojPoena -= 10;
this.PrikaziBodove();
this.PrikaziResenje();
}
else
{
this.rbrPokusaja++;
}
}
}
private void PrikaziResenje()
{
this.PrikaziRezultatPokusaja(new PictureBox[]
{
this.pictureBox55,
this.pictureBox56,
this.pictureBox57,
this.pictureBox58
}, this.skocko.DajKombinacijuKaoNizSlika());
}
private void pictureBox47_Click(object sender, EventArgs e)
{
if (this.brojKlikova > 0 && this.brojKlikova < 4)
{
this.brojKlikova--;
this.trenutneKontrole[this.brojKlikova].Image = null;
}
}
}
}
Other class with code:
using Skocko5.Properties;
using System;
using System.Drawing;
namespace Skocko5
{
public class Igra
{
private int[] kombinacija;
public bool reseno;
public Igra()
{
int[] array = new int[4];
this.kombinacija = array;
this.reseno = false;
Random random = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < 4; i++)
{
this.kombinacija[i] = random.Next(0, 6);
}
}
public int[] DajKombinacijuKaoNizInt()
{
return this.kombinacija;
}
public Bitmap[] DajKombinacijuKaoNizSlika()
{
Bitmap[] array = new Bitmap[4];
for (int i = 0; i < this.kombinacija.Length; i++)
{
switch (this.kombinacija[i])
{
case 0:
array[i] = new Bitmap(Resources.herc);
break;
case 1:
array[i] = new Bitmap(Resources.pik);
break;
case 2:
array[i] = new Bitmap(Resources.karo);
break;
case 3:
array[i] = new Bitmap(Resources.tref);
break;
case 4:
array[i] = new Bitmap(Resources.skocko);
break;
case 5:
array[i] = new Bitmap(Resources.zvezda);
break;
}
}
return array;
}
public Bitmap[] ProveriPokusaj(int[] pokusaj)
{
int num = 0;
int num2 = 0;
int[] array = (int[])this.kombinacija.Clone();
for (int i = 0; i < pokusaj.Length; i++)
{
for (int j = 0; j < array.Length; j++)
{
if (pokusaj[i] == array[j])
{
num++;
array[j] = -1;
break;
}
}
}
for (int i = 0; i < this.kombinacija.Length; i++)
{
if (pokusaj[i] == this.kombinacija[i])
{
num2++;
}
}
int num3 = num - num2;
Bitmap[] array2 = new Bitmap[4];
for (int i = 0; i < num2; i++)
{
array2[i] = new Bitmap(Resources.pogodakPravoMesto);
}
for (int i = num2; i < num2 + num3; i++)
{
array2[i] = new Bitmap(Resources.pogodakPogresnoMesto);
}
for (int i = num2 + num3; i < this.kombinacija.Length; i++)
{
array2[i] = new Bitmap(Resources.promasaj);
}
if (num2 == 4)
{
this.reseno = true;
}
return array2;
}
private void PostaviPrvoPojavljivanjeBrojaNaMinusJedan(int broj, ref int[] niz)
{
for (int i = 0; i < niz.Length; i++)
{
if (niz[i] == broj)
{
niz[i] = -1;
break;
}
}
}
}
}
|
|
|
|
|