WPF C# Image MouseDown to switch pages - c#

I have 3 Images with Mouse Down event like this :
private void button12_MouseDown(object sender, MouseButtonEventArgs e) // Back to choose story menu page.
{
//if button 2 is pressed then show FoodKing
// if button 1 is pressed then show Grasshopper
stackPanelHome.Visibility = System.Windows.Visibility.Hidden;
pageTransition1.Visibility = System.Windows.Visibility.Visible;
pageTransition1.ShowPage(What to put in here?);
}
private void button2_MouseDown(object sender, MouseButtonEventArgs e) // Food fit for a king.
{
FoodKing controlpage = new FoodKing(); // Calling user control page
stackPanelHome.Visibility = System.Windows.Visibility.Hidden;
pageTransition1.Visibility = System.Windows.Visibility.Visible;
pageTransition1.ShowPage(controlpage);
}
private void button1_MouseDown(object sender, MouseButtonEventArgs e) // Grasshopper
{
GrasshopperMenu controlpage = new GrasshopperMenu(); / Calling user control page
stackPanelHome.Visibility = System.Windows.Visibility.Hidden;
pageTransition1.Visibility = System.Windows.Visibility.Visible;
pageTransition1.ShowPage(controlpage);
}
In the first page , there is 2 icon , 1 is FoodKing , 1 is Grasshopper then they choose which image to press on and each icon have many buttons and leads to else where , so i create a menu page to revert the user back to the page that they have selected in the beginning ( FoodKing or GrassHopper) . But how do i do this? See my code above.

Page currentPage;
private void button12_MouseDown(object sender, MouseButtonEventArgs e) // Back to choose story menu page.
{
stackPanelHome.Visibility = System.Windows.Visibility.Hidden;
pageTransition1.Visibility = System.Windows.Visibility.Visible;
if(currentPage != null)
{
pageTransition1.ShowPage(currentPage);
}
}
private void button2_MouseDown(object sender, MouseButtonEventArgs e) // Food fit for a king.
{
FoodKing controlpage = new FoodKing(); // Calling user control page
stackPanelHome.Visibility = System.Windows.Visibility.Hidden;
pageTransition1.Visibility = System.Windows.Visibility.Visible;
pageTransition1.ShowPage(controlpage);
currentPage = controlpage;
}
private void button1_MouseDown(object sender, MouseButtonEventArgs e) // Grasshopper
{
GrasshopperMenu controlpage = new GrasshopperMenu(); // Calling user control page
stackPanelHome.Visibility = System.Windows.Visibility.Hidden;
pageTransition1.Visibility = System.Windows.Visibility.Visible;
pageTransition1.ShowPage(controlpage);
currentPage = controlpage;
}

Related

Changing the properties of a dynamically created control

