|
Hi,
The default scrollbar of windows seems to be to narrow for the ListView i'm developing for the application, this is why i have created a custom ScrollBar. The question is how do i attach the scrollbar to the listview that when scrolled the listview will scroll.
Please advice.
Thanks
|
|
|
|
|
|
I am Manually calling a context menu from a listview based on if it is on an item or not. When i right-click, the function validates if an item is present or not, then shows the correct contextMenu.
The problem that i am running into is that the context menu is showing quiet a bit off of the correct coordinates, (top-left of the mouse, off by an inch or so)
Here is an exert of that code.
private void iconView_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ListViewHitTestInfo hitTestInfo = iconView.HitTest(e.Location);
string filePath, fName;
if (hitTestInfo.Item != null)
{
fName = hitTestInfo.Item.Text;
filePath = getNodePath();
filePath = filePath + @"\" + fName;
ShellMenu.ShellContextMenu ctxMnu = new ShellMenu.ShellContextMenu();
FileInfo[] arrFI = new FileInfo[1];
arrFI[0] = new FileInfo(filePath);
ctxMnu.ShowContextMenu(arrFI, this.PointToScreen(e.Location));
}
else
{
iconContext.Visible = true;
iconContext.Show(this, e.Location);
}
}
}
}
Here is the kicker, i am using a fellow coders ShellContextCode, and it is doing the same thing, so i am pretty sure something is not happening on an application or system level.
-Edit-
Just had a brainstorm, maybe the location being reported is relative to the app, then the input is relative to the screen. I am going to test this while waiting for an experts view.
|
|
|
|
|
kodysumter wrote: ctxMnu.ShowContextMenu(arrFI, this.PointToScreen(e.Location));
I wonder why you are doing it on screen coordinates?
i think you should simply do:
ctxMnu.Show(iconView, e.X, e.Y);
this will show the context menu where you right clicked. do a hit test to confirm whether you should or shouldn't show the contextmenu.
|
|
|
|
|
Ive used all variations.
ctxMnu.Show(iconView, e.X, e.Y);
ctxMnu.Show(iconView, e.Location);
ctxMnu.Show(iconView, this.PointToScreen(e.X, e.Y));
ctxMnu.Show(iconView, this.PointToScreen(e.Location));
This is happening with BOTH menus that are popping up, (the context menu, and the Shell Context.
|
|
|
|
|
Usually, the best method that worked for me so far is:
1. Let windows handle how to select an item. This is automatically done when mousedown is fired.
2. check for any item being selected. if it is selected, show the menu otherwise not.
this is a code snippet which is working.
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
contextMenuStrip1.Show(listView1, e.X, e.Y);
}
}
|
|
|
|
|
That would work, if an item was deselected before. Problem is that an item will remain highlighted and selected if i right click white space within a listview control. And i cannot/will not disable the selected item because it is needed for some data being showed within the context menu.
|
|
|
|
|
Could you clarify?
kodysumter wrote: item will remain highlighted and selected if i right click white space within a listview control
This can be sorted in MouseDown event. Do a hit test to check if there is an item under the mouse. If not, deselect items in the listview. At Mouseup event, since no items are selected, menu won't be displayed.
kodysumter wrote: And i cannot/will not disable the selected item because it is needed for some data being showed within the context menu.
I do not understand this part. Do you want to show the menu when there is no item selected? or the mouse is right clicked at some place where there is no item? In that case too, you should deselect the listview at mousedown and then show a different contextmenu at mouseup. because, ultimately, there is no item selected when the mouse is clicked elsewhere.
|
|
|
|
|
hey guys...i have a combobox in my form and i fill it with one column from my db...everything is fine till here...and i open another form if the user wants to add another data different than the db that are in combobox...and in new form user enter new data to a textbox and i write that data to a txt file...and i want to add that data to combobox also..that is why i wrote that function
public void DosyaOku(object sender, EventArgs e)
{
StreamReader read = new StreamReader("Firmalar.txt");
string kelime = read.ReadLine();
while (kelime != null)
{
cmbFirma.Items.Add(kelime);
kelime = read.ReadLine();
}
read.Close();
}
but whenever i try to call it..the program gives error and says combobox has a datasource already
so how can i add new data to combobox ??
thanks for your help
|
|
|
|
|
Try setting the datasource equal to null.
|
|
|
|
|
where should i make it null ????
|
|
|
|
|
Hello
I have the following piece of code (RSSToolkit). The RSS feed that I try to parse has multiple link elements and each such element has "rel" as attribute but with a different value. I'm interested in the link element with "absolute" as "rel" value.
[XmlElementAttribute("link")]
[XmlAttributeAttribute(AttributeName = "rel")]
public string LinkAlternate
{
get
{
return _link_alternate;
}
set
{
_link_alternate = value;
}
}
Thanks for your time
|
|
|
|
|
Hi. I'm having a problem with focusing from textbox to a button. Is there a way in doing this? I tried coding it but it doesn't work. Here's my code so far:
The textbox:
private void txtBoxLogIn_TextChanged(object sender, EventArgs e)
{
txtBoxLogIn.KeyPress += new KeyPressEventHandler(txtBoxLogIn_KeyPress);
}
and the eventhandler:
private void txtBoxLogIn_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
txtBoxPassword.Focus();
}
else if (e.KeyChar == (char)Keys.Enter && txtBoxPassword.Focused == true)
{
btnProceedLogIn.Focus();
}
}
I'll be using only one eventhandler in transferring focus to other controls.
|
|
|
|
|
Is it a login dialog? and you want btnProceedLogIn to behave as default button?
set the DialogResult property of btnProceedLogIn to OK and then whenever you press Enter, btnProceedLogIn will be activated.
Now, in the code, if the login is successful, and you want to close the form, no need to do anything else.
In case the login is unsuccessful, add a line:
this.DialogResult = DialogResult.None;
This will ensure that form will not be closed.
|
|
|
|
|
Actually to have a button respond to the enter key you need to set the AcceptButton property of the form that the button is on.
|
|
|
|
|
There is a catch. If you only set AcceptButton Property, the Button doesn't change its DialogResult property.
This however is not true for Cancel button in which case, Cancel button dialogresult is changed.
|
|
|
|
|
To have a button respond to a key press, you set the AcceptButton property of the form for enter and the CancelButton property for the esc key the DialogResult property of the buttons has no effect on this what so ever.
The DialogResult property of the buttons is only used to control what is returned as a DialogResult when you use ShowDialog() to display a form.
-- EDIT --
As the OP is trying to move focus from control to control when the enter key is pressed, DialogResult really has no bearing on the OP question.
|
|
|
|
|
gamer1127 wrote: private void txtBoxLogIn_TextChanged(object sender, EventArgs e)
{
txtBoxLogIn.KeyPress += new KeyPressEventHandler(txtBoxLogIn_KeyPress);
}
Why re-subscribe to the event every time the text changes? Shouldn't you only need to subscribe to that once?
gamer1127 wrote: I'll be using only one eventhandler in transferring focus to other controls.
Why? Have you tried separate handlers for each control? That might be the source of the problem.
[EDIT]
Actually, that is the source of the problem. The first if statement in the KeyPress handler doesn't check what control the Enter Key was pressed in. It just sets the focus to the password box regradless of where Enter was pressed.
|
|
|
|
|
Jimmanuel wrote: Why? Have you tried separate handlers for each control?
I just thought of applying the concept of having controls tagged to only one event-handler if they will have the same behavior.
If that is the case, then i'll try using one event-handler for each control.
|
|
|
|
|
But they don't all have the same behaviour, they have similar behaviour. It's not a bad idea, but you have to abstract away all of the things from the handler that are specific for each control. You can actually do this pretty elegantly with one handler by using the Control s Tag property and the sender param of the handler.
The idea is that the Enter key press transfers focus to the next Control , right? That's the similar behaviour the handler should implement but it needs to know which Control to transfer focus to. That's easy with the Tag . In the constructor of your form go through the similar behaving Control s and save a reference to the next Control that gets focus in the Tag :
txtBoxLogIn.Tag = txtBoxPassword;
txtBoxPassword.Tag = btnProceedLogIn;
btnProceedLogIn.Tag = null;
Then your handler simplifies to this:
private void focusForwardingControl_KeyDown(object sender, KeyEventArgs e)
{
Control control = sender as Control;
if (control == null)
{
return;
}
if (e.KeyCode == Keys.Enter)
{
Control nextControl = control.Tag as Control;
if (nextControl != null)
{
nextControl.Focus();
}
}
}
Then both text boxes can use to the same event handler to forward focus to the next Control in the chain.
Note that I'm using the KeyDown handler instead of KeyPress , I like the Event Args better.
Also, all code here was written in the CP post editor so it hasn't been tested or compiled
|
|
|
|
|
Ok. I'll try using this one and see if it works.
|
|
|
|
|
Your right about that. It is indeed the source of the problem. Thanks for that.
|
|
|
|
|
Now I have a new problem. Whenever I click the Log-In button it doesn't show an error message even if there's no entered data or the entered username and passwords are both wrong. It just do the event of logging in if both the username and passwords are correct. The username and password are saved in an MS Access database and i used SELECT query to check if both exists. Here is the code to clearly understand:
string sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Schedule.mdb";
try
{
OleDbConnection dbConnect = new OleDbConnection(sConnection);
dbConnect.Open();
string sqlString = "SELECT * FROM LogIn WHERE ID = '" + txtBoxLogIn.Text + "' AND Password = '" + txtBoxPassword.Text + "';";
OleDbCommand dbCmd = new OleDbCommand();
dbCmd.CommandText = sqlString;
dbCmd.Connection = dbConnect;
OleDbDataReader dbReader;
dbReader = dbCmd.ExecuteReader();
while (dbReader.Read())
{
if (dbReader[0].ToString() == txtBoxLogIn.Text && dbReader[1].ToString() == txtBoxPassword.Text )
{
frmMainForm frmMainForm = new frmMainForm();
frmMainForm.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("Either the Username or Password does not exist", "Log-In Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
dbReader.Close();
dbConnect.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
Did I do something wrong?
|
|
|
|
|
There are a couple things wrong:
* This deserves a new post instead of a reply to your original question.
* The reader and connection are not properly disposed of, they should be used in using statements to ensure that even if the Exception handler or this.Close() is called they get cleaned up properly.
* Catching the general Exception is considered bad practice.
* The case where dbReader.Read() returns false isn't handled - if there aren't any matching rows then the inner if - else will be skipped completely *** This is probably the cause of what you're seeing
* SELECT * is also not the best practice, it'd be better if you specify the column names in the SELECT .
* You should use a parameterized query to prevent SQL injection attacks. Here's and example for SqlConnection s: clickety[^], OleDbConnection s are similar
* shouldn't that while be an if ?
* If those column values might be null then they should be checked for DBNull.Value before they're ToString() ed
|
|
|
|
|
With all the things you pointed out, it means that I have to learn a lot of things when it comes to programming with relational database. Have to start reading all the ebooks that i got. We just started discussing programming with database and we already have a project that has it.
|
|
|
|