I've been on this for a while but can't get it to work.
I came from C# programming within Unity3D, but now need to build something in "normal" C# now through Visual Studio.
So I've got this button in C#, which I want to give a background. No biggy.
But the problem is, I want the background to expand itself.
So I want an image like this: !http://i39.tinypic.com/2wphvo6.png combined with a button so it becomes like this: !http://i42.tinypic.com/dzxlom.jpg
In Unity3D it was easy, but I don't know how to get it to work in Visual Studio C#. Hope someone can help me. I don't even know how to properly call this image expanding to button thing...
edit: Windows Forms
Assuming you want the change the picture on mouse over you can do something like this using events
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
this.button1.Size = button1.BackgroundImage.Size;
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
this.button1.Size = button1.BackgroundImage.Size;
}
You need to set the BackgroundImageLayout property.
public Form1()
{
InitializeComponent();
button1.BackgroundImageLayout = ImageLayout.Stretch; //Or maybe you want Zoom
}
You can also set the property in the design view of the form.
Related
I am developing a C# windows forms application that supports multiple languages. When I change the language to Arabic, everything must be from right to left. this causes a flickering problem in my form. Any ideas on how to fix that?
This the code I am using:
private void arabicSAToolStripMenuItem_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ar-SA");
reset();
tabPatientCard.RightToLeft = RightToLeft.Yes;
tabs.RightToLeft = RightToLeft.Yes;
paneltabs.Dock = DockStyle.Right;
}
please for a little help in displaying the panel at the click of a button, namely in my application I have a FormMenu in which the panelCentralForm is displayed when loading.
When I click on btnListOfActiveUser_Click I manage to open FormListStaff over panelCentralForm using this code:
private void btnListOfActiveUser_Click(object sender, EventArgs e)
{
FormListStaff formListStaff = new FormListStaff();
AddFormToPanel(formListStaff);
}
private void AddFormToPanel(object form)
{
if (this.panelCentralForm.Controls.Count > 0)
this.panelCentralForm.Controls.RemoveAt(0);
Form fh = form as Form;
fh.TopLevel = false;
fh.FormBorderStyle = FormBorderStyle.None;
fh.Dock = DockStyle.Fill;
this.panelCentralForm.Controls.Add(fh);
this.panelCentralForm.Tag = fh;
fh.Show();
}
my question is how can I in a similar way that now when I click on buttonDashboard to return to me the first view, ie the view from the Central Form panel (panelCentralForm).
private void buttonDashboard_Click(object sender, EventArgs e)
{
panelCentralForm.Visible = true; //something like this, p.s. this code doesn't just work as an example I wrote
}
the whole view belongs from FormMenu.cs
Thanks a lot to everyone for the help
my question is how can I in a similar way that now when I click on
buttonDashboard to return to me the first view
Just Clear() the panel, then add the label back in?
this.panelCentralForm.Controls.Clear();
this.panelCentralForm.Controls.Add(label1);
The label should still be accessible even though it was previously removed from the panel since it is declared at form level in your designer file.
I've watched a few tutorials online on how to change tabs using buttons but for some reason all the code I've tried doesn't work. I am currently running Microsoft Visual Studio 2017 and am trying to write some code for a button to change tabs.
I couldn't find any differences between my code and code shown in tutorials, so it may just be a Visual Studio setting that I haven't set up correctly to allow the button correctly, but I couldn't figure out if or where it may be.
Here's my current code:
//Element event handlers
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
tabControl.SelectedTab = DestSelect;
}
private void buttonGotoIntro_Click(object sender, EventArgs e)
{
tabControl.SelectedTab = Intro;
}
//An old computer-generated segment code for the previous button.
//When I try to remove it the computer gets mad at me.
private void GotoIntro_Click(object sender, EventArgs e)
{
}
Please confirm you have subscribed to the Click event for the buttons.
public Form1()
{
InitializeComponent();
buttonStart.Click += buttonStart_Click;
buttonGotoIntro.Click += buttonGotoIntro_Click;
}
Instead of 'tabControl.SelectedTab = DestSelect;" try instead the method 'tabControl.SelectTab(DestSelect);'
I read through this article to find your (hopefully) answer:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/2cf22896-c5bd-4a9b-ab61-34404b55ef01/how-to-jump-to-a-specific-tab-in-the-tabcontrol?forum=vbgeneral
I assume u want to select a tab when a different Button is clicked.
tabControl.SelectedIndex = [Index of tab to switch to];
Code should look like;
tabControl.SelectedIndex = feeTabIndex;
If this is not clear enough, tell me exactly what you want to do.
I'm trying to make a button on my windows form create a new page when clicked, something like when you are going through an installation of a program and you would click "Next" and it would take you to a new page but not bring up an entirely seperate window. I would also have another button that would be pressed to bring up the original form.
I've looked everywhere for a answer to this so any help on how I would create this would be greatly appreciated.
private void Cleaning_Click(object sender, EventArgs e)
{
// executes new page
}
What you want to do is to create a wizard. There are lot of samples on internet regarding that. Take a look;
http://www.codeproject.com/Articles/31770/Wizard-Form-Implementation
Or
You can see that SO question
Creating Wizards for Windows Forms in C#
One simple way is to use panels in a single form. The only problem with panels is that it is hard to edit the layout and also your code would become very messy.
it IS simple BUT has a very bad downside
Simply we can add one groupbox and add your controls, in form load set groupbox visible to false and button click event visible to true something like....
private void Form3_Load(object sender, EventArgs e)
{
groupBox1.Visible = false;
}
private void button1_Click_1(object sender, EventArgs e)
{
if (button1.Text == "&Show")
{
button1.Text = "&Hide";
groupBox1.Visible = true;
}
else if (button1.Text == "&Hide")
{
button1.Text = "&Show";
groupBox1.Visible = false;
}
}
Have a simple form that has a PictureBox in one location. I want to change the cursor to the Cross cursor when entering that control, and change it back when it leaves.
private void Canvas_MouseEnter(object sender, EventArgs e)
{
this.Canvas.Cursor = Cursors.Cross;
}
private void Canvas_MouseLeave(object sender, EventArgs e)
{
this.Canvas.Cursor = Cursors.Default;
}
This doesn't work. If I look closely, I can see it quickly change on MouseEnter, but it flips right back to the default cursor. I have to add "this.Canvas.Cursor = Cursors.Cross;" to the MouseMove event in order for it to work, but then I can constantly see it flickering back to the default cursor.
What gives? This is the only cursor-related code in my whole application, what would be causing it to reset to the default cursor every time I move the mouse?
Thanks.
EDIT: I am an idiot. Person I am collaborating with on this little app had some cursor code tucked away somewhere else that was causing the problem. Thanks guys.
Why don't you set the cursor for the picturebox?
yourPictureBox.Cursor = Cursors.Cross;
I've tried in a new project from scratch (with just the mouseenter/leave handlers and nothing else) and it works.
Might be something else in your application ?
public Form1()
{
InitializeComponent();
pictureBox1.MouseHover += new EventHandler(PictureBox1_MouseHover);
}
void pictureBox1_MouseHover(object sender, EventArgs e)
{
this.PictureBox1.Cursor = Cursors.Cross;
}
You want to use MouseHover event handler.