C# Button Object as a parameter - c#

I'm trying to make a function with a parameter so whenever I call it, it will change the button's back color but my code doesn't work, any ideas
Button button = new Button();
private void Change(object sender)
{
if (button.Visible == true)
{
button.BackColor = Color.Red;
}
}
private void hr1_left2_btn_MouseEnter(object sender, EventArgs e)
{
Change(hr1_left2_btn);
}

Perhaps like this? I've changed the Sender Arg type of the Change function to ref a button object.
private void Change(Button sender) {
if (sender.Visible)
sender.BackColor = Color.Red;
}
private void hr1_left2_btn_MouseEnter(object sender, EventArgs e) {
Change(hr1_left2_btn);
}

Related

if statements in C# for a textbox to change back to normal when i press a button for the second time

This is the code that i used to change the text in the text box from "Livre" to "Ocupado"
What code should i use to change it from "Ocupado" to "Livre"
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text="Ocupado";
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "Livre")
{
textBox1.Text = "Ocupado";
}
else
{
textBox1.Text = "Livre";
}
}
you can add a variable to your class
e.g.:
bool livre = true;
private void button1_Click(object sender, EventArgs e)
{
if (livre)
{
textBox1.Text="Ocupado";
}
else
{
textBox1.Text="Livre";
}
livre = !livre;
}

Show TextBox on every TabPage

I attached a TextBox to the first TabPage of a TabControl. I would like to display the same TextBox object on every TabPage. I tried to add the control to the tabControl Collection but unfortunately it's not working.
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}
Button b;
public Form1()
{
InitializeComponent();
b = new Button() { Text = "Prueba" };
}
private void Form1_Load(object sender, EventArgs e)
{
AddButtonToTabControl();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
AddButtonToTabControl();
}
public void AddButtonToTabControl()
{
tabControl1.SelectedTab.Controls.Add(b);
}
I missed two methods. It's working now!
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}

How to go from one "private void" to other "private void"?

how can i go from private void turnon to private void turnoff ? I want only know how to go from one void to other. I know i can just make from these two a one private void but i don't want it
private void turnon(object sender, EventArgs e)
{
button2.Visible = true
}
private void turnoff(object sender, EventArgs e)
{
button3.Visible = false
}
If you're wanting it to be visible in one event and visible in another, why not use one method like so:
private void SwitchState(object sender, EventArgs e)
{
button3.Visible = !button3.Visible;
}
On reading your comments I guess you want to add this line in at the end of your turnon event:
turnoff(sender, e);
Do you want a toggle? If so, you could use the following code.
private void toggle(object sender, EventArgs e)
{
button2.Visible = !button2.Visible;
}
Hi I want to make toggle button, So that it shows/hides the content by clicking that searchButton
Here is my code,
private boolean visible;
protected Button SearchButton;
private void Toggle(){
if(visible=false){
DishButton.setVisibility(View.INVISIBLE);
SpoonButton.setVisibility(View.INVISIBLE);
cupButton.setVisibility(View.INVISIBLE);
FridgeButton.setVisibility(View.INVISIBLE);
}
else {
DishButton.setVisibility(View.VISIBLE);
SpoonButton.setVisibility(View.VISIBLE);
cupButton.setVisibility(View.VISIBLE);
FridgeButton.setVisibility(View.VISIBLE);
visible=true;
}
}

Execute a Method

Goal:
When you press on the menu button named "Delete" when you right click in a listView, the method named Test() should be executed.
Problem:
I cannot make it to be happened because I get a error messsage.
Error 1 No overload for 'Test' matches delegate
'System.Windows.RoutedEventHandler'
private void lvw_bokade_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
ListView selectedData = (ListView)sender;
Passlista myPasslista = (Passlista)selectedData.SelectedItem;
System.Windows.Point pt = e.GetPosition(this);
if (myPasslista != null && !(System.Windows.Media.VisualTreeHelper.HitTest(this, pt).VisualHit is ScrollViewer))
{
ContextMenu myContextMenu = new ContextMenu();
MenuItem menuItem2 = new MenuItem();
menuItem2.Header = "Delete";
myContextMenu.Items.Add(menuItem2);
menuItem2.Click += new RoutedEventHandler(Test);
myContextMenu.IsOpen = true;
}
}
private void Test()
{
MessageBox.Show("ssss");
}
Change the signature of your method to match the delegate of RoutedEventHandler:
private void Test(object sender, RoutedEventArgs e)
{
MessageBox.Show("ssss");
}
The signature of the delegate looks like this, according to MSDN:
public delegate void RoutedEventHandler(
Object sender,
RoutedEventArgs e
)
Your method needs to match this delegate:
public delegate void RoutedEventHandler(
Object sender,
RoutedEventArgs e
)
You need to change it to:
private void Test(object sender, RoutedEventArgs e)
{
MessageBox.Show("ssss");
}
change the signature of the method test to:
private void Test(object sender, RoutedEventArgs e)

c# error cant understand what i should do to solve it

hi i have this code(and i get the ERROR that i must declare a body because it is not not marked abstract,extern or partial...by the way i couldnt get it to post it here but right over the public partial class Form1 : Form i also have written bool play1 = true;bool play2 = false; int x=1;int o=10; )when i click on it it goes up to that area that i marked with fat letters
could someone please show me what is wrong and how to solve it step by step
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
værdier();
int[] status = new int[9];
zeihne();
}
private string[] status;
private void value()
{
int[] status = new int[9];
zeihne();
}
private void zeihne()
{
button1.Text = status[0];
button2.Text = status[1];
button3.Text = status[2];
button4.Text = status[3];
button5.Text = status[4];
button6.Text = status[5];
button7.Text = status[6];
button8.Text = status[7];
button9.Text = status[8];
}
// private void label1_Click(object sender, EventArgs e)
{
}
**private void Form1_Load(object sender, EventArgs e);**
private void button1_Click(object sender, EventArgs e)
{
if (play1 == true)
{
play1 = true;
button1.Text = "X";
play1 = false;
}
else
{
play2 = true;
button1.Text = "O";
play2 = false;
play1 = true;
}
You haven't defined the method body for
private void Form1_Load(object sender, EventArgs e);
That's an abstract method and they are only allowed on abstract classes
Something like this would do the trick
private void Form1_Load(object sender, EventArgs e)
{
}
Or you can just get rid of the method if you don't plan to have any code on it.
EDIT
Something else that looks weird on your code is
// private void label1_Click(object sender, EventArgs e)
{
}
What are you trying to do? Two options:
// Option1: All the method commented
*/private void label1_Click(object sender, EventArgs e)
{
}*/
// Option2: Nothing commented
private void label1_Click(object sender, EventArgs e)
{
}
Hello Hwoarang H i tryed your code in my form and its working fine. You have to just follow step.
You have difine your method like this
private void Form1_Load(object sender, EventArgs e);
This is not right way to define method like this.You must declare like bellow
private void Form1_Load(object sender, EventArgs e)
{ }
Now remove your lable click event and brackets
// private void label1_Click(object sender, EventArgs e)
//{
//}
Now you got warning like x is issigned to but never used, o is assigned to but never used and status is assigned to but never used.No warry about that bcos you assign x and o as integer and used as string so this warning is come,i dont know whats your logic but if you want to remove this warning than use this int x,and o.Also play2 is never used so put condition like bellow
if (play1 == true)
{
btnSendNotification.Text = "x";
play1 = false;
}
else if(play2==true)
{
btnSendNotification.Text = "o";
play2 = false;
play1 = true;
}
Hope this is help you if not get solution than comment me.

Categories

Resources