How can I Display UserControl into a form - c#

I have created a UserControl and I want when I click on some button to display this UserControl into my form.
Is there any way to do that ?

You can dynamically add the user control to the forms Controls collection.
For example, in the button click event handler:
MyUserControl uc = new MyUserControl();
uc.Dock = DockStyle.Fill;
this.Controls.Add(uc);

Put it on the form. Start it out invisible, when the button is clicked set it to visible.

Drag your user control to your form and change it's Visible property to false
UserControl1.Visible = false;
Then on your button set the Visibility of your usercontrol to true.
private void button1_Click(object sender, EventArgs e)
{
UserControl1.Visible = true;
}

Is it a typical ascx type of control? If yes then you can set the "visible" property of the control through the button click event.
Let say you have :
<uc1:ft ID="userctrl" runat="server" Visible="false" />
then on your button event:
protected void Button1_Click(object sender, EventArgs e)
{
userctrl.Visible = true;
}

Related

Control keys not working while window is disable and again Enable

I have a XAML MainWindow and Child User control loaded on that window. On button click event in user control I disable mainWindow as Well as User Control till a work on button click is not finished. When I again updating the property from User control to enable the window again, now user control keys are not working, because while debugging its showing IsEnabled Property of window/user control is still disable. I am also forcefully updating user control property this.IsEnabled = true; but still its not updating IsEnabled property of class true.
How could I update the mainwindow and user control property to enable or how could my Control keys can work again.
If you set mainWindow.IsEnabled to false, you cannot set userControl.IsEnabled to true. You have to set the mainWindow.IsEnabled to true first. You cannot have an enabled control inside a disabled control.
I wrote a simple WPF app with a user control with a button. The main window has the user control and the code for the user control is:
public partial class MyUserControl : UserControl
{
DispatcherTimer m_timer = new DispatcherTimer();
public MyUserControl()
{
InitializeComponent();
m_timer.Tick += Timer_Tick;
m_timer.Interval = TimeSpan.FromSeconds(4);
}
private void Timer_Tick(object sender, EventArgs e)
{
Window window = Window.GetWindow(this);
window.IsEnabled = true;
m_timer.Stop();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window window = Window.GetWindow(this);
window.IsEnabled = false;
m_timer.Start();
}
}

Parent Panel Click Event

I have a parent panel in a form.
This panel will display an array of different user control which will take up the full panel dimension.
I tried to use the panel 'Click' event. However when the user control is added to the panel, it does not fire the event when click.
Due to there are many widget in each user control, it would be tedious to implement 'Click' on each widget.
Is there anyway when i click on a user control, it fires the form panel event instead?
I suggest to loop the panel's controls on the form loading:
private void MyClick(object sender, EventArgs e) {
...
}
private void MyForm_Load(object sender, EventArgs e) {
foreach (Control control in myPanel.Controls)
control.Click += MyClick;
...
}

Why does my dynamically created user control doesn't fire button click event

i have a problem with user control.
i create it dynamically on my aspx page after clicking on a button:
protected void btnAddRules_Click(object sender, EventArgs e)
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
MyPanel.Controls.Add(Control);
}
when i click on a button of my user control, the button event wont fire and the user control will disappear. here is the button event:
protected void btnAdd_Click1(object sender, EventArgs e)
{
WowzaRule rule = GetRuleFromGUI();
RuleList.Add(rule);
//Session["RuleList"] = RuleList;
//List<WowzaRule> test = new List<WowzaRule>();
SaveToXMLFiles(txtdbnum.Text, RuleList);
}
i understand that after pressing the button on mypage the usercontrol is released and if its not created on pag_init or page Load it wont stay, but i need to create it on my button click event and find a way for it not to disapper.
thanks in advance, Daniel
You might have to add an event handler that it can fire the click event and call your delegate
Control.Click += btnAdd_Click1;
Dynamically created controls, once added, have to be on a page on every page load in order to work correctly. What happens in your case:
RuleProperty is added after the button click
Page loads with this control
User clicks on the button within RuleProperty
Control is not added to the control tree during the page load (corresponding code is only in the button click handler, and that button was not clicked)
ASP.NET does not know which control triggered the event, so the event is not processed
To go around this issue you need to add you control on every page loading, for example using some flag stored in ViewState:
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["AddRuleProperty"] != null && (bool)ViewState["AddRuleProperty"])
{
AddRulePropertyControl();
}
}
protected void btnAddRules_Click(object sender, EventArgs e)
{
AddRulePropertyControl();
ViewState["AddRuleProperty"] = true;
}
private void AddRulePropertyControl()
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
MyPanel.Controls.Add(Control);
}
Update.
If you want to remove the control from the page later on in the control's click handler, you need to remove corresponding ViewState key. This is not possible from the control directly, since property Page.ViewState is protected, and also this would have created an unwanted dependency.
What seems as the right way to do this is to subscribe to the very same event from the Page (you might need to make this event visible from the controller) and reset the key in there. Like this:
private void AddRulePropertyControl()
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
Control.ButtonClick += RuleProperty_ButtonClick;
MyPanel.Controls.Add(Control);
}
private void RuleProperty_ButtonClick()
{
ViewState["AddRuleProperty"] = false;
}
Please note that event name here is not real, this is just a sketch of what can be done.

