When opening one dynamic created button all buttons are opening - c#

I am testing a small app where main window has a text box for entry, user clicks add (List Add in code) and it updates a simple list. In addition to the list update a dynamic button will be created in a wrap panel. The idea is the user then can click what ever entry they need to update and it load the second window with that entry correlated to an index in the list.
I have code that works for the most part but am blanking on what I believe is something trivial. If there is 3 buttons created and I go to click button 3 it will pop up all entries. So three windows with their assigned index. I just need the one window to show for the button that was clicked. I have played around and this particular issue is escaping me.
I have searched for a similar answer but came up short over the past few days.
MainWindow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<string> listA = new List<string>();
private int x = 1;
private int t = 1;
private void addBtn_Click(object sender, RoutedEventArgs e)
{
listA.Add(listBx.Text);
Button btns = new Button();
btns.Width = 70;
btns.Height = 30;
btns.Content = "Button " + x;
btns.Name = "Btn" + t;
btns.Click += (s, t) =>
{
for (int index = 0; index < listA.Count; index++)
{
EditWindow win = new EditWindow();
win.updateBx.Text = listA[index];
win.Show();
}
};
btnPanel.Children.Add(btns);
x++;
t++;
}
}
}
Second window (no code here yet, update btn will rewrite list index it correlates with:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace TestApp
{
/// <summary>
/// Interaction logic for EditWindow.xaml
/// </summary>
public partial class EditWindow : Window
{
public EditWindow()
{
InitializeComponent();
}
private void updateBtn_Click(object sender, RoutedEventArgs e)
{
}
}
}

I am not familiar with c# but the reason is i guess the loop in the click function. It does what you said. Instead of loop you should only use the current listA element.

In your button event handler, you are using a for loop to open an EditWindow for every entry in the list. Instead, only open one EditWindow and populate it with the specified entry.

Related

Why do I have a compiler error (CS1503) in this Ozeki file?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ozeki.Camera;
using Ozeki.Media;
using Ozeki;
using System.Windows.Media;
namespace BasicCameraViewer
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private VideoViewerWPF _videoViewerWPF;
private BitmapSourceProvider _provider;
private IIPCamera _ipCamera;
private WebCamera _webCamera;
private MediaConnector _connector;
public MainWindow()
{
InitializeComponent();
_connector = new MediaConnector();
_provider = new BitmapSourceProvider();
SetVideoViewer();
}
private void SetVideoViewer()
{
_videoViewerWPF = new VideoViewerWPF
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Background= Brushes.Black
};
CameraBox.Children.Add(_videoViewerWPF);
_videoViewerWPF.SetImageProvider(_provider);
}
#region IP Camera Connect/Disconnect
private void ConnectIPCamera_Click(object sender, RoutedEventArgs e)
{
var host = HostTextBox.Text;
var user = userTextBox.Text;
var pass = Password.Password;
_ipCamera = IPCameraFactory.GetCamera(host, user, pass);
if (_ipCamera == null) return;
_connector.Connect(_ipCamera.VideoChannel, _provider);
_ipCamera.Start();
_videoViewerWPF.Start();
}
private void DiconnectIPCamera_Click(object sender, RoutedEventArgs e)
{
_videoViewerWPF.Stop();
_ipCamera.Disconnect();
_ipCamera.Dispose();
_connector.Disconnect(_ipCamera.VideoChannel, _provider);
}
#endregion
}
}
Can somebody tell me what I could do? It tells me that i can not convert the _videoViewerWPF.SetImageProvider(_provider) line from Ozeki.Media.BitmapSourceProvider to Ozeki.Media.IImageProvider and i have absolutely no idea what to do that it could work.
I tried several times to do something, but I dont even know what I´m doing.
I would be thankfull if somebody could help me so I can finish this.
I came across the same issue after following the Ozeki video C# camera tutorial #3 - Camera viewer on YouTube.
It may be that the API has moved on since that video because sample code on the Ozeki Website (How to connect to an RTSP camera and display the picture in C#) uses a DrawingImageProvider instead of a BitmapSourceProvider:
...
private DrawingImageProvider _provider = new DrawingImageProvider();
...
...and then (for a WPF app):
_videoViewerWpf.SetImageProvider(_provider);
In my case, this fixed the compiler error and I can now display RTSP video in a WPF app.

Closing all Windows in a C# WPF application

I'm creating a little WPF app in VS2013Express and I've come across a little problem.
You see, there are three windows, MainWindow, LatAndLongDialog, TimeCitiesDialog.
`LatAndLongDialog` and `TimeCitiesDialog` are opened from MainWindow (with the click of a button).
I want all the other windows to close when the `Closed()` event is called on `MainWindow`.
The code on MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace GlobalTime_ELITE_for_WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
UserDescText.Content =
"Select a TimeCity or enter the latitude and longitude in \n" +
"to view the World Time there. Or, select another one of the\n" +
"options below to do that. Go to Help by clicking on the link\n" +
"on the upper-right corner of the window to view everything you\n" +
"can do.";
this.Closed += CloseOff;
}
private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
{
TimeCitiesDialog ObjectReference = new TimeCitiesDialog();
ObjectReference.Show();
}
private void OpenLatAndLongDialog(Object Sender, EventArgs E)
{
LatAndLongDialog ObjectReference = new LatAndLongDialog();
ObjectReference.Show();
}
private void CloseOff(Object Sender, EventArgs E)
{
this.Close();
TimeCitiesDialog tcdor = new TimeCitiesDialog();
LatAndLongDialog laldor = new LatAndLongDialog();
}
}
}
How can I close them all? Please help!
The proper way to shutdown a WPF app is to use Application.Current.Shutdown() . This will close all open Windows, raise some events so that cleanup code can be run, and it can't be canceled.
Environment.Exit() terminates the application immediately even if other threads are executing.
You should also consider setting the Owner on non-main Windows. The behavior will likely be more like what you would expect in regards to Z-order, minimizing, and maximizing. As an added bonus, the owned windows will automatically close when the owner Window closes.
private void CloseAllWindows()
{
for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
{
App.Current.Windows[intCounter].Close();
}
}
Close all opened current windows.
Use this instead this.Close()
Environment.Exit(0);
this will force everything to close
If you keep track of the Dialogs outside of the scope of the methods you use to open them, you can call which ever methods on those Dialogs you wish from anywhere within the Class. Here I have them as Class variables and they are instantiated there but not shown until you press the buttons. You can also create "Close" buttons for those specific windows and call their .Close() methods when ever you wish. That will allow you to open and close them at will. You can also call their .Close() methods when the main form closes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace GlobalTime_ELITE_for_WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
TimeCitiesDialog tcDialog = new TimeCitiesDialog();
LatAndLongDialog lalDialog = new LatAndLongDialog();
public MainWindow()
{
InitializeComponent();
UserDescText.Content = "Select a TimeCity or enter the latitude and longitude in \n" +
"to view the World Time there. Or, select another one of the\n" +
"options below to do that. Go to Help by clicking on the link\n" +
"on the upper-right corner of the window to view everything you\n" +
"can do.";
this.Closed += CloseOff;
}
private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
{
tcDialog.Show();
}
private void OpenLatAndLongDialog(Object Sender, EventArgs E)
{
lalDialog.Show();
}
private void CloseOff(Object Sender, EventArgs E)
{
// Close the dialogs first, then allow this method
// to end which will finish the this.Close() process.
tcDialog.Close();
lalDialog.Close();
}
}
}

