Rename a checkbox by implementing ContextMenu c# winforms [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have set of dynamically created checkboxes on a panel and also have implemented ContextMenuStrip on all checkboxes.
I am unable to detect that which control currently displays the shortcut menu defined in the ContextMenuStrip.

I have got the answer.
private void MenuViewDetails_Click(object sender, EventArgs e)
{
// Try to cast the sender to a MenuItem
MenuItem menuItem = sender as MenuItem;
if (menuItem != null)
{
// Retrieve the ContextMenu that contains this MenuItem
ContextMenu menu = menuItem.GetContextMenu();
// Get the control that is displaying this context menu
Control sourceControl = menu.SourceControl;
}
}

Use the SourceControl() property.
With a ContextMenu:
private void menuItem1_Click(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)contextMenu1.SourceControl;
Console.WriteLine(cb.Name);
}
With a ContextMenuStrip:
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)contextMenuStrip1.SourceControl;
Console.WriteLine(cb.Name);
}

Related

Use Variable after this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
How is it possible to use a var after "this."
i want to set the Background color of a TextBlock to Blue.
Brush blue = new SolidColorBrush(Colors.LightBlue); //Working Fine
//Click on the TextBlock (Working Fine)
private void MouseDown_TextBlock(object sender, MouseButtonEventArgs e)
{
var stand = ((TextBlock)sender).Name; //Here i am getting the Name of the TextBlock (Working Fine)
this.stand.Background = blue; //This is not working here
}
If you want to access the control you receive as Sender in an event, just cast it to the appropriate class:
private void textBlock1_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get sender as TextBlock
TextBlock standTextBlockFromSender = (TextBlock)sender;
standTextBlockFromSender.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
}
If you want to find the control by name, you can use the FindName method.
private void textBlock1_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get name of sender
string stand = ((TextBlock)sender).Name;
// Get TextBlock by name
TextBlock standTextBlockFromName = (TextBlock)this.FindName(stand);
standTextBlockFromName.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
}

Determine Which Control In My WPF Has Focus Via Script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to determine which control has focus in C# ?
Example :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox); /*Here there my textBox has focus.*/
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, /*I mean here : the focus remains where it is already, on the textBox.*/);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
myControl.Focus();
}
In the NotCorrect() method :
textBlock.Background means : I want the same brush as the one already existing (see the first line of the Click event), I do not want to change anything.
(I prefer to write textBlock.Background rather than rewrite Brushes.White.)
Now I ask if there is also a way to say : I want the focus to remain on the control on which it is already, I do not want to change anything.
Form.ActiveControl is you should search for.
I found out a kind of solution using null :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox);
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, null);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
if (myControl != null)
myControl.Focus();
}
Another simpler solution I found is to use this :
private void button_Click(object sender, RoutedEventArgs e)
{
Update(Brushes.White, textBox);
//... (some codes)
NotCorrect();
//... (some codes)
}
private void NotCorrect()
{
Update(textblock.background, this);
textBlock.text = "Try again..."
}
private void Update(Brushes myBrush, Control myControl)
{
texBlock.Background = myBrush;
myControl.Focus();
}

Label don't change value when I choose one CheckBox [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hello for everyone I am new here,sorry if this post doens't match with at all with how should be,because It's my first post this.
I am wanting to change the value of the Label when I choose one CheckBox,but It's seems that the Label just changes when I click in the Label.
I was looking for see if the Label had some Events for make work it but I didn't found one.
The Label it's in another groupBox,It's seems like that :
In one GroupBox has 3 CheckBox and i want that when i choosing one or all from that CheckBox will change the value of the Label in the another GroupBox.
Please guys,help me here,I am really wanting to understand why It's not working.
You can just register an event handler for the CheckChanged event and change the label's caption in the event handler :
public partial class Form1 : Form
{
public MyForm()
{
InitializeComponent();
myCheckbox.CheckedChanged += new System.EventHandler(this.checkedChanged);
myCheckbox1.CheckedChanged += new System.EventHandler(this.checkedChanged);
myCheckbox2.CheckedChanged += new System.EventHandler(this.checkedChanged);
}
private void checkedChanged(object sender, EventArgs e)
{
myLabel.Text = "Some text";
}
}
use this code here, but change the event to the control you're looking for
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "your value here";
}
you can do this by double clicking on the radio button control and then it will generate the event for you and then set your code as per the example here.

ListBox items doesn't appear when I get it from Method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have problem with ListBox.
I made a method that does some work and returns a ListBox type. I tried to get this Listbox instance from the method, but the ListBox doesn't show any items.
I hope to know how I can replace the existing ListBox with the returned Listbox from the method.
public ListBox GetProduct()
{
ListBox MyListBox = new ListBox();
MyListBox.Sorted = true;
MyListBox.Items.Add("aaaaa");
return MyListBox;
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox1 = GetProduct();
}
try this:
public void GetProduct(ListBox MyListBox)
{
MyListBox.Sorted = true;
MyListBox.Items.Add("aaaaa");
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
GetProduct(listBox1);
}

Why doesn't my second form show up? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I made a timer, but when I press a button nothing happens.
My code is:
private void button3_Click(object sender, EventArgs e)
{
Instellingen form = new Instellingen();
form.Show();
}
Instellingen.cs code:
public partial class Instellingen : Form
{
public Instellingen()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show("text", "text");
}
}
}
The button3 event doesn't even fire (confirmed by adding a breakpoint, it doesn't get there)
Check if the button's event realy connected to the method button3_Click.
Put a breakpoint in the first line of button3_Click and check if the code get there.
Make sure your button's click event is button3_Click. Probably your button's click event is empty.

Categories

Resources