Anchoring checkbox in Datagridview - c#

I am creating a winform based Desktop application and I am using Datagridview to populate the data.
I am using checkbox in one of the header column. The Table is quiet big to fit into the screen and am using scroll bar to move in the horizontal and vertical direction.
The checkbox need to move as the scroll bar moves. However, the problem is it remains static.
Any idea how to anchor it, so that when the scroll bar moves, the checkbox moves accordingly.
Thanks
EDIT :
Auto generated Designer Code :
this.checkheader.AutoSize = true;
this.checkheader.BackColor = System.Drawing.Color.White;
this.checkheader.FlatAppearance.BorderColor = System.Drawing.Color.White;
this.checkheader.Location = new System.Drawing.Point(49, 96);
this.checkheader.Name = "checkheader";
this.checkheader.Size = new System.Drawing.Size(15, 14);
this.checkheader.TabIndex = 21;
this.checkheader.UseVisualStyleBackColor = false;
this.checkheader.CheckedChanged += new System.EventHandler(this.checkboxHeader_CheckedChanged);
//

You could use the datagridview 'scroll'-event to do something like this:
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation.Equals(ScrollOrientation.HorizontalScroll))
{
checkBox1.Location = new Point(checkBox1.Location.X - (e.NewValue - e.OldValue), checkBox1.Location.Y);
}
if (checkBox1.Location.X < dataGridView1.Location.X + 40)
{
checkBox1.Visible = false;
}
else
{
checkBox1.Visible = true;
}
}
Better late than never ?

Related

problems updating picture box control

I have two controls on the same form. Both controls contain an ObjectListView control. The one listview was created with the graphical editor in visual studio. This one is not causing any issues. The other listview is created programmatically at run-time. I have defined an event handler for each control that gets called when the hot item changes and they are both firing when they should. Both event handlers call the same code to update a picturebox control. The problem is that the picturebox does not get updated when the programmatically defined listview is asking it to. I am positive the event handler is getting called because my code writes to a text file as well as updating the picture box. The text file gets updated but the picture box does not. I have tried updating, invalidating, and refreshing the PicutureBox as well as the parent form, but I just can not get it to update.
I am not sure if this is an ObjectListView issue or a standard WinForms problem. I realize my question is very vague but I am not sure how to clarify it without posting all my code. Any advice would be appreciated.
Here is the code that the event handler calls:
public void ShowBitmap(object sender, HotItemChangedEventArgs e, ObjectListView lv, string type)
{
ObjectListView olv = sender as ObjectListView;
if (sender == null)
{
return;
}
switch (e.HotCellHitLocation)
{
case HitTestLocation.Nothing:
break;
case HitTestLocation.Group:
break;
case HitTestLocation.GroupExpander:
break;
default:
if (e.HotColumnIndex == 0)
{
pictureBox1.Hide();
pictureBox1.BorderStyle = BorderStyle.FixedSingle;
int rowIndex = e.HotRowIndex;
string text = "";
if (type == "Main Parts")
{
TypedObjectListView<MainRadanProjectPartsPart> tlist = new TypedObjectListView<MainRadanProjectPartsPart>(lv);
text = tlist.Objects[rowIndex].Symbol;
}
else if (type == "Parts")
{
TypedObjectListView<RadanProjectPartsPart> tlist = new TypedObjectListView<RadanProjectPartsPart>(lv);
text = tlist.Objects[rowIndex].Symbol;
}
else if (type == "Nests")
{
TypedObjectListView<MainRadanProjectNestsNest> tlist = new TypedObjectListView<MainRadanProjectNestsNest>(lv);
text = tlist.Objects[rowIndex].FileName;
}
if (text != null)
{
Point screenCoords = Cursor.Position;
Point controlRelatedCoords = lv.PointToClient(screenCoords);
if (controlRelatedCoords.Y < oldCursorPosition.Y)
{
pictureBox1.Location = controlRelatedCoords;
int xPos = controlRelatedCoords.X;
int yPos = controlRelatedCoords.Y + 60;
pictureBox1.Location = new Point(xPos, yPos);
}
else if (controlRelatedCoords.Y > oldCursorPosition.Y)
{
pictureBox1.Location = controlRelatedCoords;
int xPos = controlRelatedCoords.X;
//int yPos = controlRelatedCoords.Y - pictureBox1.Height;
int yPos = controlRelatedCoords.Y - pictureBox1.Height + 30;
pictureBox1.Location = new Point(xPos, yPos);
}
pictureBox1.Show();
pictureBox1.BringToFront();
olvTreeViewMainParts.Focus();
lv.Focus();
pictureBox1.Visible = true;
DrawSymbol(text);
oldCursorPosition = controlRelatedCoords; // save the cursor position to track cursor direction between calls
}
else
{
DrawSymbol("");
}
}
else
{
pictureBox1.Hide();
}
break;
}
}
Here is the event handler for the programmaticaly defined listview:
// track the cursor as it moves over the items in the listview
private void olvPartsListView_HotItemChanged(object sender, HotItemChangedEventArgs e)
{
ShowBitmap(sender, e, olvPartsListView, "Parts");
}

