If i click to button , my below code works very well.
if (Application.OpenForms["StockCardForm"] == null)
{
var stockCardForm = new StockCardForm();
stockCardForm.MdiParent = this;
stockCardForm.Show();
}
else
Application.OpenForms["StockCardForm"].Focus();
Instead of "StockCardForm" how can i give form name dynamic or how can i prevent to open same forms second time as dynamic ?
Any help will be appreciated.
Thanks.
You can check if there are any forms of some type already open and then do whatever you want.
if (!Application.OpenForms.OfType<StockCardForm>().Any())
{
var form = new StockCardForm();
form.Show();
}
else
Application.OpenForms.OfType<StockCardForm>().First().Focus();
Related
I am using MVVM for my WPF form and now I want to close the new Dialog that I made when a user presses the Cancel button.
The cancel button is part of a seperate XAML that gets used around multiple forms. (similar to scripts in javascript)
this is what I have so far:
private void CloseDialogView(object sender)
{
var currentElement = (DependencyObject)sender;
List<object> windowTypes = new List<object>() {
typeof(fooDialogView),
typeof(barDialogView),
typeof(foobarDialogView) };
Type elementType;
do
{
var parent = VisualTreeHelper.GetParent(currentElement);
currentElement = parent;
elementType = (currentElement.GetType());
}
while (!windowTypes.Contains(elementType));
foreach (var type in windowTypes)
try
{
var Window = (type.GetType())currentElement;
Window.Close();
}
catch
{ }
}
in the do-while I just pass through all the elements in the form untill I hit the element that is the window.
In the foreach I want to check if it is one of the windowtypes (Xaml-forms) and if it is, cast that type to the 'currentElement' and then close it.
It does work if I just do
var Window = (fooDialogView)currentElement;
Window.Close();
but I want to avoid having to manually enter each seperate form-name.
Someone posted the answer
var Window = (Window)sender;
Window.Close();
but it turned out all I needed was
var Window = (Window)currentElement;
Window.Close();
right after the do-while, no need for the foreach-loop.
thanks for the swift help :)
I'm fairly new to C# (and programming in general) so stick with me if I make any huge errors or talk complete bull.
So what I'm trying to do is have a private void that resizes the background image of a button. I send the name of the button to the private void via a string. Anyway, the code looks something like this:
ButtonResize("Zwaard");
protected void ButtonResize(string Button)
{
string ButNaam = "btn" + Button;
Button Butnaam = new Button();
Butnaam.Text = ButNaam;
if (Butnaam.BackgroundImage == null)
{
return;
}
else
{
var bm = new Bitmap(Butnaam.BackgroundImage, new Size(Butnaam.Width, Butnaam.Height));
Butnaam.BackgroundImage = bm;
}
}
But it doesn't work like that. I can't seem to find a way to declare a new object named the value I have in a string. What I want my code to do is instead of making a button called "Butnaam", I want it to create a button called btnZwaard (the value of the string Butnaam).
How do I tell C# I want the value of the variable to be the name of a new button, not literally what I type?
Thanks in advance.
Are you looking for something like this? By passing the Button to the method you can then act on the object. If this is what you are looking for then you should read Passing Reference-Type Parameters
protected void ButtonResize(Button button)
{
if (button != null && button.BackgroundImage != null)
{
button.BackgroundImage = new Bitmap(button.BackgroundImage, new Size(newWidth, newHeight));
}
}
A string is a piece of text. You subsequently refer to it as a class, which is wrong. Assuming it were right you create a new button rather than "resize its image".
What you want to do to get you started is create a new function in the same class as the dialog that has the button. That function can resize the image of the control.
Edit: this doesn't seem like a good starting point for learning a language, btw. Please find a good online tutorial for starting in C# (e.g. a hello world application).
I want to build my own web browser, but I'm stuck on operation for adding new tab, does anyone have any idea to make it done?
The final result will should like this.
you can try this:-
if (tabControl1.SelectedTab.Text == "+")
{
AddNewTab();
}
foreach (Control item in tabControl1.SelectedTab.Controls)
{
if (item.GetType() == typeof(WebBrowser))
{
WebBrowser wb = (WebBrowser)item;
toolStripButton1.Enabled = wb.CanGoBack;
toolStripButton2.Enabled = wb.CanGoForward;
}
}
The way I'd go about it would be (using a TabControl or similar) to create a special tab with just the plus icon you want. Then handle the tab changed event, check if you've switched to the special tab, and if so, cancel the tab change, create a new tab and set that to be displayed instead.
I have a unique c# source file named source.cs that i compile using CSharpCodeProvider from a builder to get an executable.
I would put an option on the builder whether to display the About form on application startup or not.
How can i create a form with title as About Us then add controls within (Labels, RichTextEdit etc..)
Something like
if (display_about_dialog) {
// code to display the form }
Any help would be highly appreciated
Try something like this:
using (Form form = new Form())
{
form.Text = "About Us";
// form.Controls.Add(...);
form.ShowDialog();
}
Here's the documentation page for the System.Windows.Forms.Form class.
if you have a class MyForm : System.Windows.Forms.Form (that you create using windows form builder)
You can do
MyForm form = new MyForm();
form.Show();
To launch an instance of MyForm.
Though if you want to create a simple confirmation or message dialog, check out the many uses of MessageBox
MessageBox.Show("text");
MessageBox.Show("text", "title", MessageBoxButtons.OKCancel);
Form aForm = new Form();
aForm.Text = #"About Us";
aForm.Controls.Add(new Label() {Text = "Version 5.0"});
aForm.ShowDialog(); // Or just use Show(); if you don't want it to be modal.
Form is a class which you can instantiate like any other, set it's properties, call it's methods.
I'm new to Visual Studio 2010 and I'm planning to create a Timekeeping system. I'm just want to ask how could I create a form that compose 2 forms in it. For example, if I will click a button it will open a new form inside a form. Please help. Thanks
Form formA = new Form();
formA.IsMdiContainer = true;
Form formB = new Form();
formB.MdiParent = formA;
formB.Show();
You have to work with MDI (Multiple Document Interface), have alook at this article that might help.
You could create custom form, remove all borders, and toolbars to make it look as closely to a panel as possible. Then make that new custom form a MdiContainer / MDI-panel and show forms in that panel, something like the code below will do the job
Mdi-Panel definiton:
public class MdiClientPanel : Panel
{
private Form mdiForm;
private MdiClient ctlClient = new MdiClient();
public MdiClientPanel()
{
base.Controls.Add(this.ctlClient);
}
public Form MdiForm
{
get
{
if (this.mdiForm == null)
{
this.mdiForm = new Form();
/// set the hidden ctlClient field which is used to determine if the form is an MDI form
System.Reflection.FieldInfo field = typeof(Form).GetField("ctlClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
field.SetValue(this.mdiForm, this.ctlClient);
}
return this.mdiForm;
}
}
}
Usage:
/// mdiChildForm is the form that should be showed in the panel
/// mdiClientPanel is an instance of the MdiClientPanel
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
I think, this is a very easy way:
Form1 form= new Form1 ();
form.TopLevel = false;
this.Controls.Add(form);
form.Show();
Maybe MDI interface will do what you want..
Here's a tutorial to do that.