Hide image when click outside of image in wpf

When my WPF application loads, an image is shown in center.
how can i handle mouse click outside of image. when user clicks outside of image it hide.
My code is in c#.
You can simply add a handler to the top level control, eg. Grid, Window, etc. In that handler, you can check whether the control that was clicked on was the Image and if it wasn't, then you could hide it:
The XAML:
<Grid PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown">
...
<Image Name="TheImage" Source="/WpfApplication2;component/Images/Add_16.png" />
...
</Grid>
The code behind:
private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource != TheImage)
{
TheImage.Visibility = Visibility.Hidden;
}
}
While this fulfils your requirements, it should be noted that once hidden, the Image will no longer be 'clickable'.
just put the code MyImage.Visibility = System.Windows.Visibility.Hidden; in whichever event you want to capture.
for eg:
private void MyButton_Click_1(object sender, RoutedEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
private void MyTextbox_PreviewMouseDown_1(object sender, MouseButtonEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
private void MyWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
where MyImage is the name of image, MyTextbox is the name of textbox, MyButton is the name of Button and MyWindow is the name of Main Window
you can simply write
YourImageName.Visibility = Visibility.Hidden;
Write this code to any of the control i.e. TextBox or Button.
first click on the form to select it. then go to the event's section (near property section) and double-click the click event to make a function call when a form is clicked. in the code compare the sender object with the IsEqual(obj) method and see the sender is same image or it's not. if not hide it. :)
You can handle the click event of Application current window , and inside that event you can check if the mouse position is within that image or outside of that image , on basis of this you can set your image visibility condition you prefer .
Code sample example :
//registering event
Application.Curent.mainWindow.MouseRightButtonDown += MainWindow_MouseRightButtonDown;
//event implementation
void MouseRightButtonDown(object sender , MouseButtonEventArgs e)
{
//here you can check the ui element for image control using sender
//below will let you know the position of Click
e.GetPosition(// pass the ui element here)
}
Note : The above code shown is at app level click handling . If you
dont want at app level you can take the parent xaml within which the
image is present and do the same

Auto expand the window by pressing a button

I am using “Microsoft Visual Studio 2010” and C# language. My user interface look like this(before user click the Advance button):
If user click Advance button, I want it to show the rest of the window as shown in the picture bellow:
Can you please tell me how can have all these information hidden till the user click the Advance button? How can I have a smaller window first, as shown in the first figure. And when the user press the advance button, it will expand and show the rest.
If you can show me with details, I would really appreciate it
All WinForms controls, including the Form itself, have an AutoSize property. When set to true, it causes the control to automatically resize itself to fit its contents.
Therefore, you should place your "advanced" controls into a UserControl and add that UserControl to your form (or you can use a Panel if you're lazy). Then, when the "Advanced" button is clicked, toggle the visibility of your UserControl. The form should automatically adjust its size accordingly.
Alternatively, you could add SplitContainer to your form, which has the ability to collapse one of its two panels. The "Advanced" button would then toggle the state of the Panel2Collapsed property to expand/collapse the bottom panel.
Note: Grammatically, the caption of that button should be "Advanced", not "Advance". For an improved user experience, I recommend adding some kind of indicator that the button expands the available information on the window, rather than submitting it or opening a second window. Most "expander" buttons accomplish this using a downward-facing arrow, e.g.
You could use an image for this, or a Unicode glyph. For example, ▼, the black down-pointing triangle. Change it to an upward-pointing triangle when the panel is expanded.
1.Add a panel to bottom of your form and add all the controls that you need to display in the advanced button click.
2.change the following properties of both the panel and your form,
> AutoSize >> true
> AutoSizeMode >> GrowAndShrink
3.then in form load event you can use like following
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible = false;
}
4. then in advanced button click event
private void button1_Click_1(object sender, EventArgs e)
{
//panel1.Visible = true;
string value1 = button1.Text;
switch(value1)
{
case "Expand":
panel1.Visible = true;
break;
case "Reduce":
panel1.Visible = false;
break;
}
button1.Text = "Reduce";
if(panel1.Visible==true)
{
button1.Text = "Reduce";
}
else if(panel1.Visible==false)
{
button1.Text = "Expand";
}
}
At first set the following properties visible false
like all lebels and text boxs. then in the click event of the advanced button set all properties visible true.
OnLoad event of your first form set every control or groupbox (whichever you are using) visibility as false.
And on advance buttonclick event make its visibility true.
Code as follows:
private void FirstForm_Load(object sender, EventArgs e)
{
controlName.Visible=false;
}
private void btnAdvance_Click(object sender, EventArgs e)
{
controlName.Visible=true;
}
MSDN For Visibility Property:
http://msdn.microsoft.com/en-IN/library/system.windows.uielement.visibility.aspx
Hope its helpful.
you can simply do it like this,
1.Add a panel to bottom of your form and add all the controls that
you need to display in the advanced button click.
2.change the following properties of both the panel and your form,
> AutoSize >> true
> AutoSizeMode >> GrowAndShrink
3.then in form load event you can use like following
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible = false;
}
4. then in advanced button click event
private void button1_Click_1(object sender, EventArgs e)
{
panel1.Visible = true;
}
Hope this will help you and any other need this in future...!

Categories

Resources