ProgressBar not starting, not going in foreach loop until window not closed

I work on an export plugin to export revit models to database.
To do this, my main window is calling ExportEngine which is a Window:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using UserConnect;
using System.Collections.ObjectModel;
using System.IO;
namespace UserConnect
{
public partial class ExportEngine : Window
{
private List<Component> components = new List<Component>();
private static String zoneName = "REVIT";
private User user;
private Server neo4j = new Server("172.16.1.104", 8000);
public ExportEngine(User userp)
{
user = userp;
InitializeComponent();
}
public void addComponent(Component c)
{
components.Add(c);
}
public bool save()
{
this.ShowDialog();
float progress = components.Count / 100;
foreach (Component c in components)
{
//Here is my export request, working good
pbStatus.Value+=progress;
}
this.Close();
return result;
}
}
}
I call this in Main :
db = new ExportEngine(user);
foreach(String name in mats)
{
Component c = new Component(name, "m2",1);
db.addComponent(c);
}
db.save();
I don't understand why my save() starts only after I close the progressbar Window, and this progressbar doesn't move at all (no progress).
save doesnt run until windows is closed
ShowDialog does not return until the window you are showing is closed. That explains why it does not run until the windows closes.
Progress bar doesn't update
Try this.
pbStatus.MaxValue = components.Count; //make sure you set the max value
foreach (Component c in components)
{
pbStatus.Value+= 1; //increment by one item as you have processed on item.
}

