C# message box appears twice - c#

I am trying to do a simple World of Warcraft profile picture "extractor". Here is my code:
private void pictureBox1_Click(object sender, EventArgs e)
{
if (textBox4.Text != "")
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("https://worldofwarcraft.com/en-gb/search?q=" + textBox4.Text);
webBrowser1.DocumentCompleted += GetImg;
}
}
private void GetImg(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string result = "";
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
if (el.GetAttribute("className") == "Avatar-image")
{
result = (el.OuterHtml).Substring(el.OuterHtml.IndexOf("https"));
result = result.Substring(0, result.IndexOf(")"));
pictureBox1.ImageLocation = result;
DialogResult YESNO = MessageBox.Show("Is this your character?", "Select your char", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (YESNO == DialogResult.Yes)
{
break;
}
}
}
}
The problem is that I click the image once and if the character is the right one I click "Yes" and everything is good. However if I click the image a second time the message box will pop up, then I chose my answer BUT the message box pops up again. If I am to click the image a third time, the message box will appear 3 times...
Any ideas?

You are subscribing to the event an additional time each time you click it on this line:
webBrowser1.DocumentCompleted += GetImg;

Related

I need to validate Tabcontroll in winform c#, I want show a message before selecting another tab

I want to show a message before selecting another tab if Message result is 'NO' then it should remain in the current tab if message result is YES then selected tab should open.
I have tried following code.
''
'private void tbRWINV_Selected(object sender, TabControlEventArgs e){
if (dgvSaleReturnWintoutInvoice.Rows.Count > 0)
{
DialogResult msg = new DialogResult();
msg = MessageBox.Show("The data entered for return will be lost if you move to other Tab", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (msg == DialogResult.No)
{
tbRWINV.TabIndex = 1;
}
}
}
'''
The TabControlCancelEventArgs event object has a bool Cancel property that will cancel the tab change. E.g:
public class MyForm : Form {
TabControl tc = new TabControl();
public MyForm() {
//...
tc.Selecting += tc_Selecting;
}
void tc_Selecting(object sender, TabControlCancelEventArgs e) {
DialogResult r = MessageBox.Show(this, "Are you sure you want to change tabs?", "Confirm", MessageBoxButtons.YesNo);
e.Cancel = (r == DialogResult.No);
}
}

Disable NumericUpdown C#

The student is back! I am trying to self-teach C#, so please pardon my simple but many questions. I appreciate you all.
I am working on a quiz app.What I want but cant seem to achieve is that when "Testing mode" (radio button) is selected, "Number of questions" need to be grayed out.Otherwise, student can select number of questions to attempt.
Here is my code
private void rdotesting_CheckedChanged(object sender, EventArgs e)
{
if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
MessageBox.Show("Click 'Start' to continue..");
btnclose.Hide();
}
else
{
MessageBox.Show("You Must select an option to continue.");
}
}
//if testing mode, dissable number of questions ,and also the 'Close' button
Like this - see comments
private void rdotesting_CheckedChanged(object sender, EventArgs e)
{
//this event fires when rdotesting is checked or when it is unchecked (change)
//set the enabled state of the nud/button to th opposite of the checked state
//ie when checked = true then enabled = false
numberQsNUD.Enabled = !rdotesting.Checked;
closeButton.Enabled = !rdotesting.Checked;
//if not in test mode, exit to stop the message showing every time
if(!rdotesting.Checked)
return;
if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
rdotesting.Checked = false; //user said no; turn off test mode
simply you can try this code...
private void rdotesting_CheckedChanged(object sender, EventArgs e) {
if(rdotesting.Checked) {
numberQsNUD.Enabled = false;
closeButton.Enabled = false;
} else {
numberQsNUD.Enabled = true;
closeButton.Enabled = true;
}
}
or you can customize this via using this code...
private void EnableComponent(bool check) {
numberQsNUD.Enabled = check;
closeButton.Enabled = check;
}
private void rdotesting_CheckedChanged(object sender, EventArgs e) {
if(rdotesting.Checked) {
EnableComponent(false);
} else {
EnableComponent(true);
}
}

how to implement a function when cancel button is clicked return 0 but when save is clicked return 1 and refresh grid

i have an update form which opens as showDialog. i notice when i click on save my update process goes through, closes form and refresh datagrid on parent form and when cancel button is click it closes form and also refreshes datagrid. how do i prevent datagrid from refresh when cancel button is clicked, am thinking of doing a public int declaration to return 1 when save is clicked and 0 when cancel is clicked but can't figure out how to do so. below is code for calling update form from parent form
private void kryptonDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
frmUpdate f2 = new frmUpdate();
f2.lblClientID.Text = kryptonDataGridView1.SelectedRows[0].Cells["ClientID"].Value.ToString();
f2.lblClearinAgentID.Text = kryptonDataGridView1.SelectedRows[0].Cells["Clearing_Agent_ID"].Value.ToString();
f2.textboxClientCode.Text = kryptonDataGridView1.SelectedRows[0].Cells["Client Code"].Value.ToString();
f2.txtboxClientName.Text = kryptonDataGridView1.SelectedRows[0].Cells["Client Name"].Value.ToString();
f2.txtboxPostalAddress.Text = kryptonDataGridView1.SelectedRows[0].Cells["Postal Address"].Value.ToString();
f2.txtboxTelephone.Text = kryptonDataGridView1.SelectedRows[0].Cells["Telephone"].Value.ToString();
f2.txtboxFax.Text = kryptonDataGridView1.SelectedRows[0].Cells["Fax"].Value.ToString();
f2.txtboxEmailAddress1.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 1"].Value.ToString();
f2.txtboxEmailAddress2.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 2"].Value.ToString();
f2.txtboxEmailAddress3.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 3"].Value.ToString();
f2.txtboxWebsite.Text = kryptonDataGridView1.SelectedRows[0].Cells["Website"].Value.ToString();
f2.txtboxChargeRate.Text = kryptonDataGridView1.SelectedRows[0].Cells["Charge Rate"].Value.ToString();
//f2.lblTotalDeposit.Text = kryptonDataGridView1.SelectedRows[0].Cells["Total Deposit"].Value.ToString();
//f2.lblAccountBal.Text = kryptonDataGridView1.SelectedRows[0].Cells["Account Balance"].Value.ToString();
f2.ShowDialog();
kryptonbtnDelete.Enabled = false;
private void kryptonbtnEdit_Click(object sender, EventArgs e)
{
kryptonDataGridView1_CellDoubleClick(null, null);
}
and inside update form which displays as dialog to parent form i tried something like that
private void kryptonCancel_Click(object sender, EventArgs e)
{
frmUpdate_FormClosing(null,null);
}
private void frmUpdate_FormClosing(object sender, FormClosingEventArgs e)
{
if (DialogResult != DialogResult.OK)
{
return;
}
else
{
e.Cancel = true;
}
}
dialog result from save click method
DialogResult result = MessageBox.Show("Do you want to Update this Client?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
MessageBox.Show("Client information successfully Updated", "Updating Client(s) Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
int rowsUpdated = cmd.ExecuteNonQuery();
if (rowsUpdated > 0)
{
}
}
else if (result == DialogResult.No)
{
return;
}
The return value of a call to Form.ShowDialog is a DialogResult value. This value is taken by the Form engine directly from the DialogResult property of the Button clicked. If the button has any value for this property different from DialogResult.None then this button will close the form and its DialogResult property is returned by the call to ShowDialog()
So if you have a button on your frmUpdate with its DialogResult set to OK then
if(f2.ShowDialog() == DialogResult.OK))
// User hits the OK button, refresh
else
// No refresh here...
Of course you don't need to handle yourself the closing process of the frmUpdate form. It is automatically handled by the Form engine

Click event in FlowLayoutPanel

i am programming in Visual Studio 2013, c# winform. Is it posible to choose click event for each button in FLP? Im trying to do something like Steam Library, I already did a lot of things and it now looks like i want to.
This is how it looks (adding)
This is how it looks (library)
(sorry, im not able to add images)
But i don't know how to open choosed game when you click on a button in FLP (in library).
This is how my code looks:
private void btnTest_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (textBox2.Text != "")
{
Button btn = sender as Button;
Button btnNew = new Button();
btnNew.Text = "";
btnNew.Height = 108;
btnNew.Width = 230;
btnNew.Image = new Bitmap(textBox1.Text);
btnNew.FlatStyle = FlatStyle.Flat;
flpContainer.Controls.Add(btnNew);
btnNew.Click += btnNew_Click;
counter1++;
label1.Text = counter1.ToString(); //label1 is that "Number of games in library:"
System.Windows.Forms.MessageBox.Show("Game was succesfully added to library!");
}
else if (textBox2.Text == "")
{
System.Windows.Forms.MessageBox.Show("You didn't choosed exe file!");
}
}
if (textBox1.Text =="")
{
System.Windows.Forms.MessageBox.Show("You didn't choosed image!");
}
}
private void btnNew_Click(object sender, EventArgs e)
{
Process.Start(textBox2.Text); //textbox2 is the path to exe file, but it change when you want to add another game to library
}
But how to do different click event for each button in FlowLayoutPanel?
Thank you for answers.
EDIT: I want to do that, when you click on a button in library it will open that game (program).
Thank you a lot!
Store the required information in Tag property for future use and get the job done.
Button btnNew = new Button();
btnNew.Text = "";
btnNew.Height = 108;
btnNew.Width = 230;
btnNew.Image = new Bitmap(textBox1.Text);
btnNew.FlatStyle = FlatStyle.Flat;
flpContainer.Controls.Add(btnNew);
btnNew.Click += btnNew_Click;
btnNew.Tag = textBox2.Text;// <--Store it in Tag
Then use it in Click event
private void btnNew_Click(object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
Process.Start((string)clickedButton.Tag);
}

Action taken when Enter key is pressed. But nothing happens. No errors

private void button1_Click(object sender, EventArgs e)
{
DialogResult dResult;
dResult = MessageBox.Show("You have entered: " + textBox1.Text, "Message Box Info", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Information);
label1.Text = "You clicked " + dResult.ToString();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(this, KeyEventArgs.Empty);
}
}
This is my code to see if the enter key was pressed on a text box, and i want it to click the button when the enter key is pressed. I run the program, and nothing happens. Im new to C# as you can tell. Any help or comments would be helpful.
Thank you in advance.
You can use a shared method:
public void SharedMethod()
{
DialogResult dResult;
dResult = MessageBox.Show("You have entered: " + textBox1.Text, "Message Box Info", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Information);
label1.Text = "You clicked " + dResult.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
SharedMethod();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SharedMethod();
}
}
You should definitely refactor your code the way pcnThird did it, but I recreated your code in an otherwise empty WinForms app and it worked just fine, even with passing KeyEventArgs.Empty.
The way you've got it, the code should run.
I suspect your KeyDown event is not actually attached to textBox1_KeyDown.
Check your properties window at design-time to make sure it's actually attached.
Or temporarily, type the following into your form's constructor:
this.textBox1.KeyDown += new KeyEventHandler(this.textBox1_KeyDown);

Categories