wpf prevent second click button - wpf

I have a problem again. when I click button, window appears. when I click button again same window appears again. I want when I click button first time, page appears. but I want to prevent second click. can anyone help me with this problem? thanks in advance.
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
}

set a simple boolean value to check if the window is already open?
private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
if (!isWindowAlreadyOpen)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
isWindowAlreadyOpen = true;
}
}
Should do the trick.
EDIT
You'll have to register the closed event of the window to unset the boolean:
private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
if (!isWindowAlreadyOpen)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
dic.Closed += Dictionary_Closed;
isWindowAlreadyOpen = true;
}
}
private void Dictionary_Closed(object sender, EventArgs e)
{
isWindowAlreadyOpen = false;
}
EDIT2
Alternatively, you can use dic.ShowDialog() if you want this window to be topmost and only one instance.

Related

Stop further link navigating inside web browser control [WPF]

Hello is there something similar to AllowNavigation like in WinForms?
My search didn't yield any satisfying result.
Basically I'm trying to open a webpage inside new wpf window and stop user from clicking random links on that webpage and navigating further.
Saw something with
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}
Rest of the code is :
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
browser1.Navigating += browser1_Navigating;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
But it just makes my webpage not display ?
Thanks
Try this:
Once you have loaded your page, then you assign the browser_Navigating event handler.
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
browser1.Navigating += browser1_Navigating;
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}

how to clear an image on button click

How do I clear an Image with the click of a button. I already use bt_ClearOnclick to remove data from text boxes.
private void btClear_Click(object sender, RoutedEventArgs e)
{
txtId.Text = String.Empty;
txtvalue.Text = String.Empty;
image?
private void btClear_Click(object sender, RoutedEventArgs e)
{
txtId.Text = String.Empty;
txtvalue.Text = String.Empty;
image.Source = null;
}

Change the location of the window form in c#?

In window application, using c# i created one form and put visible false minimize, maximize button and formborder to none, i place one panel at top of the form, in that panel i place close, minimize buttons. Now how can i drag the window form. Any reference please. my code is
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.FormBorderStyle = FormBorderStyle.None;
Thank you.
Simply register the MouseDown, MouseMove and MoueUp events for your Panel
bool MouseDownFlag = false;
Point start = new Point(0, 0);
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
MouseDownFlag = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseDownFlag)
{
Point newPoint = new Point();
newPoint.X = this.Location.X - (start.X - e.X);
newPoint.Y = this.Location.Y - (start.Y - e.Y);
this.Location = newPoint;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
MouseDownFlag = false;
}

Disable menuItem while ModalDialog is shown and enable when window is closed?

I am implementing an application, and if I click on a menu item:
<MenuItem Name="menuAlgemeneGeg" Header="Algemene gegevens" Click="AlgemeneGegevensClick" />
the method is :
private void AlgemeneGegevensClick(object sender, RoutedEventArgs e)
{
ToetsBeheerViewModel vm = (ToetsBeheerViewModel)this.DataContext;
EditAlgemeneGegevens window = new EditAlgemeneGegevens(vm.Examination);
window.ShowDialog();
menuAlgemeneGeg.IsEnabled = false;
}
Now what I want is, that when you click on it the menuitem is disabled. But from the moment the user closes this window it must be enabled again. I can disable the menuitem but can't change this back to enable it.
Someone who can help me out please?
you can handle window closed event.. and enable menu in even handler
private void AlgemeneGegevensClick(object sender, RoutedEventArgs e)
{
ToetsBeheerViewModel vm = (ToetsBeheerViewModel)this.DataContext;
EditAlgemeneGegevens window = new EditAlgemeneGegevens(vm.Examination);
window.Closed += new EventHandler(Window_Closed);
window.ShowDialog();
menuAlgemeneGeg.IsEnabled = false;
}
void Window_Closed(object sender, EventArgs e)
{
menuAlgemeneGeg.IsEnabled = true;
}

WPF mediaelement

I have a MediaElement, but how can I call a function when the property "position" of MediaElement changes?
Position is not a DependencyProperty.
You can use a DispatchTimer. This article provides some good insight on how to get this working. MediaElement and More with WPF.
Here is some sample code that I took from a project I'm working on. It shows the position of the video using a slider control and allows the user to change the position.
I'm a bit of a newbie too, so it is possible that some of it is wrong (feel free to comment on problems in the comments section :).
private DispatcherTimer mTimer;
private bool mIsDragging = false;
private bool mTick = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
medPlayer.Play();
medPlayer.Stop();
mTimer = new DispatcherTimer();
mTimer.Interval = TimeSpan.FromMilliseconds(100);
mTimer.Tick += new EventHandler(mTimer_Tick);
mTimer.Start();
}
void mTimer_Tick(object sender, EventArgs e)
{
if (!mIsDragging)
{
try
{
mTick = true;
sldPosition.Value = medPlayer.Position.TotalMilliseconds;
}
finally
{
mTick = false;
}
}
}
private void sldPosition_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
mIsDragging = true;
medPlayer.Pause();
}
private void sldPosition_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
mIsDragging = false;
if (chkPlay.IsChecked.Value)
medPlayer.Play();
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var pos = TimeSpan.FromMilliseconds(e.NewValue);
lblPosition.Content = string.Format("{0:00}:{1:00}", pos.Minutes, pos.Seconds);
if (!mTick)
{
medPlayer.Position = TimeSpan.FromMilliseconds(sldPosition.Value);
if (medPlayer.Position == medPlayer.NaturalDuration.TimeSpan)
{
chkPlay.IsChecked = false;
medPlayer.Stop();
}
}
}

Resources