1 button, 2 webpages, but one webpage at a time? - c#

Here's what H have so far:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
}
private void button3_Click(object sender, EventArgs e)
{
audio.Stop();
if (button1.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
if (button2.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
}
}
this is only my test so far but what i want to do is change what button 3 does, i.e. if button 1 is clicked button three will open webpage 1, if button2 is clicked button 3 will open webpage 2, button 3's image will change depending, but what im finding with what i have done so far is that it opens BOTH pages AT THE SAME TIME ... how to i prevent this? i have tried if, else and else if, same result every time.

Both of your buttons are enabled, you are checking to see if the buttons are enabled or disabled (clickable or not), not which one has been clicked.
also:if (button2.Enabled == true)
is nested in the first conditional, I'm not sure if that's what you want.
You can: disable buttons 1 and 2 after their clicked so that, for instance button2.Enabled will now = false; (but then you will not be able to reclick that button)
More sophisticated, but better, is to use a delegate for the button3, and assign them in your button1_Click and button2_Click events. Something like this:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
button3.Click += new EventHandler(this.Button3_Click_First);
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
button3.Click += new EventHandler(this.Button3_Click_Second);
}
void Button3_Click_First(Object sender,
EventArgs e)
{
// When the button is clicked,
// change the button text, and disable it.
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
}
void Button3_Click_Second(Object sender,
EventArgs e)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
You may also have to check and make sure an event handler was not previously assigned, in calse someone clicks button1, then button2, then button1 ect. This is described here: Removing event handlers

You can handle your problem by storing the URL of the webpage in a private field, setting it when buttons 1 or 2 are clicked and reading from it after clicking button 3.
private string _address = null;
private void button1_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide";
}
private void button2_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide";
}
private void button3_Click(object sender, EventArgs e)
{
if (_address != null)
{
audio.Stop();
if (button1.Enabled || button2.Enabled)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start(_address);
}
}
}
I wasn't sure if all the code in button3_Click is necessary, so I cleared it up a little. I might be a bit off, though.

button.Enabled is always true for all buttons by default unless you set it to false. So you cannot use button1.Enabled property to check which button is pressed. try below approach.
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
if ((bool)ViewState["Button1Clicked"])
{
//open webpage2 code comes here
}
else
{
//open webpage2 code comes here
}
}

Related

How to make multiple objects not visible with multiple form events

I have a C# windows form project with five read only text boxes.
When a box is clicked a textbox specific list box becomes visible for selection which closes once something is selected from that listbox and written to the textbox.
I have also been able to make it so that any open listboxes are not visible, if another textbox is selected.
What I need help with is how to make sure all listboxes are not visible if anything other than the textboxes on the form is selected eg buttons, other text boxes, menus etc.
I wondered if there was a more efficient way to do this than calling a method which ensures all listboxes are not visible, from every defined event on the form .
Thanks
Apologies but should have entered more detail and oversimplified things.
In fact the text boxes activate panels with a list box and buttons which modify that listbox.
I have tried leave event handlers on both the panel and listbox but neither seem to work when you click anywhere other than the textboxes. If you put a leave event on the text boxes, you don't get the opportunity to select anything from the list box as the panel is closed.
Code is as follows
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible = false;
panel2.Visible = false;
}
private void textBox2_MouseDown(object sender, MouseEventArgs e)
{
panel1.Visible = true;
panel2.Visible = false;
}
private void textBox3_MouseDown(object sender, MouseEventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox2.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox3.Text = listBox2.SelectedItem.ToString();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
panel2.Visible = false;
}
private void listBox1_Leave(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void listBox2_Leave(object sender, EventArgs e)
{
panel2.Visible = false;
}
private void panel1_Leave(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void panel2_Leave(object sender, EventArgs e)
{
panel2.Visible = false;
}
}
}
Thanks again
You can control the visibility of multiple Controls by binding their Visible property to a common variable. In the Visual Studio designer, select the textbox and look at its properties. There is a section labeled DataBindings, and under that an (Advanced) option. Click that, and you will see a list of properties available for binding. Select the Visibility property and choose an existing boolean variable (as a Project data source) to associate with it. You can use the same variable to bind to the Visibile property of several textboxes. When your program logic sets that variable to True or False, the group of textboxes should appear/disappear accordingly.
I think I have in fact solved this with a bit of perseverence
To précis, two textboxes open individual panels with buttons and a listbox. Buttons within the panel will modify the listbox and when a selection is made in the listbox it is written to the same read only textbox. My problem was I needed to have these panels close if a selection was not made and a user went to another part of the form.
I have tried all combinations, and the solution seems to be to set focus on the panel when it is opened by textbox click event, and then have a panel leave event which makes the panel not visible. I also needed to make panels non visible with a form click event.
The final code is as follows:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible = false;
panel2.Visible = false;
}
private void textBox2_MouseDown(object sender, MouseEventArgs e)
{
panel1.Visible = true;
panel2.Visible = false;
panel1.Focus();
}
private void textBox3_MouseDown(object sender, MouseEventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
panel2.Focus();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox2.Text = listBox1.SelectedItem.ToString();
panel1.Visible = false;
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox3.Text = listBox2.SelectedItem.ToString();
panel2.Visible = false;
}
private void panel1_Leave(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void panel2_Leave(object sender, EventArgs e)
{
panel2.Visible = false;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
panel1.Visible = false;
panel2.Visible = false;
}
}
}

c# remove items when uncheck check box