I have a Form with a LayoutPanel that has dynamically added buttons inside. The buttons are added at runtime, which works, but my problem is I'd like to set the properties to one of the buttons to disabled if a textBox is empty and enable it when it the textBox is not empty.
Here is a code example, with the error I am receiving below:
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
Button button1 = new Button();
button1.Name = "button1";
tableLayoutPanel1.Controls.Add(button1, 0, 0);
Button button2 = new Button();
button1.Name = "button2";
tableLayoutPanel1.Controls.Add(button2, 0, 0);
Button button3 = new Button();
button3.Name = "button1";
tableLayoutPanel1.Controls.Add(button3, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
CS0103 The name 'button1' does not exist in the current context
Should I be declaring the buttons elsewhere so the entire code can see that they do in fact exist or is my problem elsewhere? Thanks.
You must declare a Button outside Form1_Load if you want to access the button with name directly from other methods, because in your case the buttons are just available in the Form1_Load method :
Button button1;
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
button1 = new Button();
button1.Name = "button1";
tableLayoutPanel1.Controls.Add(button1, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
or if you want declare Buttons inside Form1_Load, you can access to Button like this :
private void textBox1_TextChanged(object sender, EventArgs e)
{
var btn = tableLayoutPanel1.Controls.OfType<Button>().Where(x => x.Name == "button1").FirstOrDefault();
(btn as Button).Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
As an alternative, you could dynamically get the control from the Layout Panel and set the enabled property like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
Control button = tableLayoutPanel1.Controls["button1"];
button.Enabled = string.IsNullOrEmpty(textBox1.Text) ? false : true;
}
This approach is not as "safe" as declaring the buttons at form level, but I thought it would be useful to mention for times when you need to be genuinely dynamic in referencing controls.
There are a couple ways you can do this, one would be to use the Find method of the TableLayoutPanel's Controls Collection like this.
private void textBox1_TextChanged(object sender, EventArgs e)
{
Button btn =(Button)tableLayoutPanel1.Controls.Find("button1", true)[0];
if (string.IsNullOrEmpty(textBox1.Text))
{
btn.Enabled = false;
}
else
{
btn.Enabled = true;
}
}
The second would be to use the buttons Tag Property to determine which control to use, I have used this in the past for dynamically generated controls.
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
Button button1 = new Button();
button1.Name = "button1";
button1.Tag = 1; //note the Tag property being used
tableLayoutPanel1.Controls.Add(button1, 0, 0);
Button button2 = new Button();
button1.Name = "button2";
button2.Tag = 2;
tableLayoutPanel1.Controls.Add(button2, 0, 0);
Button button3 = new Button();
button3.Name = "button3";
button3.Tag = 3;
tableLayoutPanel1.Controls.Add(button3, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (Control c in tableLayoutPanel1.Controls) //iterate through controls
{
if ((int)c.Tag == 1) //if Tag is equal then process
{
if (c is Button)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
((Button)c).Enabled = false;
}
else
{
((Button)c).Enabled = true;
}
break; //if you have multiple controls to process remove this
} //and assign the same tag to the controls you want processed
}
}
}

how to show icon inside a button windowsForms

I want to add icon inside a button. Here is my code
private void Printbutton_Click(object sender, EventArgs e)
{
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
if (SetupThePrinting())
printDocument1.Print();
}
The problem here is that the icon doesn't appear at first , it appears when I click to the button.
What's wrong here ?
you added the icon in printbutton_click event instead defining it in Form initializecomponents
public Form2()
{
InitializeComponent();
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
}
private void Printbutton_Click(object sender, EventArgs e)
{
if (SetupThePrinting())
printDocument1.Print();
}
Like this
public Form1()
{
InitializeComponent();
// Assign an image to the button.
button1.Image = Image.FromFile("C:\\Yourfolder");
// Align the image and text on the button.
button1.ImageAlign = ContentAlignment.MiddleRight;
button1.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
button1.FlatStyle = FlatStyle.Flat;
}
private void button1_Click(object sender, EventArgs e)
{
// Your print code.
}

Double Click on label open Form C#

Im trying to open a new form when a label is double clicked. Im able to drag and drop the label .Im trying to open a new form on double click of label now.
private void control_MouseDown(object sender, MouseEventArgs e)
{
var control = sender as Control;
this.DoDragDrop(control.Name, DragDropEffects.Move);
}
private void control_DoubleClick(object sender, EventArgs e)
{
frm = new Frm();
frm.ShowDialog();
frm.Dispose();
}
EDIT 1:
I have tried both possible answers below, and they have not worked for me?
A more cleaner way is (note I changed Frm to Form1):
private void control_DoubleClick(object sender, EventArgs e)
{
using (Form1 frm = new Form1())
{
frm.ShowDialog();
}
}
You can't add DragDrop on MouseDown and then DoubleClick. That won't work.
I don't think there's an easy way to get around that, but once a control is being dragged, it won't respond to double click messages.
I've made some quick tests, and there's a "hacky" way. It'll make your dragging look weird (since it'll start after some time, instead of immediately after you press the mouse button), but here it goes:
private bool _willDrag = false;
private bool control_MouseUp(object sender, MouseEventArgs e)
{
// disable dragging if we release the mouse button
_willDrag = false;
}
private bool control_DoubleClick(object sender, EventArgs e)
{
// disable dragging also if we double-click
_willDrag = false;
// .. the rest of your doubleclick event ...
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
var control = sender as Control;
if (control == null)
return;
_willDrag = true;
var t = new System.Threading.Timer(s =>
{
var callingControl = s as Control;
if (callingControl == null)
return;
// if we released the mouse button or double-clicked, don't drag
if(!_willDrag)
return;
_willDrag = false;
Action x = () => DoDragDrop(callingControl.Name, DragDropEffects.Move);
if (control.InvokeRequired)
control.Invoke(x);
else
x();
}, control, SystemInformation.DoubleClickTime, Timeout.Infinite);
}
In the form.Designer right click on your label then properties, in the properties window click in events (the thunder icon), in the double_Click event dropdown select the event handler (control_DoubleClick) this method must have two parameters an object and a eventArgs
This is tricky as the DoDragDrop will eat up any further mouse events, and MSDN posting a rather stupid example doesn't help much.
Solution: Do not start the D&D in the MouseDown if you want to still receive click or double click events but use the MouseMove instead:
Replace this
private void control_MouseDown(object sender, MouseEventArgs e)
{
var control = sender as Control;
this.DoDragDrop(control.Name, DragDropEffects.Move);
}
by this:
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
DoDragDrop((sender as Control), DragDropEffects.Move);
}
Don't forget to hook up the new event!

