Get Date in Text Box from month calendar with button in c# - c#

I want to use month calendar date in text box after click on button for window form.
Calendar should be hide when form open and after click on button that calendar be visible.
I am using c# with visual studio 2008.So please give me response according this tool.

Use the DateTimePicker class for this. It has exactly the behavior you described.

Try to use Control.Show() method and Control.Visible property:
private MonthCalendar _monthCalendar;
public Form1()
{
InitializeComponent();
// invisible on startup
_monthCalendar.Visible = false;
_monthCalendar.MaxSelectionCount = 1;
}
private void button1_Click(object sender, EventArgs e)
{
//show when needed
_monthCalendar.Show();
}
private void textBox1_Enter(object sender, EventArgs e)
{
_monthCalendar.Show();
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (!_monthCalendar.Focused)
_monthCalendar.Visible = false;
}
private void monthCalendar_DateSelected(object sender, DateRangeEventArgs e)
{
var monthCalendar = sender as MonthCalendar;
textBox1.Text = monthCalendar.SelectionStart.ToString();
}
private void monthCalendar_Leave(object sender, EventArgs e)
{
var monthCalendar = sender as MonthCalendar;
monthCalendar.Visible = false;
}

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

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

How to select all text in textbox when it gets focus

In Windows phone, how can I select all text in Textbox when the TextBox has focus?
I try setting the get focus property of Textbox:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox .SelectAll();
}
What I see is I see all the text is being selected for 1-2 sec and then it goes back to cursor mode (i.e. 1 blink line).
I had this same problem on WPF and managed to solve it. Not sure if you can use what I used but essentially your code would look like:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox .CaptureMouse()
}
private void TextBox_GotMouseCapture(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.SelectAll();
}
private void TextBox_IsMouseCaptureWithinChanged(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.SelectAll();
}
All events hooked up to the original textbox. If this doesn't work for you, maybe you can replace CaptureMouse with CaptureTouch (and use the appropriate events). Good luck!
You can try this code,
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
String sSelectedText = mytextbox.SelectedText;
}
If user clicks on copy icon that comes after selection it will get copied, if you want to do it programmatically you can try this
DataPackage d = new DataPackage();
d.SetText(selectedText);
Clipboard.SetContent(d);
I would suggest doing the copying in some other event rather than gotfocus, as this will be triggered immediately after user taps on text field so this method will be called when there is no text actually entered.
protected override void OnStartup(StartupEventArgs e)
{
//works for tab into textbox
EventManager.RegisterClassHandler(typeof(TextBox),
TextBox.GotFocusEvent,
new RoutedEventHandler(TextBox_GotFocus));
//works for click textbox
EventManager.RegisterClassHandler(typeof(Window),
Window.GotMouseCaptureEvent,
new RoutedEventHandler(Window_MouseCapture));
base.OnStartup(e);
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).SelectAll();
}
private void Window_MouseCapture(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
textBox.SelectAll();
}

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