Open the last used RibbonTab when the wpf RibbonWindow is loaded - c#

How can I return to the last used RibbonTab that was in focus when the window was last closed?

You could create a variable to hold the reference to the RibbonTab and listen to the SelectionChanged event on your Ribbon object.
MyRibbonObj.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
{
RibbonTab rt = ((sender as Ribbon).SelectedItem as RibbonTab);
MyReferenceToRibbonTab = rt;
}
This way you keep track of the latest selected RibbonTab within your Ribbon.
PS: code might needs tweaks. I didn't test it.

Create a setting LastRibbonTab
Save the last tab used in MainWindow_Closed
MainWindow_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.LastRibbonTab = (MyRibbon.SelectedItem as RibbonTab).Header.ToString();
Properties.Settings.Default.Save();
}
Select the last tab in MainWindow_Loaded
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Init();
foreach (RibbonTab r in MyRibbon.Items)
if (r.Header.ToString() == Properties.Settings.Default.LastRibbonTab)
{
MyRibbon.SelectedItem = r;
break;
}
}

Related

Edit Items in a ListBox

I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.

On listbox double click open new form

Im using windowsForms c# have a listbox which is bound with data items ...So depending on double click of the listbox item i want the corressponding form open...thnx in adv
ListBox has an DoubleClick event. You can access it if you select object, open Events tab in a Properties window.
Double click it and Visual Studio will create an event handler for you like this:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
// here is your code
}
Now, you just need to enter your code.
If you want to open a form with a corresponding item then it will be something like:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
string text = listBox1.Text; // Don't forget to manipulate with it
Form1 form = new Form1();
form.Show();
}
Subscribe to DoubleClick event on ListBox
listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
private void listBox1_DoubleClick(object sender, EventArgs e)
{
// logic here
}
or
listBox1.DoubleClick += (s,e) => { /*logic here */};
I would use the MouseDoubleClick Event, it provides the cursor position in MouseEventArgs so you can easily detect which item got doubleclicked.
void Listbox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = Listbox1.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
object clickedItem = Listbox1.Items[index];
// open your form here
}
}

How to create a right click event handler in c#

I would please like to know how to display a context menu when you right click on the window.
Here is my code so far :
private void ShowContextMenu_RightClick(object sender, EventArgs e)
{
toolStripMenuItem5.Visible = true;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programmed by D & K");
}
AFAIK there is no direct RightClick event in winforms. You can use the mousedown event to achieve this
private void toolStripButton1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programed by D & K");
}
}
in form cs file you can attach your context menu like this..
public Form1()
{
InitializeComponent();
//Create right click menu..
ContextMenuStrip s = new ContextMenuStrip();
// add one right click menu item named as hello
ToolStripMenuItem hello = new ToolStripMenuItem();
hello.Text = "Hello";
// add the clickevent of hello item
hello.Click += hello_Click;
// add the item in right click menu
s.Items.Add(hello);
// attach the right click menu with form
this.ContextMenuStrip = s;
}
void hello_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Clicked");
}
You should use MouseDown. Then you can get the clicked button with e.Button and the coordinates with e.X and e.Y.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show(e.Button.ToString() + " " + e.X + " " + e.Y);
}
Add a ContextMenuStrip control to the form (note, it does not display on the form but is instead shown at the bottom of the designer).In the form's ContextMenuStrip property, choose the name of the ContextMenuStrip control you just added to the form.That's it. HansPassant stated this in a comment to the question but I think it is being overlooked.
The ContextMenuStrip property is a property of many UI controls that you would use the same technique for.

DoubleClick on a row in ListView

