Text property not changing when the event is triggered - c#

I'm making a quick quiz game in c# and I'm struggling at making the label (questionLabel) and the buttons (ans1 - ans4) display the specified text, when the play button has been clicked, thank you in advance.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int pointCounter = 0;
private SoundPlayer _soundPlayer;
int questionNr = 1;
public Form1()
{
InitializeComponent();
_soundPlayer = new SoundPlayer("song.wav");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.amazon.com/Chuck-Seasons-One-Five-Blu-ray/dp/B007AFS0N2");
}
private void Form1_Load(object sender, EventArgs e)
{
_soundPlayer.PlayLooping();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void muteButton_Click(object sender, EventArgs e)
{
if (muteButton.Text == "Mute")
{
muteButton.Text = "Unmute";
_soundPlayer.Stop();
}
else
{
muteButton.Text = "Mute";
_soundPlayer.PlayLooping();
}
}
private void button1_Click(object sender, EventArgs e)
{
ans1.Visible = true;
ans2.Visible = true;
ans3.Visible = true;
ans4.Visible = true;
playButton.Visible = false;
while (questionNr >= 2)
{
if (questionNr == 1)
{
questionLabel.Text = "What is Chuck's full name?";
ans1.Text = "fushfus";
ans2.Text = "bhfsfs";
ans3.Text = "Chuck";
ans4.Text = "sfhus";
}
}
}
private void ans3_Click(object sender, EventArgs e)
{
if (questionLabel.Text == "What is Chuck's full name?")
{
pointCounter++;
}
else
{
}
}
public void PointCounter(){
pointsLabel.Text = pointCounter.ToString();
}
}
}

If button1 is the play button then I guess you got it wrong with the while() loop.
It will never enter the while(questionNr >= 2) since questionNr is 1 from when you create the instance.
public partial class Form1 : Form
{
int pointCounter = 0;
private SoundPlayer _soundPlayer;
int questionNr = 1; <--
public Form1() { ... }
}
Change:
while (questionNr >= 2)
{
if (questionNr == 1)
{
questionLabel.Text = "What is Chuck's full name?";
ans1.Text = "fushfus";
ans2.Text = "bhfsfs";
ans3.Text = "Chuck";
ans4.Text = "sfhus";
}
}
To:
if (questionNr == 1)
{
questionLabel.Text = "What is Chuck's full name?";
ans1.Text = "fushfus";
ans2.Text = "bhfsfs";
ans3.Text = "Chuck";
ans4.Text = "sfhus";
}

Related

How to make two forms receive information from the serial port that receives information from Arduino?