WPF C# Drag and Drop Drag Element as Cursor

i am new to programming and i am trying to do a drag and drop here , i can drag and drop now but the custom cursor to drag and drop is ugly , how do i drag the element that i am draging as the cursor? i search online and found some mentioning about adorner but i don't even understand the codes . Is there any simple or simplified and better way to do this?
I have this code here that i can drag and drop ( i dynamically created textbox and label in a for loop , i retrieve the text and append it to label from a database :
TextBox tbox = new TextBox();
tbox.Width = 250;
tbox.Height = 50;
tbox.AllowDrop = true;
tbox.FontSize = 24;
tbox.BorderThickness = new Thickness(2);
tbox.BorderBrush = Brushes.BlanchedAlmond;
tbox.PreviewDrop += new DragEventHandler(tbox_Drop);
if (lstQuestion[i].Answer.Trim().Length > 0)
{
wrapPanel2.Children.Add(tbox);
answers.Add(lbl.Content.ToString());
MatchWords.Add(question.Content.ToString(), lbl.Content.ToString());
}
Dictionary<string, string> shuffled = Shuffle(MatchWords);
foreach (KeyValuePair<string, string> s in shuffled)
{
Label lbl = new Label();
lbl.Content = s.Value;
lbl.Width = 100;
lbl.Height = 50;
lbl.FontSize = 24;
lbl.DragEnter += new DragEventHandler(lbl_DragEnter);
// lbl.MouseMove += new MouseEventHandler(lbl_MouseMove);
lbl.MouseDown +=new MouseButtonEventHandler(lbl_MouseDown);
dockPanel1.Children.Add(lbl);
}
I am dragging labels(drag target) into textbox( drop target ) , which event should i use and how i write the events to set the drag cursor to the labels that i am dragging?
Here are the events i have used atm :
private void tbox_Drop(object sender, DragEventArgs e)
{
MessageBox.Show("Are you sure ? Wrong don't blame me ");
(sender as TextBox).Text = string.Empty;
}
private void lbl_DragEnter(object sender, DragEventArgs e)
{
if (sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
Any solutions or help is appreciated , I've seen adorner , its way too complicated for me to understand to implement it . Looking for a simple and simplified way to do this .
You can use opensource like FluidKit
http://fluidkit.codeplex.com/
Tutorials for the same
http://blog.pixelingene.com/2006/11/drag-drop-with-attached-properties
http://blog.pixelingene.com/2006/11/drag-drop-with-attached-properties-part-2
http://blog.pixelingene.com/2006/12/drag-drop-with-attached-properties-part-3
http://blog.pixelingene.com/2006/12/drag-drop-with-attached-properties-part-4
http://blog.pixelingene.com/2007/07/making-the-scrollbar-work-with-dragdropmanager
http://blog.pixelingene.com/2007/11/improved-dragdropmanager-source-code

Form based on selected menu item

I have made a sidebar that I dock on the left side of my application.
Now I'm wondering what's the best way to show a form based on the menu option they've selected from my sidebar.
This is basically what I want to do:
http://www.dreamincode.net/forums/topic/176782-building-an-application-poscash-register-part-one/
On the left is my menu bar, on the right side I want to have a form based on the option clicked on the left.
I have looked into MDI but when I do that I always get a ControlBox even though I've disabled it in the child form.
Update: Seems that this works as well:
Looks like you can also create your own user controls to do it:
User controls
If you want second wont allow you to access your main form unless its closed.then use this...
second_form_name sfn = new second_form_name();
sfn.ShowDialog();
If you want second allow you to access your main form unless its closed.then use
second_form_name sfn = new second_form_name();
sfn.Show();
First create a panel in your form that will hold your current form's contents (add it to the form but leave it empty for now).
Panel active = new Panel();
this.Controls.Add(active);
Then create a panel containing controls for each form you want to display.
Panel firstFormPanel = new Panel();
firstFormPanel.Add(new Button());
Panel secondFormPanel = new Panel();
secondFormPanel.Add(new Button());
Now assign which panel you want by default:
active = firstFormPanel;
Then when you want to change to a new form (click event in sidebar), assign one of the panels to the active panel like so:
active.Visible = false;
active = secondFormPanel;
active.Visible = true;
Here's a little example i have of a sidebar from my game im making being able to slide a new submenu into visual range. I use a timer to control movement and a list of submenus also a index to determine which to show. as far as i've gotten along theres only one so far so not a prime example.
public List<UserControl> Submenus = new List<UserControl>();
Multiplayer_Menu MPM;
enum At { Left, Right }
At Current = At.Right;
At Go_to = At.Right;
int Submenu_Index = 0;
bool done = false;
public void Load_Submenus()
{
Multiplayer_Menu MM = new Multiplayer_Menu(this);
MainMenu.Controls.Add(MM);
MM.Location = new Point(MainMenu.Size.Width, 0);
MM.Visible = false;
Submenus.Add(MM);
PictureBox PB = new PictureBox();
MainMenu.Controls.Add(PB);
PB.Location = new Point(MainMenu.Size.Width, 0);
PB.Size = new System.Drawing.Size(924, 736);
PB.SizeMode = PictureBoxSizeMode.StretchImage;
PB.ImageLocation = "http://www.taloreal.com/Earth%20Rotation/Rotating.gif";
PB.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
Load_Submenus();
}
public void MML_Multiplayer_Click(object sender, EventArgs e)
{
Submenus[Submenu_Index].Visible = false;
if (Current == At.Left)
Go_to = At.Right;
if (Current == At.Right)
Go_to = At.Left;
ShowHideMenus.Enabled = true;
Submenu_Index = 0;
}
private void ShowHideMenus_Tick(object sender, EventArgs e)
{
Point P = new Point(MainMenu.Location.X, MainMenu.Location.Y);
Size S = new Size(MainMenu.Size.Width, MainMenu.Size.Height);
if (Go_to == At.Left)
{
P = new Point(P.X - 30, P.Y);
S = new Size(S.Width + 30, S.Height);
if (P.X == 0)
{
Submenus[Submenu_Index].Visible = true;
ShowHideMenus.Enabled = false;
Current = Go_to;
}
}
if (Go_to == At.Right)
{
P = new Point(P.X + 30, P.Y);
S = new Size(S.Width - 30, S.Height);
if (P.X == 930)
{
ShowHideMenus.Enabled = false;
Current = Go_to;
}
}
Reposition_Menu(P, S);
}
void Reposition_Menu(Point P, Size S)
{
MainMenu.Location = P;
MainMenu.Size = S;
}

Docking/Displaying Mutiple DataGridViews with Splitters Programmatically

All, I want to build and display multiple DataGridView seperated by horizontol Splitters at runtime. To test out doing this I have created a test application with the following code
private void button1_Click(object sender, EventArgs e)
{
int i = 1;
List<DataGridView> DgvList = new List<DataGridView>()
{
new DataGridView(), new DataGridView()
};
foreach (DataGridView Dgv in DgvList)
{
Dgv.Parent = this.panelMain;
int verticalSize = (int)(panelMain.Height / DgvList.Count);
Dgv.Height = verticalSize;
Dgv.Dock = DockStyle.Top;
if (DgvList.Count > 1 && i < DgvList.Count)
{
Splitter tmpSplitter = new Splitter();
tmpSplitter.Parent = this.panelMain;
tmpSplitter.Dock = DockStyle.Top;
tmpSplitter.BringToFront();
tmpSplitter.Height = 8;
}
i++;
}
}
However, this is not displaying the Splitter
Can someone highlight the error of my ways?
Thanks for your time.
Drop this line:
tmpSplitter.BringToFront();
and splitter will show.
Note that you are displaying grids in reversed order - the first in list will be at bottom of the screen.

How to scroll in flowlayout panel without showing scrollbar in windows form

I am working on a touch screen POS in WinForms.
I have a flowlayoutpanel and add buttons dynamically but I dont want to show a scrollbar.
I use 2 buttons to scroll instead, so please help me how to scroll without showing a scrollbar
Try placing the FlowLayoutPanel inside another panel with these properties:
flowLayoutPanel1.AutoScroll = false;
flowLayoutPanel1.AutoSize = true;
flowLayoutPanel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
From here, you have to control yourself the location of the FlowLayoutPanel1 inside your panel (which should also have AutoScroll = false;) based on your two buttons.
Take two buttons btnLeft and btnRight and try this code :
private void btnLeft_Click(object sender, EventArgs e)
{
if (flowPanelItemCategory.Location.X <= xpos)
{
xmin = flowPanelItemCategory.HorizontalScroll.Minimum;
if (flowPanelItemCategory.Location.X >= xmin)
{
xpos -= 100;
flowPanelItemCategory.Location = new Point(xpos, 0);
}
}
}
private void btnRight_Click(object sender, EventArgs e)
{
if (flowPanelItemCategory.Location.X <= xpos)
{
xmax = flowPanelItemCategory.HorizontalScroll.Maximum;
if (flowPanelItemCategory.Location.X < xmax)
{
xpos += 100;
flowPanelItemCategory.Location = new Point(xpos, 0);
}
}
}

Categories