When I check the checkbox in the checkboxlist, the checkbox.text will add into the listbox.
But when I uncheck the checkbox, the checkbox.text will be removed from the listbox.
But the problem is I do not know how to remove the selected items from the listbox.
For example, when i check the checkbox1, checkbox2, checkbox3, the listbox will display
checkbox1
checkbox2
checkbox3
However, when i uncheck the checkbox, Still Same
I stuck at here. Help !!
here is my code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked) listBox1.Items.Add(checkBox1.Text);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked) listBox1.Items.Add(checkBox2.Text);
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked) listBox1.Items.Add(checkBox3.Text);
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked) listBox1.Items.Add(checkBox4.Text);
}
if (checkBox3.Checked)
listBox1.Items.Add(checkBox3.Text);
else
listBox1.Items.Remove(checkBox3.Text);
Note that this will always remove whatever is in the Text property. This means that, if I check the box, change the text in textBoxX, and then uncheck, it will remove a different item.
The Items collection on a ListBox has a Remove method. Put an else in each of your CheckedChanged events and use the Remove method.
if (checkBox4.Checked) listBox1.Items.Add(checkBox4.Text);
else listBox1.Items.Remove(checkBox4.Text);
Make a common function and call. For e.g.
private void addRemove(CheckBox chk)
{
if (chk.Checked)
listBox1.Items.Add(chk.Text);
else
listBox1.Items.Remove(chk.Text);
}
Call
addRemove(checkbox1);
You can do one funciton
private void checkBoxCheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
if (cb != null)
if (cb.Checked) listBox1.Items.Add(cb.Text); else listBox1.Items.Remove(cb.Text);
}
And then add it for all your checkboxes as CheckedChanged event.
Call Remove() method. It accepts one argument that specifies the item to remove.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
listBox1.Items.Add(checkBox1.Text);
else
listBox1.Items.Remove(checkBox1.Text);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
listBox1.Items.Add(checkBox2.Text);
else
listBox1.Items.Remove(checkBox2.Text);
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
listBox1.Items.Add(checkBox3.Text);
else
listBox1.Items.Remove(checkBox3.Text);
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked)
listBox1.Items.Add(checkBox4.Text);
else
listBox1.Items.Remove(checkBox4.Text);
}
If thats all you want to do why not assign the same event to every checkbox and do the below:-
CheckBox chkBox=(CheckBox)sender;
if (chkBox.Checked)
listBox1.Items.Add(chkBox.Text);
else
listBox1.Items.Remove(chkBox.Text);

C# How to make another button appear by clicking a different button?

I want to click on a button and make a text block and another button appear. I managed to make the text block appear. How do I make the button appear. I can't seem to find a function that does so. Also, does it need to be a different private void statement. So far I have:
private void one_Click(object sender, RoutedEventArgs e)
{
oneBlock.Text = "one";
}
private void one_Click(object sender, RoutedEventArgs e)
{
one_Trans.ClickMode = "two";
}
You can do something like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
button2.Visibility = Visibility.Visible;
}
XAML:
<Button x:Name="button2" Content="Button" Visibility="Collapsed"/>
More here: http://msdn.microsoft.com/en-us/library/system.windows.visibility(v=vs.95).aspx
use
protected void button1_Click(object sender, EventArgs e)
{
button2.Visible=true;
}
on the click event of button1
Put everything into the same event
private void one_Click(object sender, RoutedEventArgs e)
{
oneBlock.Text = "one";
one_Trans.ClickMode = "two";
button1.Visible = true;
}

Changing the BackColor of a textbox upon entry

I have the following code for my form:
private void txt1_Enter(object sender, EventArgs e)
{
txt1.SelectAll();
txt1.BackColor = Color.LightBlue;
}
private void txt2_Enter(object sender, EventArgs e)
{
txt2.SelectAll();
txt2.BackColor = Color.LightBlue;
}
private void txt1_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
private void txt2_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
There are another 20 textboxes on my form that I would like to do the same for. Is it possible to combine all of the enter events and all of the leave events so I have two events in total rather than 44 individual events?
In your Designer view, select each textbox and set the Enter and Leave events to point to a single implementation of each.
Then you can do this:
private void txt_enter(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.LightBlue;
}
private void txt_leave(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.White;
}
Also, SelectAll isn't required because you're setting the entire textbox's background color.. not the SelectionColor of a RichTextBox.
You could either add manually or iterate over all textboxes in form (extension method found here GetChildControls.
foreach (TextBox textBox in this.GetChildControls<TextBox>())
{
textBox.Enter += new EventHandler(TextBox_Enter);
textBox.Leave += new EventHandler(TextBox_Leave);
}
The above can be called from the Form's Load event.
The event listener now can look like the following by casting the sender to TextBox.
private void TextBox_Enter(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox .SelectAll();
txtBox .BackColor = Color.LightBlue;
}
private void TextBox_Leave(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox.BackColor = Color.White;
}
It is, just use something like the following:
private void tbLeave(object sender, EventArgs e) {
((TextBox) sender).BackColor = Color.White;
}
The set the controls event declaration to point to this function.
You can also do the same for the Leave() event.
(Just a little note to say, I much prefer to handle this kind of thing client side where possible.)

Display a message in the font type and size selected by the user from two listboxes

I have been working on this project for a few days, it’s a C# Windows Visual Studio 2010 form and I have been posting different questions that relate to the same project; as I was told to post different questions instead on having them all in the same post. So this is the project: create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. After the user clicks a button, display "Hello" in the selected font and size.
This time I’m having a problem getting the message in the textbox to display according to the font type and size that the user selected. Here is where I’m at in the coding:
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
remove this method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
in the button_click event of your button, add this :
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
you might want to remove the selectedindexchanged methods in your code if you are going to use a button tho. depends on what you want.
edit:
public Form2()
{
InitializeComponent();
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
if you just use the above code everything should work as you want it to. I tried it out myself and it's working fine for me
This was my final submission. Thanks for all of the advice guys.
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}

Categories

Resources