So I just learned Microsoft Visual. I tried to use label and trying to set a text using the event handler, but i got an error in the line this.lblText.Text, more specifically in the "Text", could someone please let me know how to solve it? I don't really understand about C#
this is the code
namespace Praktikum1
{
public partial class Form1 : Form
{
private object lblText;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
this.lblText.Text = "Belajar Pemrograman Visual C#";
}
}
}
the error is in = this.lblText, the Text,
You see to have added lblText by hand here
private object lblText;
if generated by the form designer it would be declared as
private Label lblText;
You say the 'module' did this, I dont know what that means. Also its impossible to know if lblText is really a label or not.
If it is then this will work
((Label)this.lblText).Text = "Belajar Pemrograman Visual C#";
ie - casting the object to the Label type
You should create the label and then change its text:
Label testLBL = new Label();
this.Controls.Add(testLBL);
testLBL.Text = "testLBL";
Or add the label to the form through the toolbox(from designer) and set text.
Related
I'd like to add a UserControl (UserControl2) to a panel on another form (MainForm) when the user clicks a button on the UserControl that is currently in the panel (UserControl1). UserControl2 should replace UserControl1 as the contents of the panel when this button is clicked.
I've tried figuring out how to use an event to communicate between UserControl1 and MainForm, but I just don't know where to start, as I can't find an example that is easily adaptable to my particular situation (Adding a control to a panel).
THIS question is similar, but doesn't quite fit what I'm trying to do (or at least I just don't know how to adapt it to what I'm trying to do)
I've tried this method, and get no errors, but my button doesn't do anything:
MainForm:
private void SubscribeToEvent(MyUserControl1 Button_Click)
{
MyUserControl1 CurrentClass = new MyUserControl1();
CurrentClass.Button.Click += new EventHandler(this.ButtonClickHandler);
}
private void ButtonClickHandler(object sender, EventArgs e)
{
MyUserControl2 MainPanelControls = new MyUserControl2();
MainPanel.SuspendLayout();
MainPanel.Controls.Clear();
MainPanel.Controls.Add(MainPanelControls);
MainPanel.ResumeLayout();
}
UserControl1:
public void Button_Click(object sender, EventArgs e)
{
//... Not sure what I'm missing here
}
I've also tried this method, this time trying to implement something similar to the method described in the link near the top of my question. I know these are obviously wrong (otherwise I wouldn't need to ask the question), but I don't have enough knowledge on the subject to figure it out on my own:
UserControl1:
public MyUserControl1()
{
InitializeComponent();
}
private MainForm mainForm = null;
public MyUserControl1(Form callingForm)
{
mainForm = callingForm as MainForm;
InitializeComponent();
}
//...
private void Button_Click(object sender, EventArgs e)
{
MyUserControl2 MainPanelControls = new MyUserControl2();
mainForm.MainPanel.SuspendLayout();
mainForm.MainPanel.Controls.Clear();
mainForm.MainPanel.Controls.Add(MainPanelControls);
mainForm.MainPanel.ResumeLayout();
}
Now when I click Button I get an "Object reference not set to an instance of an object" error at mainForm.MainPanel.SuspendLayout(); (Or anything past that point).
I've also tried modifying Joh's answer, but end up with the same Null Reference Exception, this time on my Button_Click event at ButtonClickedToMainForm(this, e); I'm not sure if I need to create an instance of ButtonClickedToMainForm, or how to do that properly (if it's not something else, that is).
I have a feeling I've likely just placed some of this code in the wrong place, so I'm hoping someone more experienced may be able to help me sort that out.
UPDATE
This is my attempt at implementing Joh's answer, being so new to this, I'm not quite sure where I've messed up:
MainForm:
namespace MyProject
{
public delegate void ButtonClickToMainForm(object sender, EventArgs e);
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
UserControl1 userControl1 = new UserControl1();
userControl1.Button1Clicked += userControl1_Button1Clicked;
}
private void userControl1_Button1Clicked(object sender, EventArgs e)
{
try
{
UserControl2 MainPanelControls = new UserControl2();
MainPanel.SuspendLayout();
MainPanel.Controls.Clear();
MainPanel.Controls.Add(MainPanelControls);
MainPanel.ResumeLayout();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
UserControl1:
public partial class UserControl1: UserControl
{
public event ButtonClickToMainForm Button1Clicked;
public UserControl1()
{
InitializeComponent();
}
private void OnButton1Clicked(object sender, EventArgs e)
{
Button1Clicked?.Invoke(this, e);
}
private void Button1_Click(object sender, EventArgs e)
{
Button1Clicked(this, e); //"Object reference not set to an instance of an object."
}
}
I'm sure it's something simple I'm missing on UserControl1, I'm just not sure what.
Here is an easy example for a delegate event.When you implement it in all your usercontrols it should work for your problem.
UserControl:
//Create a delegate
public delegate void ButtonClickToMainForm(object sender, EventArgs e);
public partial class UserControl1 : UserControl
{
//Your own event based on created delegate
public event ButtonClickToMainForm ButtonClickedToMainForm;
public UserControl1()
{
InitializeComponent();
}
//This method will invoke your event
private void OnButtonClickedToMainForm(object sender, EventArgs e)
{
ButtonClickedToMainForm?.Invoke(this, e);
}
private void button1_Click(object sender, EventArgs e)
{
//On button1_Click this will fire the event on mainform
OnButtonClickedToMainForm(this, e);
}
and the mainForm :
public Form1()
{
InitializeComponent();
//Subscribe to event from your usercontrol
userControl11.ButtonClickedToMainForm += UserControl11_ButtonClickedToMainForm;
}
//Button on userControl1 has been clicked
private void UserControl11_ButtonClickedToMainForm(object sender, EventArgs e)
{
//do your things here...
}
Hope this helps.
I have a form with 6 buttons. These buttons serve to increase/decrease tha value of the respective textbox. Now I'm trying to "animate" the buttons. I want to get another effect on the button when the mouse is over him.
To do that, I have two diferent images in Resources and I am doing this code:
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.DownHover;
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.Down;
}
This works fine. My question is: it wouldn't be wise to create a class (ButtonBehaviour.cs) and put this code in that class?
So I would have something like this:
ButtonBehaviour buttonBehaviour = new ButtonBehaviour();
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
buttonBehaviour.buttonDownHover();
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
buttonBehaviour.buttonDownLeave();
}
And the class should be:
public class ButtonBehaviour {
public void buttonDownHover() {
// code
}
public void buttonDownLeave() {
// code
}
}
How can I create this Class Behaviour and make the buttons adapt this Behaviour?
if one effect should be applied for all buttons, try to add the same event handlers to them
private void btn_MouseHover(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.DownHover;
}
private void btn_MouseLeave(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.Down;
}
button which raised event is available via sender variable
this way you avoid code duplication for every button. creating a ButtonBehaviour or CustomButton is probably an over-engineering unless you need them in many forms
I made a combobox with the name FormatComboBox. I populated it with a list of items. I want to trigger an event whenever the user selects an item from the list. The following is my code.
private void FormatComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
/// some code
}
I put a break point inside the code to see whether it is working and found that it isn't. After I tried using
private void FormatComboBox_SelectedValueChanged(object sender, EventArgs e)
private void FormatComboBox_SelectedItemChanged(object sender, EventArgs e)
I am working on c# for the first time and I was following this tutorial
http://www.kinectingforwindows.com/2013/04/09/tutorial-kinect-television/
The one they used was the following
private void OnSelectedFormatChanged(object sender, SelectionChangedEventArgs e)
But even that is not working
Make sure that the event is attached to the FormatComboBox.
By design:
By Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndexChanged +=comboBox1_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You need to make sure that you are actually adding the event handler properly in your code or in the text box's properties. It should look something like this:
public partial class Form1 : Form
{
FormatComboBox fbox = new FormatComboBox();
// Associate event handler to the combo box.
fbox.SelectedValueChanged+=FormatComboBox_SelectedValueChanged;
prviate void FormatComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
// do stuff
}
}
First, pardon my new-ness, I just started coding class recently. Now, upon startup, I want parts of my form (c#) to not be shown, however when I put
NameDisplay.Visible = false;
(NameDisplay being the label I wish to hide) into my Form1.cs it gives me the error of that it is a 'field' being used as a 'type'. How do I correct this, and apply to other object types (buttons, textboxes, etc?)
EDIT 1-
Code- as it stands
namespace ATM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label NameDisplay;
NameDisplay.Visible = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Remove Label NameDisplay;, and place NameDisplay.Visible = false; into your FormLoad event.
The loading of a form is an event just like clicking a button, and will execute the code like so.
Also, when I hide labels, I use .Hide(), but I believe that only works on WinForms.
Hope this helps!
You need to drag and drop the Label on the form and object will be created and initialized automatically in InitializeComponent.
In the form constructor (after InitializeComponent function) or Form_Load event, you may set the visibility to false
For example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NameDisplay.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
i try to save user settings in c#.
I can read the value and put it in a textbox but i can not change it.
This is my WindowsForm with an textbox and a save button:
namespace tool
{
public partial class Form4 : Form
{
string source = Properties.Settings.Default.Source;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = source;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = source;
Properties.Settings.Default.Save();
Application.Exit();
}
}
}
And here is a picture with my settings:
I hope someone as an idea :-)
Thanks for help
Try this:
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = textBox1.Text;
Properties.Settings.Default.Save();
Application.Exit();
}
You need to use what is currently in the text box, not your member variable.
Or you can change this event to be as follows (and then you don't have to modify the above):
private void textBox1_TextChanged(object sender, EventArgs e)
{
source = textBox1.Text;
}
could you possibly have a read lock being applied to the key you're looking at while testing this? Maybe the code's not the problem?