In my code of RichTextBox in my Windows Phone application I have:
var link = new Hyperlink();
if (!string.IsNullOrEmpty(linkUrl))
{
link.NavigateUri = new Uri(linkUrl);
}
link.Foreground = new SolidColorBrush(Colors.Blue);
link.TargetName = "_blank";
var linkText = new Run() { Text = linkDesc };
link.Inlines.Add(linkText);
link.Click += new RoutedEventHandler(NavidateTo);
paragraph.Inlines.Add(link);
private static void NavidateTo(object sender, RoutedEventArgs routedEventArgs)
{
if (MessageBox.Show(
Constants.BrowserNavigating,
"",
MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
{
//cancel Navigation
}
else
{
StateManager.Set("Browser", "true");
}
}
How can I cancel navigation in NavidateTo method?
Update
private static void NavidateTo(object sender, RoutedEventArgs routedEventArgs)
{
if (MessageBox.Show(
Constants.BrowserNavigating,
"",
MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
{
//cancel Navigation
var phoneApplicationFrame = Application.Current.RootVisual as PhoneApplicationFrame;
if (Application.Current.RootVisual as PhoneApplicationFrame != null)
phoneApplicationFrame.Navigating += new NavigatingCancelEventHandler(NavigationService_Navigating);
}
else
{
StateManager.Set("Browser", "true");
}
}
public static void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
{
e.Cancel = true;
}
}
This doesn't help
Use this.NavigationService.StopLoading();
Also consider this method:
Subscribe to the Navigating event.
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
// Don't allow refreshing of a static page
if (DO SOME CHECKS)
{
e.Cancel = true;
}
}
And take a look at this article over at msdn.
http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.navigating.aspx
Related
When salaried button is checked, change label "Hour Pay: " to "Salary" and hide the labels and text boxes below it. When Hourly button is checked, return everything to its initial form.
My main issue is when i execute code it does not hide labels and text boxes.
private void addButton_Click(object sender, EventArgs e)
{
}
private void icontype_CheckedChanged(object sender, EventArgs e)
{
if (salariedRadioButton.Checked == true)
{
hourLabel.Visible = false;
}
else if (hourlyRadioButton.Checked == true)
{
hourLabel.Visible = true;
}
}
private void hourTextBox_TextChanged(object sender, EventArgs e)
{
try
{
weekTextBox.Text =(float.Parse(hourTextBox.Text)40).ToString();
}
catch
{
}
try
{
yearTextBox.Text = (float.Parse(weekTextBox.Text) 52).ToString();
}
catch
{
}
}
I'm not sure what's your expected result, for the hide part, I think you can try this, thanks.
public Form1()
{
InitializeComponent();
this.salariedRadioButton.CheckedChanged += icontype_CheckedChanged;
this.hourlyRadioButton.CheckedChanged += icontype_CheckedChanged;
}
private void icontype_CheckedChanged(object sender, EventArgs e)
{
if (salariedRadioButton.Checked == true)
{
hourLabel.Visible = false;
weekTextBox.Visible = false;
}
else if(hourlyRadioButton.Checked == true)
{
hourLabel.Visible = true;
weekTextBox.Visible = true;
}
}
I ran into an issue where i tried displaying a display box after a countdown reached a certain time but for some odd reason it replicates with each second despite it having already passed the initial time it was supposed to appear. This is what i tried to do but now the timer has stopped and the time remaining column has stopped runng.
public partial class Form1 : Form
{
private List<CSession> sessionlist = new List<CSession>();
private TimeSpan workingTimeSpan = new TimeSpan();
private TimeSpan fiveMinutes = new TimeSpan(0,1,0);
private TimeSpan oneSecond = new TimeSpan(0,0,1);
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void AddTime_Click(object sender, EventArgs e)
{
workingTimeSpan += fiveMinutes;
DisplayWorkingTimeSpan();
}
private void DisplayWorkingTimeSpan()
{
TimerLabel.Text = workingTimeSpan.ToString();
}
private void DecreaseTime_Click(object sender, EventArgs e)
{
TimeSpan fiveMinutes = new TimeSpan(0,5,0);
workingTimeSpan -= fiveMinutes;
DisplayWorkingTimeSpan();
}
private void Confirm_Click(object sender, EventArgs e)
{
CSession newSession = new CSession();
if(PasswordText.Text == "")
{
MessageBox.Show("Password not entered");
return;
}
newSession.password = PasswordText.Text;
newSession.purchased_time = workingTimeSpan;
newSession.remaining_time = workingTimeSpan;
newSession.status = "Online";
sessionlist.Add(newSession);
PasswordText.Text = "";
TimerLabel.Text = "";
workingTimeSpan = new TimeSpan();
}
private void DisplayAllSessions()
{
listView1.Items.Clear();
foreach(CSession c in sessionlist)
{
string[] row = { c.password, c.purchased_time.ToString(), c.remaining_time.ToString(), c.status };
ListViewItem i = new ListViewItem(row);
listView1.Items.Add(i);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach(CSession c in sessionlist)
{
if (c.remaining_time.TotalMinutes == 5)
{
timer1.Stop();
MessageBox.Show("Time almost up for client.");
}
if (c.remaining_time.TotalSeconds < 1)
{
c.status = "Offline";
}
if(c.status == "Online")
{
c.remaining_time -= oneSecond;
}
}
DisplayAllSessions();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
}
Add a flag that gets toggled when you display the message, so you won't display it again:
private bool MessageDisplayed = false;
private void timer1_Tick(object sender, EventArgs e)
{
foreach(CSession c in sessionlist)
{
if (c.remaining_time.TotalMinutes == 5 && !MessageDisplayed) // <-- check the flag
{
MessageDisplayed = true;
MessageBox.Show("Time almost up for client.");
}
if (c.remaining_time.TotalSeconds < 1)
{
c.status = "Offline";
}
if(c.status == "Online")
{
c.remaining_time -= oneSecond;
}
}
DisplayAllSessions();
}
Now you can leave the timer running and your message will only appear once.
I have an object list view that has two text columns. When I edit the left column and hit tab to go to the right I receive a "ArgumentOutOfRangeException" with an index of -1. Looks like the index is something internal to the list view because I debugged my application and found no errors. Here is the code :
public partial class SummaryOverviewSettingsDlg : Form
{
private List<SummaryDataset> _localSummaryDatasets = new List<SummaryDataset>();
private bool _includeLimits;
private SummaryOverviewSettings _summaryOverviewSettings;
public bool IncludeLimits { get { return _includeLimits; } }
public SummaryOverviewSettingsDlg(SummaryOverviewSettings summaryOverviewSettings)
{
InitializeComponent();
if (summaryOverviewSettings.Datasets != null)
{
_localSummaryDatasets.AddRange(summaryOverviewSettings.Datasets);
}
_summaryOverviewSettings = summaryOverviewSettings;
}
private void DataFilesListDlg_Load(object sender, EventArgs e)
{
foreach(var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
LimitsCheckbox.Checked = _summaryOverviewSettings.ShowLimits;
}
private void OlvFilePaths_CellRightClick(object sender, CellRightClickEventArgs e)
{
var contextMenuSymbol = new ContextMenuStrip();
ToolStripItem item;
item = contextMenuSymbol.Items.Add("Add sample");
item.Click += ContextMenuAddFilePath;
if (e.Model != null)
{
contextMenuSymbol.Items.Add("-");
item = contextMenuSymbol.Items.Add("Delete sample");
item.Click += ContextMenuDeleteFilePath;
}
olvFilePaths.ContextMenuStrip = contextMenuSymbol;
}
private void ContextMenuAddFilePath(object sender, EventArgs e)
{
var item = new SummaryDataset()
{
SampleName = "Sample",
Path = "Path"
};
_localSummaryDatasets.Add(item);
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void ContextMenuDeleteFilePath(object sender, EventArgs e)
{
if (olvFilePaths.SelectedObject != null)
{
var item = (SummaryDataset)olvFilePaths.SelectedObject;
olvFilePaths.RemoveObject(item);
_localSummaryDatasets.Remove(item);
}
}
private void OlvFilePaths_CellEditFinished(object sender, CellEditEventArgs e)
{
if (e.Control is TextBox textBox)
{
var oldValue = (string)e.Value;
var newValue = (string)e.NewValue;
var col = e.Column.AspectName;
var index = e.ListViewItem.Index;
if (newValue != oldValue)
{
if (col == "SampleName")
{
_localSummaryDatasets[index].SampleName = newValue;
}
else
{
_localSummaryDatasets[index].Path = newValue;
}
}
}
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void OkButton_Click(object sender, EventArgs e)
{
_summaryOverviewSettings.Datasets.Clear();
_summaryOverviewSettings.Datasets.AddRange(_localSummaryDatasets);
_summaryOverviewSettings.ShowLimits = _includeLimits;
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LimitsCheckbox_CheckedChanged(object sender, EventArgs e)
{
_includeLimits = LimitsCheckbox.Checked;
}
}
When I am making a lot of buttons, is this the best way, or is there a better way? This code feels sort of clunky.
Button button = new Button();
button.MouseEnter += Button_MouseEnter;
button.MouseLeave += Button_MouseLeave;
Button button2 = new Button();
button2.MouseEnter += Button2_MouseEnter;
button2.MouseLeave += Button2_MouseLeave;
void Button_MouseEnter(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("buttonHover");
}
void Button_MouseLeave(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("button");
}
void Button2_MouseEnter(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("button2Hover");
}
void Button2_MouseLeave(object sender, EventArgs e)
{
BackgroundImage = Image.FromFile("button2");
}
I think there isn't better way. I would create a custom control with properties "button" and "buttonHover".
Something like this (not tested yet):
public class MyBytton : Button
{
public Image MainImage { get; set; }
public Image HoverImage { get; set; }
protected override void OnMouseEnter (EventArgs e)
{
if (HoverImage != null)
{
this.BackgroundImage = HoverImage;
}
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
if (MainImage != null)
{
this.BackgroundImage = MainImage;
}
base.OnMouseLeave(e);
}
}
Use:
MyBytton my = new MyBytton();
my.Location = new Point(10, 10); ;
my.Name = "button1";
my.Size = new System.Drawing.Size(141, 61);
my.TabIndex = 0;
my.Text = "test";
my.UseVisualStyleBackColor = true;
my.BackgroundImage = Image.FromFile("img1.jpg");
my.MainImage = Image.FromFile("img1.jpg");
my.HoverImage = Image.FromFile("img2.jpg");
I am working in silverlight and i have created a childwindow in which i have user name and passwrord.
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
this.DialogResult = true;
}
else
{
MessageBox.Show("Incorrect username and/orpassword","Error",MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
And after i have my mainpage which contains the whole project GUI.Like this: (Herei also try to opopp a ChildWindow but it don't do so but when i put a MessageBox.Show("Hello"); then it is popuped)
public partial class MainPage : UserControl
{
public MainPage()
{
loginChildWindow obj = new loginChildWindow();
obj.Show(); //It do not pop up actually
MessageBox.Show("hello"); //whereas thsi messagebox popups
InitializeComponent();
}
}
So to set the login ChildWindow at startup time for my project i think i should do InitializeComponent(); only if my password and userName are matched. so this InitializeComponent(); must be called inside the
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
InitializeComponent();
}
But when i do this it will give error (its obvious that i cannot do that in constructor):
public partial MainPage : UserControl
{
{
public MainPage()
{
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
this.DialogResult = true;
InitializeComponent();
}
else
{
MessageBox.Show("Incorrect username and/orpassword","Error",MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
But how to solve it is my question ?
EDIT AFTER User1 comments:
the two classes are:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Visibility = Visibility.Collapsed;
loginChildWindow log = new loginChildWindow();
log.Show();
}
}
and
public partial class loginChildWindow : ChildWindow
{
public loginChildWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
MainPage obj = new MainPage();
obj.Visibility = Visibility.Visible;
obj.InitializeComponent();
this.DialogResult = true;
}
else
{
MessageBox.Show("Incorrect username and/orpassword", "Error", MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void Login_Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
try:
public MainPage()
{
InitializeComponent();
LoginWindow loginWnd=new LoginWindow();
loginWnd.Closed+= new EventHandler(loginWnd_Closed);
loginWnd.Show();
}
void loginWnd_Closed(object sender, EventArgs e)
{
LoginWindow lw = (LoginWindow)sender;
if (lw.DialogResult == true && lw.nameBox.Text != string.Empty)
{
this.textBlock1.Text = "Hello " + lw.nameBox.Text;
}
else if (lw.DialogResult == false)
{
this.textBlock1.Text = "Login canceled.";
}
}
where LoginWindow is:
public LoginWindow()
{
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void LoginWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.DialogResult == true && (this.nameBox.Text == string.Empty || this.passwordBox.Password == string.Empty))
{
e.Cancel = true;
ChildWindow cw = new ChildWindow();
cw.Content = "Please Enter your name and password or click Cancel.";
cw.Show();
}
}
Please See the link below for more information
how-to-work-with-LoginWindows