I am developing a graphical interface that reads variables from different sensors that arrive through the serial port, so far I have managed to read all the variables with a single form but now I want to have two forms.
The first form asks the user to choose the COM and confirms whether the connection was successful or not. Once the connection is successful, the second form is opened where the variables from the sensors will be shown in "labels", the readings sent from Arduino are read by the serial port and stored in an array:
data[0], data[1], data[2] etc...
This is the first form where Serialport1 is found and the data arrives through the serialPort1_DataReceived event:
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.IO.Ports;
using System.Media;
namespace AUTOCLAVES_GUI
{
public partial class GUI_AUTOCLAVES_GENERADOR_DE_VAPOR : Form
{
/*VARIABLES GLOBALES*/
string puerto_seleccionado;
public GUI_AUTOCLAVES_GENERADOR_DE_VAPOR()
{
InitializeComponent();
string[] puertos = SerialPort.GetPortNames();
foreach (string mostrar in puertos)
{
comboBox1.Items.Add(mostrar);
}
}
private void Salir_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Minimizar_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
private void Maximizar_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
Maximizar.Visible = false;
Restaurar.Visible = true;
}
private void Restaurar_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Normal;
Restaurar.Visible = false;
Maximizar.Visible = true;
}
private void MenuSideBar_Click(object sender, EventArgs e)
{
if (Sidebar.Width == 270)
{
Sidebar.Visible = false;
Sidebar.Width = 68;
SidebarWrapper.Width = 90;
LineaSidebar.Width = 68;
AnimacionSidebar.Show(Sidebar);
}
else
{
Sidebar.Visible = false;
Sidebar.Width = 270;
SidebarWrapper.Width = 300;
LineaSidebar.Width = 268;
AnimacionSidebarBack.Show(Sidebar);
}
}
private void bunifuFlatButton8_Click(object sender, EventArgs e)
{
try
{
serialPort1.Close();
serialPort1.Dispose();
serialPort1.Open();
CheckForIllegalCrossThreadCalls = false;
label2.Text = "CONEXIÓN EXITOSA";
label2.ForeColor = Color.Green;
label2.Font = new Font(label2.Font, FontStyle.Bold);
openChildForm(new MUESTREO_EN_TIEMPO_REAL());
}
catch
{
label2.Text = "CONEXIÓN FALLIDA";
label2.ForeColor = Color.Red;
label2.Font = new Font(label2.Font, FontStyle.Bold);
MessageBox.Show("REVISE CONEXIÓN DE ARDUINO", "ADVERTENCIA", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
puerto_seleccionado = comboBox1.Text;
serialPort1.PortName = puerto_seleccionado;
}
private void bunifuFlatButton7_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
label2.Text = "SIN CONEXIÓN";
label2.ForeColor = Color.White;
string[] puertos = SerialPort.GetPortNames();
foreach (string mostrar in puertos)
{
comboBox1.Items.Add(mostrar);
}
}
private Form activeForm = null;
private void openChildForm(Form childForm)
{
if (activeForm != null)
activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panel1.Controls.Add(childForm);
panel1.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
private void bunifuFlatButton2_Click(object sender, EventArgs e)
{
openChildForm(new MUESTREO_EN_TIEMPO_REAL());
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string[] data = serialPort1.ReadLine().Split(',');
if (data.Length > 10)
{
}
else
{
MessageBox.Show("Intente nuevamente", "ADVERTENCIA", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
}
}
}
Here is my second form:
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 AUTOCLAVES_GUI
{
public partial class MUESTREO_EN_TIEMPO_REAL : Form
{
public MUESTREO_EN_TIEMPO_REAL()
{
InitializeComponent();
}
private void MUESTREO_EN_TIEMPO_REAL_Load(object sender, EventArgs e)
{
}
private void Salir_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I hope someone can help me overcome this problem.

Image Gallery on C#

What I have to do is this:
Make an image gallery that has 5 buttons which each one select a folder of images.
Other two buttons for next and previous of the folder you are in. In my line 76, it says
argument 1: cannot convert from 'System.collection.Generic.list' to string
Any ideas?
Here's an image of the console
http://postimg.org/image/nct5pwdit/
Line 76 says:
pictureBox1.Load(semestres[semac].imagen[]);
I have the same command like 6 times.
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;
class semestres
{
public List<string> imagen = new List<string>();
private int _semestre;
public int canti;
public int actual;
public int c;
public semestres(int semestre, List<string> imagenes)
{
_semestre = semestre;
imagen = imagenes;
c = imagen.Count;
actual = 0;
}
public int semestre
{
get
{
return _semestre;
}
set
{
c = imagen.Count;
}
}
public int can
{
get
{
return c;
}
set
{
c = imagen.Count;
}
}
}
namespace Visor
{
public partial class Form1 : Form
{
private int cont;
private int semac;
private int _cant;
private int next;
private List<semestres> semestres = new List<semestres>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
cont = semestres[semac].actual;
cont--;
if (cont >= 0)
{
pictureBox1.Load(semestres[semac].imagen[]);
semestres[semac].actual = cont;
}
else
{
// MessageBox("Esta es la primer imagen");
cont = 0;
semestres[semac].actual = cont;
pictureBox1.Load(semestres[semac].imagen);
}
}
private void button7_Click(object sender, EventArgs e)
{
cont = semestres[semac].actual;
next = semestres[semac].c;
cont++;
if (cont < next)
{
pictureBox1.Load(semestres[semac].imagen);
semestres[semac].actual = cont;
}
else
{
// MessageBox("Esta es la ultima imagen");
cont--;
semestres[semac].actual = cont;
pictureBox1.Load(semestres[semac].imagen(cont));
}
}
private void button2_Click(object sender, EventArgs e)
{
semac = 0;
try
{
if (semestres[0].c > 0)
{
cont = semestres[0].actual;
pictureBox1.Load(semestres[0].imagen(cont));
}
}
catch (Exception)
{
OpenFileDialog file = new OpenFileDialog();
file.InitialDirectory = #"C:\";
file.Filter = "Images (*.BMP; *.JPG; *.GIF)|*.BMP; *.JPG; *.GIF|" + "All files(*.*)|*.*";
file.FilterIndex = 1;
file.Multiselect = true;
file.RestoreDirectory = true;
file.ShowDialog();
string[] imgs = file.FileNames;
List<string> imagenes = new List<string>();
foreach (string imagen in imgs)
{
imagenes.Add(imagen);
}
semestres.Add(new semestres(1, imagenes));
pictureBox1.Load(imagenes[0]);
semestres[0].actual = 0;
cont = 0;
}
}
private void button6_Click(object sender, EventArgs e)
{
}
private void btn_3_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_sal_Click(object sender, EventArgs e)
{
}
private void btn_2_Click(object sender, EventArgs e)
{
}
}
}
So you have this:
pictureBox1.Load(semestres[semac].imagen);
Well the problem is that imagen is a List<string>. Picturebox.Load(string) takes a string, not a List<string>. So you need to get a string from that list instead of passing the whole list. One way would be:
pictureBox1.Load(semestres[semac].imagen[0]);
This would load the first image in that list.
Alternatively, you might be trying to do:
pictureBox1.Load(semestres[semac].imagen[cont]);
You just need to determine what the correct index is that you're trying to specify.

c# invokeMember click won't work

So I load a webpage, that has this in its html code:
<input style="margin-left: 140px;" name="e43X45asfaw4ybrZ34fi879234tg3e4eex" type="submit" id="submit" value="Begå kriminaliteten!" onmouseover="$('#ggg').fadeIn().delay(3000).fadeOut();">
and I use this to click it:
object o = webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("e43X45asfaw4ybrZ34fi879234tg3e4eex")[0].InvokeMember("click");
if (o != null)
{
MessageBox.Show("It worked!");
}
else
{
MessageBox.Show("It didnt work!");
}
and that code always leave it didn't work as well it didn't do anything to the webpage.
Here is my 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.Windows.Forms;
namespace MafiaspilletRankeBot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("mafiaspillet.no");
webBrowser1.ScriptErrorsSuppressed = true;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
timer1.Enabled = true;
timer1.Interval = 15000;
}
catch {
timer1.Enabled = false;
MessageBox.Show("Timer error", "Looks like there a error");
}
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
timerun.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("brukernavn")[0].SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("passord")[0].SetAttribute("value", textBox2.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("login_buton")[0].InvokeMember("click");
timer2.Enabled = true;
timer2.Interval = 15000;
timer1.Enabled = false;
}
private void timer2_Tick(object sender, EventArgs e)
{
webBrowser1.Navigate("http://mafiaspillet.no/kriminalitet3.php");
timer3.Enabled = true;
timer3.Interval = 15000;
timer2.Enabled = false;
}
private void timer3_Tick(object sender, EventArgs e)
{
object o = webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("e43X45asfaw4ybrZ34fi879234tg3e4eex")[0].InvokeMember("click");
if (o != null)
{
MessageBox.Show("It worked!");
}
else
{
MessageBox.Show("It didnt work!");
}
MessageBox.Show("Bot finnished!", "YEEY!");
timer3.Enabled = false;
}
int i = 0;
private void runtime_Tick(object sender, EventArgs e)
{
i++;
timerun.Text = i.ToString() + " Sekunder";
}
}
}
So my problem is that the InvokeMember("click") doesn't work in my public void timer3_Tick.
It's like it can't do anything, but I can't find the problem :/
you need to processing in the function WebBrowserDocumentCompletedEventArgs
here full code
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection inputCol = this.WebBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement el in inputCol)
{
if (el.GetAttribute("type").Equals("submit"))
{
el.InvokeMember("Click");
MessageBox.Show("It worked!");
}
}
}

