I tried to modify TextBox context menu/MenuFlyout using this code but it doesn't work (the additional menu Items don't appear and myFlyout always null) (UWP/C#)
private void Menu_Opening(object sender, object e)
{
MenuFlyout myFlyout = sender as MenuFlyout;
if (myFlyout != null && myFlyout.Target == TextBox)
{
MenuFlyoutSubItem searchWith = new MenuFlyoutSubItem();
searchWith.Icon = new SymbolIcon(Symbol.Find);
searchWith.Text = "Search With";
MenuFlyoutItem googles = new MenuFlyoutItem();
googles.Text = "Google";
googles.Click += Googles_Click;
searchWith.Items.Add(googles);
MenuFlyoutItem bings = new MenuFlyoutItem();
bings.Text = "Bing";
bings.Click += Bings_Click;
searchWith.Items.Add(bings);
myFlyout.Items.Add(searchWith);
}
}
private async void Googles_Click(object sender, RoutedEventArgs e)
{
if (TextBox.SelectedText != null)
{
var uri= new Uri(#"https://google.com/search?q=" + TextBox.SelectedText);
var success = await Launcher.LaunchUriAsync(uri);
}
}
private async void Bings_Click(object sender, RoutedEventArgs e)
{
if (TextBox.SelectedText != null)
{
var uri = new Uri(#"https://bing.com/search?q=" + TextBox.SelectedText);
var success = await Launcher.LaunchUriAsync(uri);
}
}
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
TextBox.SelectionFlyout.Opening += Menu_Opening;
TextBox.ContextFlyout.Opening += Menu_Opening;
}
private void TextBox_Unloaded(object sender, RoutedEventArgs e)
{
TextBox.SelectionFlyout.Opening -= Menu_Opening;
TextBox.ContextFlyout.Opening -= Menu_Opening;
}
<TextBox x:Name="TextBox" Loaded="TextBox_Loaded" Unloaded="TextBox_Unloaded"/>
The problem is that you have not give MenuFlyout instance to SelectionFlyout or ContextFlyout. Please refer the following code to add MenuFlyout.
<TextBox x:Name="TextBox" Loaded="TextBox_Loaded" Unloaded="TextBox_Unloaded">
<TextBox.ContextFlyout>
<MenuFlyout>
</MenuFlyout>
</TextBox.ContextFlyout>
</TextBox>
Update
The default type of SelectionFlyout is TextCommandBarFlyout, and it could not convert to MenuFlyout, if you don't want to replace the default one. you could add TextCommandBarFlyout like the following,
private void Menu_Opening(object sender, object e)
{
TextCommandBarFlyout myFlyout = sender as TextCommandBarFlyout;
if (myFlyout != null && myFlyout.Target == TextBox)
{
AppBarButton searchCommandBar = new AppBarButton() { Icon = new SymbolIcon(Symbol.Find), Label = "Search With" };
searchCommandBar.Click += SearchCommandBar_Click;
myFlyout.PrimaryCommands.Add(searchCommandBar);
}
}
private void SearchCommandBar_Click(object sender, RoutedEventArgs e)
{
}
Related
I am trying to transfer the data entered in the TextBoxes to gridcontrol. There is no problem when I write to the text manually, but I have a button on the grid, but there is a button and the path of the selected file is transferred to the text. Why would that be? How can I solve it?
I'd appreciate it if you helped.
Thank you.
public frmYazdir(TblBilgi tbl)
{
InitializeComponent();
db = new DbEntities1();
if (tbl != null)
{
tblBilgiBindingSource.DataSource = tbl;
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog DosyaYukle = new OpenFileDialog();
DosyaYukle.Filter = "REPX Dosyaları(*repx.*) | *.repx*";
if (DosyaYukle.ShowDialog() == DialogResult.OK)
{
filename = DosyaYukle.FileName;
textBox1.Text = filename;
if (tbl != null)
{
db.TblBilgi.Attach(tblBilgiBindingSource.DataSource as TblBilgi);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
db.SaveChanges();
MessageBox.Show("Kaydedildi.");
}
public frmYazdir(TblBilgi tbl)
{
InitializeComponent();
db = new DbEntities1();
if (tbl != null)
{
tblBilgiBindingSource.DataSource = tbl;
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog DosyaYukle = new OpenFileDialog();
DosyaYukle.Filter = "REPX Dosyaları(*repx.*) | *.repx*";
if (DosyaYukle.ShowDialog() == DialogResult.OK)
{
filename = DosyaYukle.FileName;
textBox1.Text = filename;
if (tbl != null)
{
db.TblBilgi.Attach(tblBilgiBindingSource.DataSource as TblBilgi);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
db.SaveChanges();
MessageBox.Show("Kaydedildi.");
}
I have this class,
public class ImageBox : Grid
{
Image imgclose; public String path;
List<ImageBox> ImageBoxes;
public ImageBox(string label,List<ImageBox> ImBox)
{
this.ImageBoxes = ImBox;
imgclose = new Image();
imgclose.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("pack://application:,,,/Close.ico"));
imgclose.Width = 20; imgclose.Height = 20; imgclose.Cursor = Cursors.Hand; imgclose.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; imgclose.VerticalAlignment = System.Windows.VerticalAlignment.Top;
imgclose.Visibility = System.Windows.Visibility.Hidden;
imgclose.MouseLeftButtonDown += new MouseButtonEventHandler(imgclose_MouseLeftButtonDown);
this.MouseEnter += new MouseEventHandler(Blank_MouseEnter);
this.MouseLeave += new MouseEventHandler(Blank_MouseLeave);
this.Height = 100; this.Width = 100;
try
{
System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
path = open.FileName.Replace(Directory.GetCurrentDirectory(), "");
this.Background = new System.Windows.Media.ImageBrush(new System.Windows.Media.Imaging.BitmapImage(new Uri(label)));
path = label;
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
Grid.SetColumn(imgclose, 0); Grid.SetRow(imgclose, 1);
this.Children.Add(imgclose);
ContextMenu contextMenu1 = new ContextMenu();
MenuItem conitem1 = new MenuItem() { Header = "Send to back" }; conitem1.Click += new System.Windows.RoutedEventHandler(conitem1_Click);
MenuItem conitem2 = new MenuItem() { Header = "Bring to Front" }; conitem2.Click += new System.Windows.RoutedEventHandler(conitem2_Click);
contextMenu1.Items.Add(conitem1); contextMenu1.Items.Add(conitem2);
this.ContextMenu = contextMenu1;
}
void conitem1_Click(object sender, EventArgs e)
{
Canvas.SetZIndex(this, (Canvas.GetZIndex(this) - 1));
}
void conitem2_Click(object sender, EventArgs e)
{
Canvas.SetZIndex(this, (Canvas.GetZIndex(this) + 1));
}
void Blank_MouseEnter(object sender, MouseEventArgs e)
{
imgclose.Visibility = System.Windows.Visibility.Visible;
}
void Blank_MouseLeave(object sender, MouseEventArgs e)
{
imgclose.Visibility = System.Windows.Visibility.Hidden;
}
void imgclose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (System.Windows.MessageBox.Show("Are you sure?", "Confirm", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
{
ImageBoxes.Remove(this);
(this.Parent as System.Windows.Controls.Canvas).Children.Remove(this);
}
else
{
}
}
}
This class displays an image (chosen from a dialog box).
How can I modify it to make it play a video file ?
More precisely,
How should I modify the line
this.Background = new System.Windows.Media.ImageBrush(new System.Windows.Media.Imaging.BitmapImage(new Uri(label)));
path = label;
so that it plays a video file.
The Background property has to have some sort of Brush in it. A quick Google search came up with this: VideoBrush. I hope this is what you were looking for.
I have a listbox in C# and want it to refresh after I added a new item(which gets opened with a new form dialog)
Here is my code which doesn't work.
private void showAllItems()
{
itemList = Db.getAllItems();
lb_itemList.DataSource = itemList;
}
private void showItemPreview(object sender, EventArgs e)
{
string curItem = lb_itemList.SelectedItem.ToString();
briefPreviewList = Db.getItemBriefPreview(curItem);
string itemInfos = string.Join(",", briefPreviewList.ToArray());
string[] infos = itemInfos.Split(',');
l_itemDB.Text = curItem;
l_CategoryDB.Text = infos[0];
}
private void b_addItem_Click(object sender, EventArgs e)
{
int uid = 1;
AddItem addItemForm = new AddItem(uid);
addItemForm.ShowDialog();
CurrencyManager cm = (CurrencyManager)BindingContext[itemList];
cm.Refresh();
}
I assume when you insert a new item it gets stored into the database, if this is the case then all you need to do is reset the datasource:
private void b_addItem_Click(object sender, EventArgs e)
{
int uid = 1;
AddItem addItemForm = new AddItem(uid);
addItemForm.ShowDialog();
addItemForm.Dispose();
this.showAllItems();
}
nothing happen and nothing change when I use datagridview and SubmitChanges()
private void Form1_Load(object sender, EventArgs e)
{
this.book_infoDataGridView.DataSource = bookstore.book_info;
BindingSource bds=new BindingSource();
bds.DataSource = this.bookstore.book_info;
this.book_infoBindingSource = bds;
this.book_infobindingNavigator.BindingSource = bds;
}
private void save_Click(object sender, EventArgs e)
{
this.book_infoBindingSource.EndEdit();
bookstore.SubmitChanges();
}
I believe you're trying to .SubmitChanges() from your data in the DataGridView
private void save_Click(object sender, EventArgs e)
{
using (var bookstoreContext = new yourContext())
{
var b = (BookClass)bds.Current;
var book = bookstoreContext.Products.Single(c => c.BookId == b.BookId); //or .First()
book.BookName = b.BookName;
book.BookInfo = b.BookInfo;
bookstoreContext.SubmitChanges();
}
}
I have a TextBox in my winforms. When a user starts typing in it I will like to set the AcceptButton property to call another function. However it is calling another function which is called by a Button in my ToolStrip. To elaborate, here is my code below:
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = searchBtn;
}
private void searchBtn_Click_1(object sender, EventArgs e)
{
if (locNameTxtBx.Text != "")
{
List<SearchLocation> locationsArray = new List<SearchLocation>();
var location = locNameTxtBx.Text;
SearchLocation loc = new SearchLocation();
loc.Where = location;
locationsArray.Add(loc);
mapArea.VE_FindLocations(locationsArray, true, true, null);
mapArea.VE_SetZoomLevel(14);
}
else
{
MessageBox.Show("Please Enter Location");
}
}
searchBtn is a Button in the ToolStrip. So, when I try to run this code, I get this error
Cannot implicitly convert type 'System.Windows.Forms.ToolStripButton' to 'System.Windows.Forms.IButtonControl'. An explicit conversion exists (are you missing a cast?)
I have tried casting it as a ToolstripButton like this:
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = (ToolStripButton)searchBtn;
}
You could use two delegates and set the delegate to use in the locNameTxtBx_TextChanged method.
private delegate void ToUseDelegate();
ToUseDelegate delegateIfNoText = delegate{
MessageBox.Show("Please Enter Location");
}
ToUseDelegate delegateIfText = delegate{
List<SearchLocation> locationsArray = new List<SearchLocation>();
var location = locNameTxtBx.Text;
SearchLocation loc = new SearchLocation();
loc.Where = location;
locationsArray.Add(loc);
mapArea.VE_FindLocations(locationsArray, true, true, null);
mapArea.VE_SetZoomLevel(14);
}
ToUseDelegate delToUse = delegateIfNoText;
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = searchBtn;
if (locNameTxtBx.Text != ""){
delegateToUse = delegateIfNoText;
} else {
delegateToUse = delegateIfText;
}
}
private void searchBtn_Click_1(object sender, EventArgs e)
{
delegateToUse();
}