How would I make a textbox appear only after clicking a button. THis means that It should be hidden, and once user clicks, then it can appear.
private void button7_Click(object sender, EventArgs e)
{
// .. what next?
}
You can use Control.Visible to make any control visible or hidden:
private void button7_Click(object sender, EventArgs e)
{
theTextBox.Visible = true;
}
Just set it's Visible property to false initially (ie: in the designer).
Assuming you have defined a TextBox textBox1 somewhere:
private void button7_Click(object sender, EventArgs e)
{
textBox1.Visible = !textBox1.Visible;
}
This way you can toggle the visibility.
You can set it just to true if you like, but make sure the initial Visible state, you can set it in the designer, is false.
Related
I Create login form and want to put "remember me" check box on it.
But every time i open program it doesn't change.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Project.Properties.Settings.Default.rememberMe = true;
Project.Properties.Settings.Default.Save();
}
else
{
Project.Properties.Settings.Default.rememberMe = false;
Project.Properties.Settings.Default.Save();
}
}
Also i want to save user login information, should i save them in app setting just like remember me setting or there is better way?
You're saving the settings, but you need to retrieve those settings too.
Subscribe to the Form's load event and set the value of the CheckBox.
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Project.Properties.Settings.Default.rememberMe;
}
Also, and this is just common practice, but your code could be shorter:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Project.Properties.Settings.Default.rememberMe = checkBox1.Checked;
Project.Properties.Settings.Default.Save();
}
I'm a beginner and have an assignment in which I must program the game of NIM. I begin with 15 "tokens" and at each turn a maximum of three can be removed, or "hidden". So far I am hiding these tokens on click by doing the following.
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
I simply copied and pasted that multiple times and changed the button numbers so that my buttons will close on click. This might be obvious, but is there a more efficient way to do this, instead of having 15 button close methods?
You can use the same click event for every single button, and make use of the sender object, casting it to Button:
private void buttonsToClose_Click(object sender, EventArgs e)
{
((Button)sender).Visible = false;
}
Then just add that handler to every single button you want to close itself on click.
Note, though, that this will throw an InvalidCastException if you or anyone else uses this handler on an object that is not a Button, so if you're actually going to use this code I would add some sort of conditional to check the real type of the sender.
Additionally, you could reuse this for any Control object by casting sender to Control instead, given that Button inherits from Control, and all Control objects have the Visible property. Here's an example, with a conditional to guard against an invalid cast:
private void controlToMakeInvisible_Click(object sender, EventArgs e)
{
if (sender.GetType() == typeof(Control))
{
((Control)sender).Visible = false;
}
}
A final note - it seems from your post like you may have a slight misunderstanding about the way events are created and wired in with objects in Windows Forms. If you go into the Designer, add a click event, and see it pop into your Form code as follows:
private void button1_Click(object sender, EventArgs e)
the name of this method has no bearing on its function. The button1 part of button1_Click doesn't actually have any logical linkage with the Button button1 - it's just the default name assigned by the Designer. The actual assignment of the method button1_Click to the Button.Click event is auto-generated into your Form's Designer.cs method.
The point of this is that if you copy and paste button1_Click and change every incidence of button1 with button2, like so:
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
it's not going to fire when button2 gets clicked. In actual fact, it's never going to fire at all, because the method hasn't actually been connected to any controls/events.
just call your event in a foreach loop.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var button in Controls.OfType<Button>())
{
button.Click += button_Click;
}
}
void button_Click(object sender, EventArgs e)
{
((Control) sender).Visible = false;
}
if you change:
Controls.OfType<Button>()
to
Controls.OfType<Control>()
it will set visible to false for any Control. so you can control what item you want the event to be raised for easily.
OfType summary: Filters the elements of an IEnumerable based on a specified type.
I am working on C# application. I have 10 radio buttons in a group panel, so now if I only checked the radioButton10 then textBox1 will be visible, if I checked someone of the other radio Buttons (radioButton1 .... radioButton9) then textBox1 should be invisible.
I wrote the following code but the textBox1 is still visible. If the code is right where can I wrote it(form load, some function ... etc) if it's NOT, then Please Help.
public TeamInfoForm()
{
InitializeComponent();
showTeam();
if (radioButton10 .Checked)
textBox1 .Visible = true;
else
textBox1 .Visible = false;
}
I think you forgot to implement the event that occur when you check or uncheck the radiobutton. Try to imlpement "OnCheckChanged" event for the radiobutton and you have to set autopostback to true if you want that the event occurs, otherwise the event won't work.
Initially you have to set the Visible property of textBox1 to false in Forms Designer. Otherwise you can set it in FormInitialize() method. Next you have write code like below
public void ToggleTextBox()
{
textBox1.Visible = radioButton3.Checked;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
ToggleTextBox();
}
i am working with split container. my split container has two panel and horizontal orientation. in first panel there are some textboxes and one button. when i click on button then a code run to collapse Panel1 of split container. code is like
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
}
my problem is when collapse occur then my button and all the textboxes getting invisible. so i next time not being able to make those control visible. so i want trick like button will not be invisible as a result i can click on that button again to make panel1 visible. if possible guide me how to fix or place my button on splitter rather on panel. so guide me how can i do it.
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
button1.Parent = splitContainer1.Panel1Collapsed ? splitContainer1.Panel2 : splitContainer1.Panel1;
}
Related to my previous comment on your entire posting. this is a small solution with a ToolBarButton. It will only be enabled if the SplitContainer.Panel1 is collapsed.
Code:
private void Form1_Load(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = true;
toolStripButton1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1.Hide();
toolStripButton1.Enabled = true;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (splitContainer1.Panel1Collapsed)
{
toolStripButton1.Enabled = false;
splitContainer1.Panel1.Show();
}
}
I have a button on my WinForms app that I want to be invisible until the user moves his mouse over the button. Then they could click it. If the mouse leaves the button, it needs to be hidden again. The button.Visible parameter makes the button completely inaccessible and disables the mouse over. Any ideas or other button parameters I could use?
This currently does not work:
private void settingButton_MouseEnter(object sender, EventArgs e)
{
settingButton.Visible = true;
}
private void settingButton_MouseLeave(object sender, EventArgs e)
{
settingButton.Visible = false;
}
This issue was brought up and answered here:
C# WinForms MouseHover and MouseLeave problem
private void Form_MouseMove(object sender, MouseEventArgs e) {
if(settingButton.Bounds.Contains(e.Location) && !settingButton.Visible) {
settingButton.Show();
}
}