One checkbox not responding to events c# forms

I have a problem its been bugging me for 2 days now.
Now the problem is that Uncertainty checkbox is not responding to events when I click search.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace THE_HELP
{
public partial class MainPanel : Form
{
public MainPanel()
{
InitializeComponent();
}
private void buttonSelectionSearch_Click(object sender, EventArgs e)
{
{
foreach (CheckBox chk in groupBox4.Controls)
{
chk.Checked = false;
{
if (checkBoxCCF.Checked == true)
{
tabControl1.SelectedTab = tabPage1;
}
else if (checkBoxReliabilty.Checked == true)
{
tabControl1.SelectedTab = tabPage2;
}
else if (checkBoxRisk.Checked == true)
{
tabControl1.SelectedTab = tabPage3;
}
else if (checkBoxSaftey.Checked == true)
{
tabControl1.SelectedTab = tabPage4;
}
else if (checkBoxSensitivity.Checked == true)
{
tabControl1.SelectedTab = tabPage5;
}
else if (checkBoxThroughput.Checked == true)
{
tabControl1.SelectedTab = tabPage6;
}
else if (checkBoxUncertainity.Checked == true)
{
tabControl1.SelectedTab = tabPage7;
}
}
}
}
}
private void checkBoxReliabilty_CheckedChanged(object sender, EventArgs e)
{
}
private void groupBoxReliability_Enter(object sender, EventArgs e)
{
}
}
}
CheckBox Uncertanity is not responding to events.
You could make it like this
var checkbox = new CheckBox[] {checkBoxReliability, checkBoxRisks, ... };
var tab = new TabPage[] {tabPageReliability, tabPageRisks, ... };
for(int i = 0; i < checkbox.Length; i++)
if(checkbox[i].Checked)
{
tabControl1.SelectedTab = tab[i];
break;
}

