Hoping you can help - I have programmatically created button & richtextbox.
// Button to Edit
Button butEditToDo = new Button();
butEditToDo.Location = new Point(285, 10);
butEditToDo.Size = new System.Drawing.Size(25, 25);
butEditToDo.BackColor = Color.Transparent;
butEditToDo.FlatStyle = FlatStyle.Flat;
butEditToDo.FlatAppearance.BorderSize = 0;
butEditToDo.FlatAppearance.MouseOverBackColor = Color.FromArgb(244, 244, 244);
butEditToDo.Cursor = Cursors.Hand;
butEditToDo.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Edit_25));
pnlPendingNote.Controls.Add(butEditToDo);
// Pending Nane + Tag
RichTextBox rxtNotes = new RichTextBox();
rxtNotes.Size = new System.Drawing.Size(317, 68);
rxtNotes.Location = new Point(3, 37);
rxtNotes.Text = (read["notNote"].ToString());
rxtNotes.ReadOnly = true;
rxtNotes.BorderStyle = BorderStyle.None;
rxtNotes.DetectUrls = true;
rxtNotes.BackColor = Color.FromArgb(244, 244, 244);
pnlPendingNote.Controls.Add(rxtNotes);
So when ever I click on ButEditToDo_Click - I can get the right button clicked.
So when I click on this button I would like to enable the RichTextbox - and when I click the button again - I would like to update the database.
Button Click:
private void ButEditToDo_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
for (int i = 1; i < pendingcounter; i++)
{
if (btn.Name == ("PenNote" + i))
{
break;
}
}
}
Hope you can help please with enabling the button, I'm all good with the database.
Thank you.
Edit One
#Ed - thank you.
Please See Image.
What I would like to achieve - When i click on the tools icon - the RichTextBox will be enabled.
So if I click on the tools on first panel - then the R_TextBox will be enabled for me to edit the text.
Then the Icon will change and I will be able to click on it again to save to the database.
Hope that makes more sense for you Ed.
Just give the button an event handler that does stuff. Use a lambda so you can reference the local reference to the RichTextBox.
Button butEditToDo = new Button();
// ...snip...
RichTextBox rxtNotes = new RichTextBox();
// ...snip...
butEditToDo.Click += (sender, args) =>
{
CycleNoteState(rxtNotes);
};
And here's the guts of the event handler. You could put this all in the event handler, but the code's more readable this way. CycleNoteState isn't a very good name, but I'm not clear about the semantics of your program.
I may have misunderstood the logic for what the button does on successive clicks. If it's more complicated than this, you can introduce a state enum or something. Let me know and we'll get it figured out.
private void CycleNoteState(RichTextBox rtb)
{
if (!rtb.Enabled)
{
rtb.Enabled = true;
}
else
{
// Do save stuff here
}
}
Related
I have a button, I put 2 other buttons inside it. I want those 2 other buttons to only appear when I enter the main button with my mouse. When I enter it, I want the 2 other buttons to be half opaque and only be fully opaque when I enter one of those 2 buttons.
These buttons are inside a FlowLayoutPanel with a background image on it.
This is how they look like:
The Buttons have a picture inside them and a text.
Here is my code:
public class MyButton : Button
{
public MyButton()
{
SetStyle(ControlStyles.StandardClick |
ControlStyles.StandardDoubleClick, true);
Text = component.ProductsName;
TextAlign = ContentAlignment.TopCenter;
ImageAlign = ContentAlignment.TopLeft;
Size = new Size(178, 75);
foreach (Button item in CustomButtons())
{
Controls.Add(item);
}
}
static Button[] CustomButtons()
{
Button delete = new Button();
delete.Location = new Point(157, 1);
delete.Size = new Size(20, 20);
delete.MouseEnter += OnMouseEnter;
delete.MouseLeave += DeleteOnMouseLeave;
Button customize = new Button();
customize.Location = new Point(delete.Left - 20, 1);
customize.Size = new Size(20, 20);
Button[] buttons = {delete, customize};
return buttons;
}
private static void DeleteOnMouseLeave(object sender, EventArgs e)
{
Button btn = (Button) sender;
btn.UseVisualStyleBackColor = true;
btn.BackColor = Color.Transparent;
}
private static void OnMouseEnter(object sender, EventArgs e)
{
Button btn = (Button) sender;
btn.UseVisualStyleBackColor = false;
btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(100,
Color.Black);
}
}
I think I tried everything that came to my mind, I tried events and everything and the buttons never worked as I intended them to work.
Any help would be appreciated! Thanks! :D
it seems I solved it! I only had to set Flatstyle = FlatStyle.Flat and backColor = Color.Transparent! :D
Here is the result: exsample of output
I have created a set of button and attach Click event and GotFocus event to them.
for (int i = 0; i < NumberOfQuestion; i++)
{
RadButton button = new RadButton();
// radButton1
//
button.Anchor = AnchorStyles.None;
button.Font = new Font("Segoe UI", 8.25F, FontStyle.Bold);
button.Location = new Point(65 * i + 15, 10);
button.Name = "btn_cauhoi" + (i + 1);
button.Size = new Size(60, 35);
button.TabIndex = 1 + i;
button.Text = "Câu " + (i + 1);
button.Tag = (i + 1);
button.Click += Button_Click;
button.GotFocus += Button_Click; ;
//
panel_nut_cauhoi.Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e)
{
var button = (RadButton)sender;
var index = (int)button.Tag;
MessageBox.Show(index.ToString());
}
It triggers Click event correctly but with GotFocus event it trigger repeatly.
Somebody helps me, please.
Thanks in advances.
When you click ok on message box it loose focus and get focus again.
So if you delete MessageBox.Show() you will see its gonna trigger only one time, so you can test code like below, you will see the name of the button as btn_cauhoi1 or btn_cauhoi2 or btn_cauhoi3 up to which button you do click, it means its gonna trigger only one time.
var button = (RadButton)sender;
var index = (int)button.Tag;
//MessageBox.Show(index.ToString());
this.Text = button.Name;
I'm trying to learn C# again and I was wondering what is the approach to achieve the result I needed, this is my code, it creates the label and button when I click a button.
private void button_Copy_Click(object sender, RoutedEventArgs e)
{
counter++;
Label lbl = new Label();
lbl.Content = counter.ToString();
lbl.HorizontalAlignment = HorizontalAlignment.Center;
lbl.VerticalAlignment = VerticalAlignment.Center;
lbl.FontSize = 50;
Button bt = new Button();
bt.Content = "X";
bt.HorizontalAlignment = HorizontalAlignment.Right;
bt.VerticalAlignment = VerticalAlignment.Top;
grid.Children.Add(lbl);
grid.Children.Add(bt);
}
However I'm having a problem determining where to put the click event since it was created dynamically. What I wanted to happen was when I click the X button, it will remove the specific label and x button I clicked. So if I clicked the main button twice, it would show a 1 with an x on the top left and a 2 with an x on the top left and when I click the 2's x, it will remove both the label with 2 and the x button for 2.
Just add an event handler to the button:
private void button_Copy_Click(object sender, RoutedEventArgs e)
{
counter++;
Label lbl = new Label();
lbl.Content = counter.ToString();
lbl.HorizontalAlignment = HorizontalAlignment.Center;
lbl.VerticalAlignment = VerticalAlignment.Center;
lbl.FontSize = 50;
Button bt = new Button();
bt.Content = "X";
bt.HorizontalAlignment = HorizontalAlignment.Right;
bt.VerticalAlignment = VerticalAlignment.Top;
// add subscriber
bt.Click += Button_Click;
grid.Children.Add(lbl);
grid.Children.Add(bt);
}
// On click event for button
private void Button_Click(object sender, EventArgs e)
{
// do whatever when button is clicked
// this is a reference to the button that was clicked,
// you can delete it here or do whatever with it
Button buttonClicked = (Button)sender;
}
Now, when the button is clicked, "Button_Click" will be fired off.
You can add a handler to your button's Clicklike this
bt.Click += (a,b) => grid.Children.Remove( a );
but you really should do it with a ListView and templates, as slugster suggested.
When clicking on a button, 5 textboxes will be displayed. I have to add a button for reply to the last textbox - can anyone show me how?
This is my code:
protected void GenTextBox(object sender, EventArgs e)
{
for (i = 1; i <= TotalReplys; i++)
{
HtmlGenericControl lineBreak = new HtmlGenericControl("br");
Page.Controls.Add(lineBreak);
TextBox MyTextBox = new TextBox();
MyTextBox.ID = i.ToString();
MyTextBox.Width = 540;
MyTextBox.Height = 60;
MyTextBox.Text = "Get the value from the database";
MyTextBox.TextMode = TextBoxMode.MultiLine;
Panel1.Controls.Add(MyTextBox);
Panel1.Controls.Add(lineBreak);
}
}
In general, you are on the right track. Use the same code that you have for generating a textbox, but repurpose it to generate a button.
Here are some hints to get you on the right track
if ( i == TotalReplys ){
Button MySearchButton = new Button();
//Set Button Properties
Panel1.Controls.Add(MySearchButton);
}
I imagine you are either hung up on the if-statement logic or perhaps not aware of the Button object. Either way, this should set you on the right track.
Why cant I do this in wpf?
button1Click.Content = "Hover over me";
or
ToolTip t = new ToolTip();
t.Content = "Something helpful";
button1Click.Tooltip = t;
I populate my widow with populate buttons on initialization I then have a foreach loop that adds buttons like so:
foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3_Click })`
Now in this area I apply styles to the buttons all in one go like so:
public void populateButtons()
{
double xPos;
double yPos;
Random ranNum = new Random();
foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3_Click })
{
Button foo = new Button();
Style buttonStyle = Window.Resources["CurvedButton"] as Style;
int sizeValue = 100;
foo.Width = sizeValue;
foo.Height = sizeValue;
xPos = ranNum.Next(200);
yPos = ranNum.Next(250);
foo.HorizontalAlignment = HorizontalAlignment.Left;
foo.VerticalAlignment = VerticalAlignment.Top;
foo.Margin = new Thickness(xPos, yPos, 0, 0);
foo.Style = buttonStyle;
foo.Click += routedEventHandler;
LayoutRoot.Children.Add(foo);
}
}
}
}
But when I try this:
private void button3_Click(object sender, RoutedEventArgs e)
{
((Button)sender).ToolTip = t;
}
It only activates when a button is pressed? (Thanks #H.B)
However when I paste the tooltip code in my populate buttons:
Random ranNum = new Random();
foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3_Click })
{
Button foo = new Button();
ToolTip t = new ToolTip();
t.Content = "Something helpful";
foo.ToolTip = t;
It works? But the problem is that doing it this way sets all buttons with the same tooltip and or button content! which I dont want, but I cant find a way around it?
So to summarize I can either set all buttons with the same tooltip message or button content within "populateButtons()" or I can only set tooltips and button content when the button has been pressed.
Is there no method possible that can add content to a named button?
Like my initial attempt:
button1Click.Content = "Home Button";
or
button1Click.ToolTip = "Hover over me";
Why on earth cant you set content and tooltips for specific buttons on initialization?
If you want to go this route then you can add a handler for Loaded event:
foo.Click += routedEventHandler;
foo.Loaded += routedEventHandler;
And then you have something like:
void button2Click(object sender, RoutedEventArgs e)
{
if (e.RoutedEvent == FrameworkElement.LoadedEvent)
{
ToolTip t = new ToolTip();
t.Content = "Something helpful";
((Button)sender).ToolTip = t;
return;
}
//Logic for handling button clicks goes here
MessageBox.Show("action 2");
}
You are not assigning the tooltip in the handler, so how should anything happen?
((Button)sender).ToolTip = t;
If I understand correctly, you want to have different "types" of buttons, each one with a different Click handler and a different Tooltip.
If that's the case, You could create different classes deriving from Button (e.g. HelpfulButton, UnhelpfulButton... choose good names please :) ) and in their constructor assign the handler and tooltip.
Then, in the main code, you could loop through the different classes and add instances of them directly to the main canvas.
Another possible option is to create different "template" buttons using commands and copy the properties from them instead of subclassing Button. Something like this:
Button helpfulTemplate = new Button { ToolTip = "blah" };
helpfulTemplate.Command = HelpfulCommand;
Button unhelpfulTemplate = new Button { ToolTip = "bloh" };
unhelpfulTemplate.Command = UnhelpfulCommand;
foreach(var template in new Button[] {helpfulTemplate, unhelpfulTemplate})
{
var newCopy = new Button();
//etc.
newCopy.ToolTip = template.ToolTip;
newCopy.Command = template.Command;
}