I have 2 forms. Form1 and Form5. Form1 has 2 buttons which open Form5. I want to change the visibility of Form5's button after it is shown. I have set Form5 button's modifiers to public and I've also tried this code below but it's not working:
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.button1.Visible = true;
fr5.ShowDialog();
}
public void button2_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.button1.Visible = false;
fr5.ShowDialog();
}
Edit: I've set button1.Visible = true; in Form5 Load event.
Thank you for your suggestions. :) I have solved the problem by changing the code like this:
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.Show(this);
fr5.button1.Visible = true;
}
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.Show(this);
fr5.button1.Visible = false;
}
Related
I want to design a windows form using C sharp on Visual Studio 2013.
I go through the Source from here. but did not got it properly.
for that I have 3 combobox. I want to disable combobox2 when I click on combobox1 NSSCM element and enable when click on NSSFO element.
Below is my part of code snippet:
namespace NSE_First_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MaximizeBox = false;
MinimizeBox = false;
if (true)
{
comboBox1.Items.Add(Exchange.NSSCM.ToString());
comboBox1.Items.Add(Exchange.NSSFO.ToString());
comboBox1.Items.Add(Exchange.BSSCM.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
string selectedItem = string.Empty;
ProcessValue(selectedItem);
}
public enum Exchange
{
NSSCM = 1,
NSSFO = 2,
BSSCM = 3
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
comboBox2.Enabled = false;
if (comboBox1.selectedIndex == 1)
comboBox2.Enabled = true;
}
Try this:
//This will disable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = false;
}
//This will enable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = true;
}
Because you want it on click, use the CLICK event, instead of SelectedIndexChange event.
My problem is very simple: i have a combobox in form1, i have a button that open form2 to write into a textbox the new item to add. Here my code:
Form1:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
}
Form2:
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
combobox.Items.Add(new_item);
this.Close();
}
But the new item is not added to my comobobox.
I tried to refresh th combobox but i have the same result.
Thank you.
You need to add the item to your ComboBox after closing Form2:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
comboBox1.Items.Add(new_item); //this is missing in your code
}
But a better way would be creating a public property in Form2 to pass the string back:
public string Value { get; set; }
private void btn1_Click(object sender, EventArgs e)
{
this.Value = textBox1.Text; //pass the TextBox value to the property
this.DialogResult = DialogResult.OK; // Cancel would mean you closed/canceled the
// form without pressing OK-Button (btn1)
this.Close();
}
Than in Form1 you can access the property and add the new item:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if(f2.ShowDialog() == DialogResult.OK) //check the result
{
comboBox1.Items.Add(f2.Value);//Add the new item
}
}
Assuming combo's name is combobox.
Form1:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if (f2.ShowDialog() == DialogResult.OK)
combobox.Items.Add(f2.ItemValue);
}
Form2:
public string ItemValue {get {return textBox1.Text;} };
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
this.DialogResult = DialogResult.OK;
}
I am having two forms (A and B). In form B there are many buttons having different background image. On clicking any of the button I want to change the background image of form A to the background image of the button which was clicked instantly as it is always open behind the form.
formA mai = new formA();
private void button1_Click(object sender, EventArgs e)
{
mai.BackgroundImage = button1.BackgroundImage;
}
This is the code I am using although it changes the background image it doesn't change instantly but if I will open and close the form the background image will be changed.
I don't need like that I need it to change instantly.
Add a field in formB to refer to the formA instance which you want to change its BackgroundImage; and initialize it when you call formB
formB's code-behind:
public partial class formB : Form
{
public formA owner;
public formB()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button1.BackgroundImage;
}
private void button2_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button2.BackgroundImage;
}
private void button3_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button3.BackgroundImage;
}
}
formA's code-behind:
public partial class formA : Form
{
public formA()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formB b = new formB();
b.owner = this;
b.ShowDialog();
}
}
Add this.Refresh()
formA mai = new formA();
private void button1_Click(object sender, EventArgs e)
{
mai.BackgroundImage = button1.BackgroundImage;
mai.BringToFront();
mai.Refresh();
}
Call mai.Invalidate() after setting the new image.
How can I ensure that when you click on label, application expect to press any key, and when user press key, label text change to this key char?
Ok, now i have:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool isLabelClicked = false;
private void label1_Click(object sender, EventArgs e)
{
isLabelClicked = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (isLabelClicked)
{
label1.Text = ((char)e.KeyValue).ToString();
isLabelClicked = false;
}
}
}
And if I press Up, Down, Left, Right, Tab or Enter, application not responding and still expects press key. Only when I press any other key, application works well.
If I create new project and paste the same code, all works well, for Up, Down, Left and Right also, but I need this keys for my application which I do.
This example a textBox class : when press any key show it
using System;
using System.Windows.Forms;
using System.Drawing;
namespace KeyPressDisplayTextBox {
public partial class Form1 : Form {
private TextBox textBox1;
private Label label1;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
textBox1 = new TextBox();
textBox1.Location = new Point(10,10);
textBox1.KeyPress += textBox1_KeyPress;
Controls.Add(textBox1);
label1 = new Label();
label1.Location = new Point(10, 40);
label1.BorderStyle = BorderStyle.FixedSingle;
label1.Font = new Font("Arial", 14);
Controls.Add(label1);
}
void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
label1.Text = e.KeyChar.ToString();
}
}
}
Best regards
You can handle KeyDown event on the Form and get the KeyValue as below
Try This:
You need to set the Form KeyPreview property to True to receive the key events from the Form
bool isLabelClicked = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (isLabelClicked)
{
label1.Text = ((char)e.KeyValue).ToString();
isLabelClicked = false;
}
}
private void label1_Click(object sender, EventArgs e)
{
isLabelClicked = true;
}
Firstly I had been searching about my problem and can't find \help.
So my question is I've got 3 buttons and three userControl and when I click on one button it displays usercontrol 1 but after I click button 2. I cannot get back to usercontrol 1 im stuck in usercontrol2 and the button 1 does not do anything anymore.
Here's my code:
public partial class Form2 : Form
{
UserControl1 u1;
UserControl2 u2;
UserControl3 u3;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
u1 = new UserControl1();
u1.Dock = DockStyle.Fill;
this.Controls.Add(u1);
}
private void button2_Click(object sender, EventArgs e)
{
u1.Hide();
u2 = new UserControl2();
u2.Dock = DockStyle.Fill;
this.Controls.Add(u2);
}
private void button3_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3 = new UserControl3();
u3.Dock = DockStyle.Fill;
this.Controls.Add(u3);
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
SOLVED CODE for other's who need :)
--->
enter code here
public partial class Form2 : Form
{
UserControl1 u1;
UserControl2 u2;
UserControl3 u3;
public Form2()
{
u1 = new UserControl1();
u2 = new UserControl2();
u3 = new UserControl3();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
u2.Hide();
u3.Hide();
u1.Show();
u1.Dock = DockStyle.Fill;
this.Controls.Add(u1);
}
private void button2_Click(object sender, EventArgs e)
{
u1.Hide();
u3.Hide();
u2.Show();
u2.Dock = DockStyle.Fill;
this.Controls.Add(u2);
}
private void button3_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Show();
u3.Dock = DockStyle.Fill;
this.Controls.Add(u3);
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
It seems that you should have:
u1=new UserControl1();
u2=new UserControl2();
u3=new UserControl3();
in the constructor public Form2() rather than in the event handlers. This will allow you to add
u2.Hide();
u3.Hide();
in your button1_Click handler.
You should probably also add u3.Hide() to button2_Click.
Take a look at this. I think it's absolutely clear and didn't need any further explanation.
public partial class Form1 : Form
{
private UserControl1 uc1 = new UserControl1();
private UserControl2 uc2 = new UserControl2();
private UserControl3 uc3 = new UserControl3();
public Form1()
{
InitializeComponent();
AssignedButtonClickEvents();
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected void ButtonClicked(object sender, EventArgs e)
{
Button button = sender as Button;
panel1.Controls.Clear();
if (button != null)
{
switch (button.Name)
{
case "button1":
uc1.Dock = DockStyle.Fill;
panel1.Controls.Add(uc1);
break;
case "button2":
uc2.Dock = DockStyle.Fill;
panel1.Controls.Add(uc2);
break;
case "button3":
uc3.Dock = DockStyle.Fill;
panel1.Controls.Add(uc3);
break;
default:
panel1.Controls.Clear();
break;
}
}
}
public void AssignedButtonClickEvents()
{
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
{
Button button = (Button)ctl;
button.Click += new EventHandler(ButtonClicked);
}
}
}
edit
note that i create a panel to store the usercontrols, but i think it's the same if you show your user controls directly on the windows forms. you only need to hide your controls.
If you use BringToFront() property when clicking the button, the functionality would happen. This method will be effective for response button clicks other than Hide() property.
private void buttonProdutsList_Click(object sender, EventArgs e)
{
productsListView.BringToFront();
}