parsing data between forms to datagridview

i have a main form name fmMain
black mark is show browse file path to datagridview
and red mark is show form to datagridview.
i try to send path to datagridview and succes. here is the code
namespace tstIniF
{
public partial class fmMain : Form
{
string ConfigFileName = "app.cfg";
CFileConfig cFileConfig;
public fmMain()
{
InitializeComponent();
cFileConfig = new CFileConfig();
}
private void btnQuit_Click(object sender, EventArgs e)
{
Close();
}
private void btnDirectort_Click(object sender, EventArgs e)
{
if (dlgFolder.ShowDialog() != DialogResult.OK) return;
string s = dlgFolder.SelectedPath;
txtDirectory.Text = s;
/*p = (string)dgvConfigFile.Rows[idx++].Cells[1].Value; cFileConfig.cfgContourFile = p;
p = (string)dgvConfigFile.Rows[idx++].Cells[1].Value; cFileConfig.cfgConnectionString = p;*/
}
private void btnDirectBase_Click(object sender, EventArgs e)
{
if (dlgFile.ShowDialog() != DialogResult.OK) return;
string s = dlgFile.FileName;
int idx = 0;
dgvConfigFile.Rows[idx++].Cells[1].Value = cFileConfig.cfgBaseMapFile = s;
}
private void btnDirectCont_Click(object sender, EventArgs e)
{
if (dlgFile.ShowDialog() != DialogResult.OK) return;
string s = dlgFile.FileName;
int idx = 1;
dgvConfigFile.Rows[idx++].Cells[1].Value = cFileConfig.cfgContourFile = s;
}
private void btnDirectConn_Click(object sender, EventArgs e)
{
fConn op = new fConn();
op.ShowDialog();
}
}
}
red mark as btnDirectConn i show new form like this
and here is my form fConn
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace tstIniF
{
public partial class fConn : Form
{
public fConn()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (txtServ.Text.Trim() == "" || txtDb.Text.Trim() == "" || txtUid.Text.Trim() == "" || txtPwd.Text.Trim() == "")
{
MessageBox.Show("Mohon diisi semua field....");
}
else
{
//string textAll = this.txtServ.Text + this.txtDb.Text + this.txtUid.Text + this.txtPwd.Text;
fmMain frm = new fmMain();
frm._textBox = _textBox1;
this.Close();
//Close();
//frm.Show();
}
}
public string _textBox1
{
get { return txtServ.Text + txtDb.Text; }
}
}
}
the question is how to show data in form fConn to fmMain datagridview , i fill the fConn entry and close and back to fmMain so the result is
I would use delegate to handle this,
change fConnform as below
public partial class fConn : Form
{
public SaveDelegate SaveCallback;
public fConn()
{
InitializeComponent();
}
public void btnSave_Click(object sender, EventArgs e)
{
SaveCallback("set text what you need to send to main form here...");
}
}
And fmMain as below
public delegate void SaveDelegate(string text);
public partial class fmMain
{
public fmMain()
{
InitializeComponent();
}
private void btnDirectConn_Click(object sender, EventArgs e)
{
fConn op = new fConn();
op.SaveCallback += new SaveDelegate(this.SavemCallback);
op.ShowDialog();
}
private void SavemCallback(string text)
{
// you have text from fConn here ....
}
In the frmMain declare the fConn form at the class level so that it won't get disposed when the form closes. Now the frmMain can access any public objects in fConn.
Don't re-declare frmMain in fConn. Use _textBox = op._textBox1 right after op.ShowDialog();

Categories

Resources