Need to populate next page's listbox by executing the code on previous page

I am working on a Windows Phone 8.1 Music Player app in C#/XAML.
When the user hits the "Next" button on the intro page and goes to the second page, he will see a listview. This listview is supposed to be populated with all the songs on the phone. But that seems to take at least 3-5 seconds and the user ends up having to look at a blank white screen until all the songs are loaded into a list and then the list is put into the listview.
My approach was to begin the population on the first page instead, so by the time the users comes arrives at the second page, either listview has already been populated or it's almost done populating.
If I stay on the first page for 3-5 seconds and then go to the next page, I can see the listview populated with all the songs fine. But instead, if as soon as the app opens, I immediately hit the button and go to the next page, then I only see the listview half-way populated. While the list eventually is able to gather all the songs, it only populates the listview to the point where it was when it reading all the songs.
MainPage.xaml.cs (First page):
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace Beta
{
public sealed partial class MainPage : Page
{
List<Music> source = new List<Music>();
public MainPage()
{
this.InitializeComponent();
this.setAllItems();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Player));
}
private async Task getMusic(IStorageFolder folder)
{
var folders = await folder.GetFoldersAsync();
if (folders != null)
foreach (var fol in folders)
await getMusic(fol);
var files = await folder.GetFilesAsync();
foreach (var file in files)
{
MusicProperties musicProperties = await file.Properties.GetMusicPropertiesAsync();
this.source.Add(new Music(musicProperties.Artist, musicProperties.Title, musicProperties.Album));
Player.source = this.source;
}
}
private async void setAllItems()
{
await this.getMusic(Windows.Storage.KnownFolders.MusicLibrary);
}
}
}
Player.xaml.cs (Second page):
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Beta;
using Windows.Storage;
using System.Threading.Tasks;
using Windows.Storage.Search;
using Windows.Storage.FileProperties;
using System.Diagnostics;
namespace Beta
{
public sealed partial class Player : Page
{
public static List<Music> source;
public Player()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.contentList.ItemsSource = source;
contentList.UpdateLayout();
}
}
}
When I arrive at the player (second) page, I waited for a minute and then hit a button (not shown) which would use Debug.WriteLine() to print out all contents of the list. It printed out the complete list.
So the async task is doing its job and completing the list, but it's not able to complete the listview properly.
I have 2 questions:
How do I get the listview to self-populate with more entries as list gets more items (perhaps in real time, where the remaining items from the list are continuously added to the listview until the list is completed?
Is there a faster way to get all the music files so I don't have to rely on this work-around?

how to call xaml file on button onclick event in windows phone 8

i have one html page which contains three button.in one button after clicking it should goto my contact list ,pick that contact and come back to that index.html page.i tried my code for contact chooser as sepratly and it runs correctly but i m not getting how to call the same contact chooser from button onclick funtion.please help if anybody knows thanks.this is my code below for contact chooser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace CordovaWP8_2_8_03
{
public partial class MainPage : PhoneApplicationPage
{
PhoneNumberChooserTask phoneNumberChooserTask;
// Constructor
public MainPage()
{
InitializeComponent();
phoneNumberChooserTask = new PhoneNumberChooserTask();
phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
phoneNumberChooserTask.Show();
}
void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show("The phone number for " + e.DisplayName + " is " + e.PhoneNumber);
// PhoneCallTask phoneCallTask = new PhoneCallTask();
// phoneCallTask.DisplayName = e.DisplayName;
// phoneCallTask.PhoneNumber = e.PhoneNumber;
// phoneCallTask.Show();
}
}
}

Categories

Resources