I have a problem, i have 5 buttons that load txt files from system and show it as string on textblocks but i dont know how to do it without 5 event handlers
private void OnClick1(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
numbers1.Text = File.ReadAllText(openFileDialog.FileName);
}
OnClick1 is button1, numbers1 is a textblock1
now i have 5 codes like this (with numbers2.Text, numbers3.Text etc) how can i do it shorter
I may be misunderstanding what you are asking. However, if you want the different buttons to use the same click event. Then the click event is going to have to be able to distinguish “which” button was clicked in order to know which text box to use.
In this case, I recommend you give each button a name and then in the click event, cast the sender as a Button, then check its name to determine which text box to use. All buttons are wired up to this ONE (1) event. Example something like…
private void btn_Click(object sender, RoutedEventArgs e) {
Button btnSender = (Button)sender;
TextBox tb = null;
switch (btnSender.Name) {
case "btn1":
tb = txt1;
break;
case "btn2":
tb = txt2;
break;
case "btn3":
tb = txt3;
break;
case "btn4":
tb = txt4;
break;
}
if (tb != null) {
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
tb.Text = File.ReadAllText(openFileDialog.FileName);
}
}
This should get you started I think
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
List<TextBlock> textBlocks = new List<TextBlock>();
textBlocks.Add(txt1);
textBlocks.Add(txt2);
textBlocks.Add(txt3);
textBlocks.Add(txt4);
textBlocks.Add(txt5);
int count = 0;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (String files in openFileDialog.FileNames)
{
var currentText = textBlocks[count];
currentText.Text = File.ReadAllText(files);
count++;
}
}
}
Here is the xaml code
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="5">
<Button Name="btnLoad" Content="Load All" Click="btnLoad_Click" ></Button>
<TextBlock x:Name="txt1" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
<TextBlock x:Name="txt2" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
<TextBlock x:Name="txt3" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
<TextBlock x:Name="txt4" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
<TextBlock x:Name="txt5" HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="118"/>
</StackPanel>
Related
i want to get in my codebehind the Content of an button that has a grid in it with multiple textboxes.
i had this before Code and this works:
XAML:
<Button Click="btnClick_upload_Data">
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="test1" ></TextBlock>
<TextBlock Text="test2" ></TextBlock>
</StackPanel>
</Button.Content>
</Button>
codebehind:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
string s = ((((sender as Button).Content) as StackPanel).Children[1] as TextBlock).Text;
//…
and this way i got the "test2" im my string variable.
now my XAML has changed a bit
my Question is how do i have to Change my function so i still get "test2" in my string variable 's'
new XAML:
<Button Click="btnClick_upload_Data" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="test1" Grid.Row="0"></TextBlock>
<TextBlock Text="test2" Grid.Row="1"></TextBlock>
</Grid>
</StackPanel>
</Button.Content>
</Button>
new XAML:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
//????
thanks in advance
Try this:
private void btnClick_upload_Data(object sender, RoutedEventArgs e)
{
string s = null;
Button btn = (Button)sender;
StackPanel sp = btn.Content as StackPanel;
if (sp != null && sp.Children.Count > 0)
{
Grid grid = sp.Children[0] as Grid;
if (grid != null && grid.Children.Count > 1)
{
TextBlock textBlock = grid.Children[1] as TextBlock;
if (textBlock != null)
s = textBlock.Text;
}
}
MessageBox.Show(s);
}
In My project i have a Text Block
<TextBlock Name="MyDisplay" TextAlignment="Center"
Style="{StaticResource HeaderTextBlockStyle}"/>
And Set Of Buttons in a canvas
<Canvas Name="ButtonPanel">
<Button Style="{StaticResource MyButtonStyle}" Content="1" Canvas.Left="0" Canvas.Top="0"/>
<Button Style="{StaticResource MyButtonStyle}" Content="2" Canvas.Left="100" Canvas.Top="0"/>
<Button Style="{StaticResource MyButtonStyle}" Content="3" Canvas.Left="200" Canvas.Top="0"/>
<Button Style="{StaticResource MyButtonStyle}" Content="4" Canvas.Top="86" Canvas.Left="0"/>
<Button Style="{StaticResource MyButtonStyle}" Content="5" Canvas.Top="86" Canvas.Left="100"/> </Canvas>
I want to write button content in a text block
My code is
public DisplayPad()
{
this.InitializeComponent();
ButtonPanel.AddHandler(PointerPressedEvent, new PointerEventHandler(ScreenMarkup_PointerPressed), true);
UpdateDisplay();
}
public void ScreenMarkup_PointerPressed(object sender, RoutedEventArgs e)
{
Button button = e.OriginalSource as Button;
if (button == null)
return;
string content = button.Content.ToString();
double digit;
if (double.TryParse(content, out digit))
{
if(content == "1")
{
//codes
MyDisplay.Text = "1";
}
}
UpdateDisplay();
}
void UpdateDisplay()
{
try
{
MyDisplay.Foreground = Application.Current.Resources["ApplicationForegroundThemeBrush"] as Brush;
// Update the display
MyDisplay.Text = String.Format("{0:##.##}");
}
catch
{
//Exception
}
}
But this code is not working it is not update number in TextBlock.
But same thing is used in windows phone 8 which was working fine but only difference in code is (Windows phone 8 code)
public DisplayPad()
{
this.InitializeComponent();
ButtonPanel.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(ScreenMarkupButton_MouseLeftButtonUp), true);
UpdateDisplay();
}
public void ScreenMarkupButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Button button = e.OriginalSource as Button;
if (button == null)
return;
string content = button.Content.ToString();
double digit;
if (double.TryParse(content, out digit))
{
//My Codes
}
UpdateDisplay();
}
The OriginalSource object is not a Button but rather the inner TextBlock within the Button, so you need to change your code to
var textBlock = e.OriginalSource as TextBlock;
if (textBlock == null)
return;
string content = textBlock.Text;
I'm trying to make it so when the user clicks on a spot in my canvas, a popup is displayed that allows the user to enter two separate pieces of data. So I need two textblocks and then a way to save the data entered into some variables in the code behind. I've been looking at different tutorials and it's easy making a window with input texblocks. Just not sure how to do it with popups. The addNode_MouseDown method is where I tried adding the popup, since the information entered is going to be about the circle that the user makes when they click on the canvas. Any help would be appreciated.
Here's my code at the moment:
XAML:
<Window x:Class="CanvasStuff.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="410" Width="869">
<Grid Height="387">
<Label Content="Image" Height="32" HorizontalAlignment="Left" Margin="11,10,0,0"
Name="selectedFileName" VerticalAlignment="Top" Width="137"
Background="LightGray" BorderBrush="Gray" BorderThickness="1"/>
<Button Content="Browse File" Height="34" HorizontalAlignment="Left" Margin="154,6,0,0"
Name="BrowseButton" VerticalAlignment="Top" Width="119"
Foreground="Maroon" FontSize="16" FontFamily="Georgia" Click="BrowseButton_Click" />
<Button Content="Input Range and Heading" Height="34" HorizontalAlignment="Left" Margin="279,6,0,0"
Name="InputRangeBearing" VerticalAlignment="Top" Width="191"
Foreground="Maroon" FontSize="16" FontFamily="Georgia" Click="InputButton_Click" />
<Canvas Margin="0,45,2,8" x:Name="canvas1" MouseDown= "addNode_MouseDown">
</Canvas>
</Grid>
</Window>
Code behind:
namespace CanvasStuff
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = "c:\\";
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string selectedFileName = dlg.FileName;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(selectedFileName));
canvas1.Background = brush;
BitmapImage bitmap = new BitmapImage();
}
}
private void InputButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Please click on known object to enter range and heading of that object.");
}
private void addNode_MouseDown(object sender, MouseButtonEventArgs e)
{
Point currentPoint = new Point();
if (e.ButtonState == MouseButtonState.Pressed)
currentPoint = e.GetPosition(this);
Ellipse ellipse = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
ellipse.Fill = mySolidColorBrush;
ellipse.Width = 10;
ellipse.Height = 10;
Canvas.SetLeft(ellipse, e.GetPosition(canvas1).X);
Canvas.SetTop(ellipse, e.GetPosition(canvas1).Y);
canvas1.Children.Add(ellipse);
}
}
}
Did you try using the PopUp window?
XAML:
<Popup Name="errMsg" StaysOpen="False">
<TextBox/>
</Popup>
in your code behind:
errMsg.IsOpen = true;
Or you could either create fully using your code:
WPF popup window
Reference:
Popup a User Control
I try to clear (and add) items to ListPicker, but when the app have to clear all of the items is an error - "SelectedItem must always be set to a valid value". My code:
<toolkit:ListPicker x:Name="select" HorizontalAlignment="Right" Margin="0,135,30,0" VerticalAlignment="Top" Width="195" Height="64" d:LayoutOverrides="HorizontalAlignment" BorderBrush="{x:Null}" FontFamily="Arial" FontWeight="Bold" FontSize="32" Style="{StaticResource ListPickerStyle1}" BorderThickness="0" DataContext="{Binding}">
<toolkit:ListPicker.Background>
<ImageBrush Stretch="Fill" ImageSource="list_picker.png"/>
</toolkit:ListPicker.Background>
and action for a button
private void button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
select.Items.Clear(); //here is an error
for (int i = 0; i < arrays.Length; i++)
{
select.Items.Add(arrays[i]);
}
}
I try another options, but it doesn't work too.
private void button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
select.SelectedItem = null; // here is an error
select.Items.Clear();
for (int i = 0; i < arrays.Length; i++)
{
select.Items.Add(arrays[i]);
}
}
To clear the selection you should use:
select.SelectedItems.Clear();
Hello I am in need of help to switch the content of two buttons
what I have done so far is to check if the buttons are neighbours.
private int row = 4;
private int col = 4;
public MainWindow()
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button cmd = (Button)sender;
MessageBox.Show(cmd.Tag.ToString());
string txt = cmd.Tag.ToString();
int r = int.Parse("" + txt[0]);
int c = int.Parse("" + txt[1]);
if (Math.Abs(r - row) + Math.Abs(c - col) == 1)
{
MessageBox.Show(r + " " + c);
}
And my buttons in my XAML file is like this
<Button Tag="00" Grid.Row="0" Grid.Column="0" Click="Button_Click">A</Button>
<Button Tag="01" Grid.Row="0" Grid.Column="1" Click="Button_Click">B</Button>
and the Challenge is to switch the content (A and B)
Can anyone help me with that?
do not use WinForms event handling like Button_Click method.
use binding for button content
look at the commands
look at the MVVM approach
Just use a temporary variable to hold one of the strings.
string tmp = Button1.Text;
Button1.Text = Button2.Text;
Button2.Text = tmp;
I'd skip the generic eventhandler name. You need to store the button you want to move to in a temporary variable.
I used nine Buttons:
<Button Tag="00" Grid.Row="0" Grid.Column="0" Margin="15,21,13,23" Name="btn1" Click="btn1_Click">A</Button>
<Button Tag="01" Grid.Row="0" Grid.Column="1" Margin="15,21,13,23" Name="btn2" Click="btn1_Click">B</Button>
<Button Tag="02" Grid.Row="0" Grid.Column="2" Margin="15,21,13,23" Name="btn3" Click="btn1_Click">C</Button>
<Button Tag="10" Grid.Row="1" Grid.Column="0" Margin="15,21,13,23" Name="btn4" Click="btn1_Click">D</Button>
<Button Tag="11" Grid.Row="1" Grid.Column="1" Margin="15,21,13,23" Name="btn5" Click="btn1_Click">E</Button>
<Button Tag="12" Grid.Row="1" Grid.Column="2" Margin="15,21,13,23" Name="btn6" Click="btn1_Click">F</Button>
<Button Tag="20" Grid.Row="2" Grid.Column="0" Margin="15,21,13,23" Name="btn7" Click="btn1_Click">G</Button>
<Button Tag="21" Grid.Row="2" Grid.Column="1" Margin="15,21,13,23" Name="btn8" Click="btn1_Click">H</Button>
<Button Tag="22" Grid.Row="2" Grid.Column="2" Margin="15,21,13,23" Name="btn9" Click="btn1_Click">I</Button>
and This Method to get the Button from the Grid and Change Values:
private int row = 2;
private int col = 2;
private void btn1_Click(object sender, RoutedEventArgs args)
{
Button cmd = (Button)sender;
string txt = cmd.Tag.ToString();
int r = int.Parse("" + txt[0]);
int c = int.Parse("" + txt[1]);
if (Math.Abs(r - row) + Math.Abs(c - col) == 1)
{
MessageBox.Show(r + " " + c);
Button nearButton = grd1.Children.Cast<Button>().First(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == col);
Object tmp = nearButton.Content;
nearButton.Content = cmd.Content;
cmd.Content = tmp;
}
}
In my Example the two Buttons next to the specified change their value with it, hope that is what you intended.
(Getting an Item from a Grid via the X and Y is stolen from here)