How to refresh a page or window in wpf? - c#

I cannot seem to refresh or reload a parent window or page in wpf. I have tried the following:
private void btClear1_Click(object sender, RoutedEventArgs e)
{
//this.LayoutUpdate();
//Refresh();
//this.NavigationService.Refresh();
}

You can achieve complete layout/render pass with InvalidateVisual call (http://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx)

Related

Dynamic Button Click on Tab Control - Tab Pages C#

I have an application that needs to click on button located on tab pages. So for example:
Tabcontrol has Tabpage1, Tabpage2, .... TabpageN
and each tabpage has a buttonX that performs a taskX.
Now supposing, I'm on TabPage1 and I want to Click ButtonX on Tabpage3 without changing tabindex/selectedindex. How can I do that?
Generally one would use PerformClick but I've noticed it failing on TabControl. So if not dependent on EventArgs, create a sub method for the click event e.g.
private void button1_Click(object sender, EventArgs e)
{
Button1PerformClick();
}
private void Button1PerformClick()
{
MessageBox.Show("Button1");
}
Than calling
private void button2_Click(object sender, EventArgs e)
{
Button1PerformClick();
}
Doesn't matter in regards to which tab the button is on in this case.

C# PerformClick method in invisible form

I am stuck on using a PerformClick method. I have a main form which is called mymainform, and I have some subforms. When loading main form, I am creating subforms and hide them with Form Visibility and access some elements on subforms.
My problem is clicking a button on subform1 from MainForm. I have written the below code and it didn't work. Normally DayModeButton changes the boolen; isDay = !isDay
After clicking the button1 on mainform it doesn't change the isDay boolen.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
}
If I write this code it works, but I don't want to show and hide the form because it is not a good view for users.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.Visible=true;
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
mysubform1.Visible=false;
}
Can anyone help me in performing a Click event in invisible forms?
Thanks
Make sure your DayModeButton is Public and not Private.

How to implement usercontrol to be visible on click inside one panel (DockPanel)?

I am making a basic application in WPF (C#) where I wanted to use UserControl. I already created 3 sample UserControl and a single page window which have buttons, labels, panel etc. I wanted to load each UserControl on click button. You can see my logic (code) below -
public partial class Dashboard : Window
{
public Dashboard()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
Dock_MainPanel.Controls.Clear();
Dock_MainPanel.Visible = true;
Sample1 usr1 = new Sample1();
usr1.Show();
Dock_MainPanel.Controls.Add(usr1);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
//SAMPLE CODE
}
}
Now my issue is this code is not working. It is stating some error. It is shown in image as below -
Error shown in image
Basically I wanted to load each UserControl on clicking of their respective button. If anyone having other solutions, it is welcomed.
As #Stefan W. suggests you should add an instance of the UserControl to the DockPanel's Children collection but if you intend to add several elements to the same DockPanel you probably also want to set the attached Dock property of the element using to specify where in the DockPanel the element will be located. You can do this using the DockPanel.SetDock method. Also, to make the DockPanel visible, you set its Visibility to Visibility.Visible. Try this:
private void btn1_Click(object sender, RoutedEventArgs e)
{
Dock_MainPanel.Children.Clear();
Dock_MainPanel.Visibility = Visibility.Visible;
Sample1 usr1 = new Sample1();
DockPanel.SetDock(usr1, Dock.Right);
Dock_MainPanel.Children.Add(usr1);
}
You should access the 'Children' collection in DockPanel
Dock_MainPanel.Children.Clear();
Dock_MainPanel.Children.Add(usr1);

set title of tab in web browser

I am trying to make a web browser with multiple tabs. But now, I have a problem with the DocumentTitle for the name of the tabs.
The problem here is that the code to name the tab is performed before it loads the page. I tried to find a way to perform it after, but it doesn't work.
For example:
private void stackoverflowToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser) tabControl1.SelectedTab.Controls[0]) .Navigate("Http://www.stackoverflow.com/");
Browser_Navigated(null, null);
}
void Browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
tabControl1.SelectedTab.Text = ((WebBrowser)tabControl1.SelectedTab.Controls[0]).DocumentTitle;
}
The WebBrowser class exposes a DocumentTitleChanged event that you can use to update the tab title.

Designing a dynamic panel

I am new to C# and I want to design a GUI for a image processing application in c#. I have a very basic rudimentary layout designed as shown below
Here, the image plane is fixed and it will show a live stream video. I have designed all the buttons frame and the side panel. But I do not know how to dynamically change the side panel for each button I click. For example, If I click button1_1, I want some things in the side panel and for button1_2, some other things in it. How do I go about doing it.
EDIT:
Thanks for the answers. I see tab controls is an option. But I want a new panel evertime a click a button. which can further open forms. Is it possible?
OK, let's see. It's easy to do with "TabControl" or array of "Panel"s.
1.Do it with TabControl.
You can design GUI in TabControl in multiple subTabs(if you don't know how please ask.). Then you change it in button click event, to make subTab you wanna show(which means make it visiable and not visiable for other subTabs.)
2.Do it with array of panel.
You can use panel[] panels. In button click event, you hide other panels and show the one you want.
Hope answer helps you!
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 0;
tabControl1.TabPages[0].Text = "First";
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 1;
}
private void button3_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 2;
}
you may want to add split container in your form.
Create UserControl for each buttons.
Code for the button click event
//Button1Click Event
private void button1_Click(object sender, EventArgs e)
{
UserControl1 m_UserControl = new UserControl1();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(m_UserControl);
}
//Button2Click Event
private void button2_Click(object sender, EventArgs e)
{
UserControl2 m_Usercontrol2 = new UserControl2();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(m_Usercontrol2);
}
you can do this if you want to change what usercontrol display in a panel at run time.
Correct me if i misunderstood your question.
In WinForms, you could use a tab control and just change the selected tabs index when a button is pressed. More specifically, when its click event is fired. Here is a good tutorial on using the TabControl and here is a tutorial on wiring up click events.
EDIT:
This is a better tutorial.
Since you can't hide the tabs of a tabcontrol without using WPF, you may need to use something else, if you don't like the way they look. A good workaround if you only have a couple of buttons and thus views, would be to use panels. When button one is clicked show panel one and hide panel two, etc. Here would be the code:
private void button1_Click(object sender, EventArgs e)
{
pane2.visible = false;
pane1.visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
pane1.visible = false;
pane2.visible = true;
}
Hope this helps you!

Categories

Resources