string okunanmetin;
string Progfilepath;
private void btnSelect_Click(object sender, RoutedEventArgs e)
{
richtextboxEdit.Document.Blocks.Remove(richtextboxEdit.Document.Blocks.ElementAt(0));
Progfilepath = "C:/Users/SC/Desktop/test.txt";
okunanmetin = File.ReadAllText(Progfilepath);
richtextboxEdit.Document.Blocks.Add(new Paragraph(new Run(okunanmetin)));
}
As you can see, I can insert a .txt file into the richtextbox. I am making changes in this text that I added. After this change I want to save to the same file.
I tried to do as seen below.
private void btnSave_Click(object sender, RoutedEventArgs e)
{
LoadXamlPackage( "C:/Users/SC/Desktop/test.txt");
}
void LoadXamlPackage(string _fileName)
{
TextRange range;
FileStream fStream;
if (File.Exists(_fileName))
{
range = new TextRange(richtextboxEdit.Document.ContentStart, richtextboxEdit.Document.ContentEnd);
fStream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
range.Save(fStream, DataFormats.Text);
fStream.Close();
}
}
It didn't work the way I wanted.
When I click the save button, it saves the changes but not everything.
Thanks for Support
Hello Guys I solved the problems like the below.
private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (File.Exists(Progfilepath))
{
TextRange textRange = new TextRange(richtextboxEdit.Document.ContentStart, richtextboxEdit.Document.ContentEnd);
File.WriteAllText(Progfilepath, textRange.Text);
}
}
Related
I have prepared more than one UserControl for a windows program in XAML. Each user control works as a separate page. But I do page transitions in navigate class. In ".xaml.cs" when calling User control
Navigate.navigate (navigate_grid, new DeviceLayout ());
I'm using the line of code. But every time I create a new user control, the background functions don't work. How do I flip one instead of invoking a new user control each time?
class Navigate
{
public static void navigate(Grid grd, UserControl uc)
{
if (grd.Children.Count > 0)
{
grd.Children.Clear();
grd.Children.Add(uc);
}
else { grd.Children.Add(uc); }
}
}
Example navigate:
public SettingsView()
{
InitializeComponent();
Navigate.navigate(navigate_grid, new SystemLayout());
}
private void system_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new SystemLayout());
previous_page.Text = "";
current_page.Text = "SİSTEM";
next_page.Text = "UYGULAMA";
}
private void application_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ApplicationLayout());
previous_page.Text = "SİSTEM";
current_page.Text = "UYGULAMA";
next_page.Text = "BAĞLANTI";
}
private void connection_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ConnectionLayout());
previous_page.Text = "UYGULAMA";
current_page.Text = "BAĞLANTI";
next_page.Text = "ÜRÜNLER";
}
private void product_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ProductsLayout());
previous_page.Text = "BAĞLANTI";
current_page.Text = "ÜRÜNLER";
next_page.Text = "CİHAZLAR";
}
private void device_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new DeviceLayout());
previous_page.Text = "ÜRÜNLER";
current_page.Text = "CİHAZLAR";
next_page.Text = "YAZICILAR";
}
private void printer_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new PrinterLayout ());
previous_page.Text = "CİHAZLAR";
current_page.Text = "YAZICILAR";
next_page.Text = "KULLANICILAR";
}
private void users_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new UsersLayout());
previous_page.Text = "YAZICILAR";
current_page.Text = "KULLANICILAR";
next_page.Text = "BAKIM";
}
private void maintenance_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new MaintenanceLayout());
previous_page.Text = "KULLANICILAR";
current_page.Text = "BAKIM";
next_page.Text = "HAKKINDA";
}
private void info_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new InfoLayout());
previous_page.Text = "BAKIM";
current_page.Text = "HAKKINDA";
next_page.Text = "";
}
}
Not sure if I get what you mean.
When you change the UserControl with the use of any of those functions eg info_button_click you can't access this funtion anymore.
That would be the case, because your XAML and .cs file are one class, containing those funcitons. If you change the UserControl (XAML) you will also change the .cs file. Therefore you can't access those functions anymore.
You could probably get the behaviour you want if you bind the commands to a viewmodel, which you could then pass through the navigation as well?
Sry, I'm still not sure what exactly it is you're doing.
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;
}
private void txtLastName_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!char.IsDigit((char)e.Key)) e.Handled = true;
}
But It not support all key in keyboard .
private void txtLastName_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^[a-zA-Z]"))
{
e.Handled = true;
}
}
You must use IsLetter.
private void txtLastName_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Char.IsLetter((char)e.Key)) e.Handled = true;
}
This thread is really old, but if someone still needs it, here is code that is working for me
(Edited a little bit Usman's code)
private void TextValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^a-zA-Z]+");
e.Handled = regex.IsMatch(e.Text);
}
And don't forget to put code below to TextBox that you want to accept only text ( in xaml )
PreviewTextInput="TextValidationTextBox"
The following code will only show a size for my bitmap (needed for processing) if I uncomment the commented lines. This doesn't seem the right to go about things but it's all I've come up with so far that works. I don't want to display my bitmap as an image in a UI element, I just want to process it.
BitmapImage bmpi;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
bmpi = new BitmapImage(new Uri("multicolor.png", UriKind.Relative));
//Image img = new Image();
//img.Source = bmpi;
//LayoutRoot.Children.Add(img);
//LayoutRoot.Children.Clear();
MessageBox.Show(bmpi.PixelWidth.ToString());
}
To load the image upfront, you need to set CreateOptions to None from its default value, DelayCreation.
Then you can get the width in the ImageOpened event.
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
bmpi = new BitmapImage();
bmpi.CreateOptions = BitmapCreateOptions.None;
bmpi.ImageOpened += new EventHandler<RoutedEventArgs>(bmpi_ImageOpened);
bmpi.UriSource = new Uri("multicolor.png", UriKind.RelativeOrAbsolute);
}
void bmpi_ImageOpened(object sender, RoutedEventArgs e)
{
MessageBox.Show(bmpi.PixelWidth.ToString());
}
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();
}
}
}