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

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;
}

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;
}
}
}

Invoking google search button

My application has one button one text box and a webbrowser control which is going to navigate at google.com at form load event.
If I am setting the text attributes to the google textbox in webBrowser1_DocumentCompleted method after clicking the button I am able to invoke google search button.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.in/?gfe_rd=cr&ei=u6PkV4X9LovZ8Aec7bI4&gws_rd=ssl");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "facebook");
}
private void btnLogin_Click(object sender, EventArgs e)
{
ele = webBrowser1.Document.All.GetElementsByName("btnK")[0];
ele.InvokeMember("click");
}
If I am setting the text attributes to the google textbox in btnLogin_Click method after clicking the button i am not able to invoke google search button.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.in/?gfe_rd=cr&ei=u6PkV4X9LovZ8Aec7bI4&gws_rd=ssl");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
}
private void btnLogin_Click(object sender, EventArgs e)
{
textElement.SetAttribute("value", "facebook");
ele = webBrowser1.Document.All.GetElementsByName("btnK")[0];
ele.InvokeMember("click");
}
Where I am going wrong please suggest me.

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

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
}
}

WPF MediaElement how to resume playing from where it pause?

i have 2 buttons , Play and Pause , When i click pause , the music stops and when i click Play it starts the audio from beginning . I want to do it like when i press Play , it resume from where i have stopped.
private void PlayAudio()
{
McMediaElement.LoadedBehavior = MediaState.Manual;
McMediaElement.Source = new Uri("../../SingAlong/Food Fit For A King/old king cole.mp3", UriKind.RelativeOrAbsolute);
McMediaElement.Play();
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
PlayAudio();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
McMediaElement.Pause();
}
Your PlayAudio() method reloads the media file when you set the Source property. This causes your object to play the newly loaded media from the beginning when you call Play(). Instead of doing this in the event handler button1_Click_1, you should call the Play() method only:
private void button1_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.Play();
}
This worked for me..
private void button1_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.LoadedBehavior = MediaState.Pause;
}
private void button2_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.LoadedBehavior = MediaState.Play;
}

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.)

Categories

Resources