how to clear an image on button click - wpf

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;
}

Related

WPF Richtextbox Write to .txt file

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);
}
}

background operation of user controller

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.

changing datagridview selection color dynamically c#

the problem is that when i change the color of selected row by RowTemplate.DefaultCellStyle.SelectionBackColor in my form constructor it works but it does not work in button events when user clicks some button for changing grid selected back color!
any help please!
public Form1()
{
InitializeComponent();
dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor=Color.Red; //this works fine
}
void button2_Click(object sender, EventArgs e)
{
dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor=Color.Blue;//but this does not work
}
Try this ..
void dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)
{
If (DatagridView1.Rows(DataGridView1.CurrentCell.RowIndex).Selected )
{
DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).DefaultCellStyle.SelectionBackColor=Color.Blue;
}
}
void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.DefaultCellStyle.SelectionBackColor = Color.Blue
}
}
Update :
void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.DefaultCellStyle.BackColor = Color.Blue
}
}

How do I display a tooltip when focus is in a specific textbox?

For a textbox, I want to display a tooltip immediatly when the focus is in on the textbox, and stay there for the duration of the focus - not just when the mouse hovers over the textbox.
How can I do that?
The Enter and Leave events are probably useful here, and show it with a duration of 0 to keep it there.
private ToolTip tt;
private void textBox1_Enter(object sender, EventArgs e) {
tt = new ToolTip();
tt.InitialDelay = 0;
tt.IsBalloon = true;
tt.Show(string.Empty, textBox1);
tt.Show("I need help", textBox1, 0);
}
private void textBox1_Leave(object sender, EventArgs e) {
tt.Dispose();
}
Note: Calling the Show(...) method twice like in my example will force the "pointer" to point correctly to the control.
have tested, the event names:
private void textbox_Enter(object sender, EventArgs e)
{
toolTip1.Show("your tip here", textbox);
}
private void textbox_Leave(object sender, EventArgs e)
{
toolTip1.Hide(textbox);
}
tooltip is a control, needs to be added from toolbox.
using mouse hover and mouse leave events
private void textBox1_MouseHover(object sender, EventArgs e)
{
toolTip1.Show("your tip here", textBox2);
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
toolTip1.Hide(textBox2);
}
>
Windows Forms
public partial class FormWindow : Form
{
//Constructor
public FormWindow()
{
txtUrl.Text = "Enter text here";
txtUrl.ForeColor = Color.Gray;
txtUrl.GotFocus += TxtUrl_GotFocus;
txtUrl.LostFocus += TxtUrl_LostFocus;
}
private void TxtUrl_GotFocus(object sender, EventArgs e)
{
txtUrl.Text = "";
txtUrl.ForeColor = Color.Black;
}
private void TxtUrl_LostFocus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtUrl.Text))
{
txtUrl.Text = "Enter text here";
txtUrl.ForeColor = Color.Gray;
}
}
}
Use a System.Windows.Forms.ToolTip and show it in textbox GotFocus event and Hide it in LostFocus event:
void textBox_GotFocus(object sender, EventArgs e)
{
toolTip.Show("your tip", textBox);
}
void textBox_LostFocus(object sender, EventArgs e)
{
toolTip.Hide(textBox);
}

wpf prevent second click button

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.

Resources