Is there any possibility to get a value of doubleclicked row in ListView?
I registered an event:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems.ToString());
}
But on message, when i doubleclick some row in listview i get:
System.Windows.Forms.ListView+SelectedListViewItemCollection
What is more, I have got 2 columns in listview:
lvLista.Columns.Add("ID");
lvLista.Columns.Add("Tilte");
And i want to show in messagebox the "ID" of doubleclicked row.
How to do it? How to get a values from this event?
If you handle the MouseDown and/or MouseDoubleClick events of the ListView control, and use the HitTest method to determine the target of the mouse action, you will know which item has been double clicked. This is also a good means to determine if NO item was clicked (for example, clicking on the empty area in a partially filled list.
The following code will display the clicked item in a textbox if a single click occurs, and will pop up a message box with the name of the double-clicked item if a double click occurs.
If the click or double click occur in an area of the list view not populated by an item, the text box or message box inform yopu of that fact.
This is a trivial example, and depending on your needs, you will have to mess with it a little.
UPDATE: I added some code which clears the SelectedItems property of the Listview control when an empty area of the list is clicked or double-clicked.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);
listView1.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
this.SetupListview();
}
private void SetupListview()
{
ListView lv = this.listView1;
lv.View = View.List;
lv.Items.Add("John Lennon");
lv.Items.Add("Paul McCartney");
lv.Items.Add("George Harrison");
lv.Items.Add("Richard Starkey");
}
void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
MessageBox.Show("The selected Item Name is: " + item.Text);
}
else
{
this.listView1.SelectedItems.Clear();
MessageBox.Show("No Item is selected");
}
}
void listView1_MouseDown(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
this.textBox1.Text = item.Text;
}
else
{
this.listView1.SelectedItems.Clear();
this.textBox1.Text = "No Item is Selected";
}
}
}
Try this:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems[0].SubItems[0].Text);
}
I know this thread is old but nobody here answered the question properly in my opinion. For those in the future, try this, from MSDN:
// User must double-click to activate item
myListView.Activation = System.Windows.Forms.ItemActivation.Standard;
// Add event handler
myListView.ItemActivate += new
System.EventHandler(this.myListView_ItemClick);
Since the accepted answer didn't help me i thought that I would share my solution to the same problem: getting data from a specific column in a listview in the double click event.
The following line returns the data of the second column in the row that I've double clicked on as a string:
private void listViewOutput_DoubleClick(object sender, EventArgs e)
{
string content = listViewOutput.Items[listViewOutput.SelectedIndices[0]].SubItems[1].Text
}
Thanks; this is what I needed. I thought I'd also mention one could set up the local info variable more generally as:
ListViewHitTestInfo info = ((ListView)sender).HitTest(e.X, e.Y);
Try this
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
Rectangle rowBounds = hit.SubItem.Bounds;
Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label);
int leftMargin = labelBounds.Left - 1;
string x = hit.Item.Text;
}

C# ListView with CheckBoxes, automatic checkbox checked when multi select rows

I'm using a ListView control with multirow and fullrow select on. When I'm selecting multiple rows at once, some of my rows magically become checked. This happens when dragging the mouse over and also when selecting one, and shift clicking another.
See image describing issue here:
What in the grapefruit is going on? Anyone?
Unfortunately there are bugs in the ListView class, this is one of them. The following code is a fix that worked for me.
Edit: Sorry, this doesn't work quite right, although it does prevent the error that you show in your question. This prevents selecting multiple items and then checking them by clicking the check box.
void SetupListView()
{
listView.ItemCheck += new ItemCheckEventHandler(listView_ItemCheck);
listView.MouseDown += new MouseEventHandler(listView_MouseDown);
listView.MouseUp += new MouseEventHandler(listView_MouseUp);
listView.MouseLeave += new EventHandler(listView_MouseLeave);
}
bool mouseDown = false;
void listView_MouseLeave(object sender, EventArgs e)
{
mouseDown = false;
}
void listView_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
void listView_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
if(mouseDown)
{
e.NewValue = e.CurrentValue;
}
}
I answered this in another forum:
http://www.codeproject.com/Messages/3417741/Re-how-to-disable-multi-select-of-checkbox-in-list.aspx
HTH - hground
it`s simple question
just try this
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (ModifierKeys == Keys.Control || ModifierKeys == Keys.Shift)
{
e.NewValue = e.CurrentValue;
}
}

Categories

Resources