Change WinForms Button Style upon mousedown and leave

How can I change a button's style, in particular the image, upon the mouse being pressed and held and then the cursor dragged away from the button?
You'll notice that, on this action, the default behaviour for the button is to revert it's style to hover style. This can be partially configured using the MouseOverBackColor. I want to ensure that, whenever the MouseOverBackColor is applied, I also have a specific image on the button.
I have tried the code below, to have an "isMouseDown" flag which is checked in the leave event. However, this doesn't work for me.
private void btnFormMinimize_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
bool isMouseDown = false;
private void btnFormMinimize_MouseDown(object sender, MouseEventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_click;
isMouseDown = true;
}
private void btnFormMinimize_MouseEnter(object sender, EventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_hover;
}
private void btnFormMinimize_MouseLeave(object sender, EventArgs e)
{
Button b = (Button)sender;
if (isMouseDown)
{
b.Image = Properties.Resources.icon_minimize_hover;
}
else
{
b.Image = Properties.Resources.icon_minimize;
}
}
private void btnFormMinimize_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown= false;
}
Thanks in advance.

Determining which tabpage has opened

I'm trying to get the tabpage that was clicked by right button of mouse,in another words the tabpage that opened the contextmenustrip.
There's a toolstripmenuitem called Close which I used to close the tab that was clicked on.
I used this code :
public partial class USBrowser : Form
{
private Point lastpoint;
}
private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < browserTabControl.TabCount; i++)
{
Rectangle rec = browserTabControl.GetTabRect(i);
if (rec.Contains(this.PointToClient(lastpoint)))
closeTab(i);//this function closes the tab at specific index
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
if (e.Button == MouseButtons.Right)
lastpoint = Cursor.Position;
}
I also added(when adding the tabpage) :
browserTabControl.TabPages.Insert(browserTabControl.TabCount - 1,WebPage);
browserTabControl.SelectTab(WebPage);
browserTabControl.SelectedTab.MouseClick += SelectedTab_MouseClick;
void SelectedTab_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
lastpoint = Cursor.Position;
}
The problem is that the lastpoint is always (0,0) !!
Why ?
Any other suggested idea is welcomed
thanx in advance
None of these event handlers will actually run. Not the form's OnMouseClick() method since you are not actually right-clicking the form. And not the tab page's MouseClick event handler since you gave the TabControl a context menu. So lastpoint being empty is the expected outcome.
It is not clear how you want this context menu to work. If you use it by right-clicking the tab page then it is simple, just destroy the selected page:
private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
tabControl1.SelectedTab.Dispose();
}
If you activate it by right-clicking a tab, one that isn't selected, then it gets more complicated. You have to memorize which tab was clicked on, do so by using the context menu's Opening event:
private TabPage RightClickedTab;
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
RightClickedTab = tabControl1.SelectedTab;
var pos = tabControl1.PointToClient(Cursor.Position);
for (int tab = 0; tab < tabControl1.TabCount; ++tab) {
if (tabControl1.GetTabRect(tab).Contains(pos)) {
RightClickedTab = tabControl1.TabPages[tab];
break;
}
}
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
if (RightClickedTab != null) RightClickedTab.Dispose();
}

Categories

Resources